Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16204 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 5.3 KB
RevLine 
[6475]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6475]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
[11073]22using System;
[8886]23using System.ComponentModel;
[6475]24using System.Text;
[11073]25using HeuristicLab.Collections;
[6475]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]
[8886]33  public class Job : Item, INotifyPropertyChanged {
[8887]34
35    [Storable(Name = "DueDate")]
36    private double dueDate;
[8886]37    public double DueDate {
38      get { return dueDate; }
39      set {
40        bool changed = dueDate != value;
41        dueDate = value;
[11073]42        if (changed) {
43          OnPropertyChanged("DueDate");
44          OnToStringChanged();
45        }
[8886]46      }
47    }
[6475]48
[8886]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;
[11073]56        if (changed) {
57          OnPropertyChanged("Index");
58          OnToStringChanged();
59        }
[8886]60      }
61    }
62
63    [Storable(Name = "Tasks")]
[8887]64    private ItemList<Task> tasks;
[8886]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
[6475]74    [StorableConstructor]
75    protected Job(bool deserializing) : base(deserializing) { }
76    protected Job(Job original, Cloner cloner)
77      : base(original, cloner) {
[11073]78      this.dueDate = original.DueDate;
79      this.index = original.Index;
80      this.tasks = cloner.Clone(original.Tasks);
81      RegisterEventHandlers();
[6475]82    }
[8887]83    public Job() : this(-1, double.MaxValue) { }
[6475]84    public Job(int index, double dueDate)
85      : base() {
[11073]86      this.dueDate = dueDate;
87      this.index = index;
88      this.tasks = new ItemList<Task>();
89      RegisterEventHandlers();
[6475]90    }
91
[8887]92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new Job(this, cloner);
94    }
95
[11073]96    [StorableHook(HookType.AfterDeserialization)]
97    private void AfterDeserialization() {
98      RegisterEventHandlers();
99    }
100
101    private void RegisterEventHandlers() {
102      Tasks.ItemsAdded += TasksOnItemsChanged;
[11903]103      Tasks.ItemsRemoved += TasksOnItemsRemoved;
[11073]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
[11903]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
[11073]134    private void TaskOnPropertyChanged(object sender, EventArgs e) {
135      OnTasksChanged();
136    }
137
138    private void TaskOnToStringChanged(object sender, EventArgs e) {
139      OnToStringChanged();
140    }
141
[6475]142    public override string ToString() {
[8886]143      var sb = new StringBuilder();
[6475]144      sb.Append("Job#" + Index + " [ ");
145      foreach (Task t in Tasks) {
[11073]146        sb.Append(t + " ");
[6475]147      }
[7116]148      sb.Append("{" + DueDate + "} ");
[6475]149      sb.Append("]");
150      return sb.ToString();
151    }
152
153    internal Task GetPreviousTask(Task t) {
[8886]154      if (t.TaskNr == 0) return null;
155      return Tasks[t.TaskNr - 1];
[6475]156    }
157
[11073]158    public event EventHandler TasksChanged;
159    protected virtual void OnTasksChanged() {
160      var handler = TasksChanged;
161      if (handler != null) handler(this, EventArgs.Empty);
162    }
163
[8886]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    }
[6475]169  }
170}
Note: See TracBrowser for help on using the repository browser.