Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2817-BinPackingSpeedup/HeuristicLab.Encodings.ScheduleEncoding/3.3/ScheduleEncoding/Job.cs @ 15306

Last change on this file since 15306 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.ComponentModel;
24using System.Text;
25using HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.ScheduleEncoding {
31  [Item("Job", "Represents a composition of tasks that require processing in a scheduling problem.")]
32  [StorableClass]
33  public class Job : Item, INotifyPropertyChanged {
34
35    [Storable(Name = "DueDate")]
36    private double dueDate;
37    public double DueDate {
38      get { return dueDate; }
39      set {
40        bool changed = dueDate != value;
41        dueDate = value;
42        if (changed) {
43          OnPropertyChanged("DueDate");
44          OnToStringChanged();
45        }
46      }
47    }
48
49    [Storable(Name = "Index")]
50    private int index;
51    public int Index {
52      get { return index; }
53      set {
54        bool changed = index != value;
55        index = value;
56        if (changed) {
57          OnPropertyChanged("Index");
58          OnToStringChanged();
59        }
60      }
61    }
62
63    [Storable(Name = "Tasks")]
64    private ItemList<Task> tasks;
65    public ItemList<Task> Tasks {
66      get { return tasks; }
67      set {
68        bool changed = tasks != value;
69        tasks = value;
70        if (changed) OnPropertyChanged("Tasks");
71      }
72    }
73
74    [StorableConstructor]
75    protected Job(bool deserializing) : base(deserializing) { }
76    protected Job(Job original, Cloner cloner)
77      : base(original, cloner) {
78      this.dueDate = original.DueDate;
79      this.index = original.Index;
80      this.tasks = cloner.Clone(original.Tasks);
81      RegisterEventHandlers();
82    }
83    public Job() : this(-1, double.MaxValue) { }
84    public Job(int index, double dueDate)
85      : base() {
86      this.dueDate = dueDate;
87      this.index = index;
88      this.tasks = new ItemList<Task>();
89      RegisterEventHandlers();
90    }
91
92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new Job(this, cloner);
94    }
95
96    [StorableHook(HookType.AfterDeserialization)]
97    private void AfterDeserialization() {
98      RegisterEventHandlers();
99    }
100
101    private void RegisterEventHandlers() {
102      Tasks.ItemsAdded += TasksOnItemsChanged;
103      Tasks.ItemsRemoved += TasksOnItemsRemoved;
104      Tasks.ItemsReplaced += TasksOnItemsChanged;
105      Tasks.CollectionReset += TasksOnItemsChanged;
106      foreach (var task in Tasks) {
107        task.PropertyChanged += TaskOnPropertyChanged;
108        task.ToStringChanged += TaskOnToStringChanged;
109      }
110    }
111
112    private void TasksOnItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<Task>> e) {
113      foreach (var task in e.OldItems) {
114        task.Value.PropertyChanged -= TaskOnPropertyChanged;
115        task.Value.ToStringChanged -= TaskOnToStringChanged;
116      }
117      foreach (var task in e.Items) {
118        task.Value.PropertyChanged += TaskOnPropertyChanged;
119        task.Value.ToStringChanged += TaskOnToStringChanged;
120      }
121      OnTasksChanged();
122      OnToStringChanged();
123    }
124
125    private void TasksOnItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<Task>> e) {
126      foreach (var task in e.Items) {
127        task.Value.PropertyChanged -= TaskOnPropertyChanged;
128        task.Value.ToStringChanged -= TaskOnToStringChanged;
129      }
130      OnTasksChanged();
131      OnToStringChanged();
132    }
133
134    private void TaskOnPropertyChanged(object sender, EventArgs e) {
135      OnTasksChanged();
136    }
137
138    private void TaskOnToStringChanged(object sender, EventArgs e) {
139      OnToStringChanged();
140    }
141
142    public override string ToString() {
143      var sb = new StringBuilder();
144      sb.Append("Job#" + Index + " [ ");
145      foreach (Task t in Tasks) {
146        sb.Append(t + " ");
147      }
148      sb.Append("{" + DueDate + "} ");
149      sb.Append("]");
150      return sb.ToString();
151    }
152
153    internal Task GetPreviousTask(Task t) {
154      if (t.TaskNr == 0) return null;
155      return Tasks[t.TaskNr - 1];
156    }
157
158    public event EventHandler TasksChanged;
159    protected virtual void OnTasksChanged() {
160      var handler = TasksChanged;
161      if (handler != null) handler(this, EventArgs.Empty);
162    }
163
164    public event PropertyChangedEventHandler PropertyChanged;
165    protected virtual void OnPropertyChanged(string propertyName) {
166      var handler = PropertyChanged;
167      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
168    }
169  }
170}
Note: See TracBrowser for help on using the repository browser.