Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16692 was 16692, checked in by abeham, 6 years ago

#2521: merged trunk changes up to r15681 into branch (removal of trunk/sources)

File size: 5.0 KB
RevLine 
[6475]1#region License Information
2/* HeuristicLab
[16692]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
[10433]22using System;
[8886]23using System.ComponentModel;
[6475]24using System.Text;
[10433]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]
[13435]33  public sealed 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 {
[13435]40        if (dueDate == value) return;
[8886]41        dueDate = value;
[13435]42        OnPropertyChanged("DueDate");
43        OnToStringChanged();
[8886]44      }
45    }
[6475]46
[8886]47    [Storable(Name = "Index")]
48    private int index;
49    public int Index {
50      get { return index; }
51      set {
[13435]52        if (index == value) return;
[8886]53        index = value;
[13435]54        OnPropertyChanged("Index");
55        OnToStringChanged();
[8886]56      }
57    }
58
59    [Storable(Name = "Tasks")]
[8887]60    private ItemList<Task> tasks;
[8886]61    public ItemList<Task> Tasks {
62      get { return tasks; }
[13435]63      private set {
[8886]64        tasks = value;
[13435]65        OnPropertyChanged("Tasks");
[8886]66      }
67    }
68
[6475]69    [StorableConstructor]
[13435]70    private Job(bool deserializing) : base(deserializing) { }
71    private Job(Job original, Cloner cloner)
[6475]72      : base(original, cloner) {
[10476]73      this.dueDate = original.DueDate;
74      this.index = original.Index;
75      this.tasks = cloner.Clone(original.Tasks);
[10433]76      RegisterEventHandlers();
[6475]77    }
[8887]78    public Job() : this(-1, double.MaxValue) { }
[6475]79    public Job(int index, double dueDate)
80      : base() {
[10476]81      this.dueDate = dueDate;
82      this.index = index;
83      this.tasks = new ItemList<Task>();
[10433]84      RegisterEventHandlers();
[6475]85    }
86
[8887]87    public override IDeepCloneable Clone(Cloner cloner) {
88      return new Job(this, cloner);
89    }
90
[10433]91    [StorableHook(HookType.AfterDeserialization)]
92    private void AfterDeserialization() {
93      RegisterEventHandlers();
94    }
95
96    private void RegisterEventHandlers() {
97      Tasks.ItemsAdded += TasksOnItemsChanged;
[11346]98      Tasks.ItemsRemoved += TasksOnItemsRemoved;
[10433]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
[11346]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
[10433]129    private void TaskOnPropertyChanged(object sender, EventArgs e) {
130      OnTasksChanged();
131    }
132
133    private void TaskOnToStringChanged(object sender, EventArgs e) {
134      OnToStringChanged();
135    }
136
[6475]137    public override string ToString() {
[8886]138      var sb = new StringBuilder();
[6475]139      sb.Append("Job#" + Index + " [ ");
140      foreach (Task t in Tasks) {
[10476]141        sb.Append(t + " ");
[6475]142      }
[7116]143      sb.Append("{" + DueDate + "} ");
[6475]144      sb.Append("]");
145      return sb.ToString();
146    }
147
[10433]148    public event EventHandler TasksChanged;
[13435]149    private void OnTasksChanged() {
[10433]150      var handler = TasksChanged;
151      if (handler != null) handler(this, EventArgs.Empty);
152    }
153
[8886]154    public event PropertyChangedEventHandler PropertyChanged;
[13435]155    private void OnPropertyChanged(string propertyName) {
[8886]156      var handler = PropertyChanged;
157      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
158    }
[6475]159  }
160}
Note: See TracBrowser for help on using the repository browser.