Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.ScheduleEncoding/3.3/Job.cs @ 15866

Last change on this file since 15866 was 13437, checked in by mkommend, 9 years ago

#2521: Added encodings for schedules.

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 sealed class Job : Item, INotifyPropertyChanged {
34
35    [Storable(Name = "DueDate")]
36    private double dueDate;
37    public double DueDate {
38      get { return dueDate; }
39      set {
40        if (dueDate == value) return;
41        dueDate = value;
42        OnPropertyChanged("DueDate");
43        OnToStringChanged();
44      }
45    }
46
47    [Storable(Name = "Index")]
48    private int index;
49    public int Index {
50      get { return index; }
51      set {
52        if (index == value) return;
53        index = value;
54        OnPropertyChanged("Index");
55        OnToStringChanged();
56      }
57    }
58
59    [Storable(Name = "Tasks")]
60    private ItemList<Task> tasks;
61    public ItemList<Task> Tasks {
62      get { return tasks; }
63      private set {
64        tasks = value;
65        OnPropertyChanged("Tasks");
66      }
67    }
68
69    [StorableConstructor]
70    private Job(bool deserializing) : base(deserializing) { }
71    private Job(Job original, Cloner cloner)
72      : base(original, cloner) {
73      this.dueDate = original.DueDate;
74      this.index = original.Index;
75      this.tasks = cloner.Clone(original.Tasks);
76      RegisterEventHandlers();
77    }
78    public Job() : this(-1, double.MaxValue) { }
79    public Job(int index, double dueDate)
80      : base() {
81      this.dueDate = dueDate;
82      this.index = index;
83      this.tasks = new ItemList<Task>();
84      RegisterEventHandlers();
85    }
86
87    public override IDeepCloneable Clone(Cloner cloner) {
88      return new Job(this, cloner);
89    }
90
91    [StorableHook(HookType.AfterDeserialization)]
92    private void AfterDeserialization() {
93      RegisterEventHandlers();
94    }
95
96    private void RegisterEventHandlers() {
97      Tasks.ItemsAdded += TasksOnItemsChanged;
98      Tasks.ItemsRemoved += TasksOnItemsRemoved;
99      Tasks.ItemsReplaced += TasksOnItemsChanged;
100      Tasks.CollectionReset += TasksOnItemsChanged;
101      foreach (var task in Tasks) {
102        task.PropertyChanged += TaskOnPropertyChanged;
103        task.ToStringChanged += TaskOnToStringChanged;
104      }
105    }
106
107    private void TasksOnItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<Task>> e) {
108      foreach (var task in e.OldItems) {
109        task.Value.PropertyChanged -= TaskOnPropertyChanged;
110        task.Value.ToStringChanged -= TaskOnToStringChanged;
111      }
112      foreach (var task in e.Items) {
113        task.Value.PropertyChanged += TaskOnPropertyChanged;
114        task.Value.ToStringChanged += TaskOnToStringChanged;
115      }
116      OnTasksChanged();
117      OnToStringChanged();
118    }
119
120    private void TasksOnItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<Task>> e) {
121      foreach (var task in e.Items) {
122        task.Value.PropertyChanged -= TaskOnPropertyChanged;
123        task.Value.ToStringChanged -= TaskOnToStringChanged;
124      }
125      OnTasksChanged();
126      OnToStringChanged();
127    }
128
129    private void TaskOnPropertyChanged(object sender, EventArgs e) {
130      OnTasksChanged();
131    }
132
133    private void TaskOnToStringChanged(object sender, EventArgs e) {
134      OnToStringChanged();
135    }
136
137    public override string ToString() {
138      var sb = new StringBuilder();
139      sb.Append("Job#" + Index + " [ ");
140      foreach (Task t in Tasks) {
141        sb.Append(t + " ");
142      }
143      sb.Append("{" + DueDate + "} ");
144      sb.Append("]");
145      return sb.ToString();
146    }
147
148    public event EventHandler TasksChanged;
149    private void OnTasksChanged() {
150      var handler = TasksChanged;
151      if (handler != null) handler(this, EventArgs.Empty);
152    }
153
154    public event PropertyChangedEventHandler PropertyChanged;
155    private void OnPropertyChanged(string propertyName) {
156      var handler = PropertyChanged;
157      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.