source: stable/HeuristicLab.Encodings.ScheduleEncoding/3.3/ScheduleEncoding/Job.cs @ 11073

Last change on this file since 11073 was 11073, checked in by ascheibe, 5 years ago

#2127 merged r10213, r10433, r10476, r10494 into stable

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 += TasksOnItemsChanged;
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 TaskOnPropertyChanged(object sender, EventArgs e) {
126      OnTasksChanged();
127    }
128
129    private void TaskOnToStringChanged(object sender, EventArgs e) {
130      OnToStringChanged();
131    }
132
133    public override string ToString() {
134      var sb = new StringBuilder();
135      sb.Append("Job#" + Index + " [ ");
136      foreach (Task t in Tasks) {
137        sb.Append(t + " ");
138      }
139      sb.Append("{" + DueDate + "} ");
140      sb.Append("]");
141      return sb.ToString();
142    }
143
144    internal Task GetPreviousTask(Task t) {
145      if (t.TaskNr == 0) return null;
146      return Tasks[t.TaskNr - 1];
147    }
148
149    public event EventHandler TasksChanged;
150    protected virtual void OnTasksChanged() {
151      var handler = TasksChanged;
152      if (handler != null) handler(this, EventArgs.Empty);
153    }
154
155    public event PropertyChangedEventHandler PropertyChanged;
156    protected virtual void OnPropertyChanged(string propertyName) {
157      var handler = PropertyChanged;
158      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.