source: trunk/sources/HeuristicLab.Clients.Hive/3.3/Tasks/ItemTask.cs @ 7530

Last change on this file since 7530 was 7530, checked in by ascheibe, 8 years ago

#1778 check if the item in ItemTask and OptimizerTask is null

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Drawing;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Hive;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Clients.Hive {
30  [Item("Item Task", "Represents a executable hive task which contains a HeuristicLab Item.")]
31  [StorableClass]
32  public abstract class ItemTask : NamedItem, ITask {
33    public virtual bool IsParallelizable {
34      get { return true; }
35    }
36
37    [Storable]
38    protected IItem item;
39    public IItem Item {
40      get { return item; }
41      set {
42        if (value != item) {
43          if (item != null) DeregisterItemEvents();
44          item = value;
45          if (item != null) RegisterItemEvents();
46          OnItemChanged();
47        }
48      }
49    }
50
51    [Storable]
52    protected bool computeInParallel;
53    public bool ComputeInParallel {
54      get { return computeInParallel; }
55      set {
56        if (computeInParallel != value) {
57          computeInParallel = value;
58          OnComputeInParallelChanged();
59        }
60      }
61    }
62
63    #region Constructors and Cloning
64    public ItemTask() { }
65
66    [StorableConstructor]
67    protected ItemTask(bool deserializing) { }
68    protected ItemTask(ItemTask original, Cloner cloner)
69      : base(original, cloner) {
70      this.ComputeInParallel = original.ComputeInParallel;
71      this.Item = cloner.Clone(original.Item);
72    }
73
74    [StorableHook(HookType.AfterDeserialization)]
75    protected virtual void AfterDeserialization() {
76      RegisterItemEvents();
77    }
78    #endregion
79
80    #region Item Events
81    protected virtual void RegisterItemEvents() {
82      item.ItemImageChanged += new EventHandler(item_ItemImageChanged);
83      item.ToStringChanged += new EventHandler(item_ToStringChanged);
84    }
85
86    protected virtual void DeregisterItemEvents() {
87      item.ItemImageChanged -= new EventHandler(item_ItemImageChanged);
88      item.ToStringChanged -= new EventHandler(item_ToStringChanged);
89    }
90
91    protected void item_ToStringChanged(object sender, EventArgs e) {
92      this.OnToStringChanged();
93    }
94    protected void item_ItemImageChanged(object sender, EventArgs e) {
95      this.OnItemImageChanged();
96    }
97
98    #endregion
99
100    #region ITask Members
101
102    public abstract ExecutionState ExecutionState { get; }
103
104    public abstract TimeSpan ExecutionTime { get; }
105
106    public abstract void Prepare();
107
108    public abstract void Start();
109
110    public abstract void Pause();
111
112    public abstract void Stop();
113
114    public event EventHandler TaskStarted;
115    protected virtual void OnTaskStarted() {
116      EventHandler handler = TaskStarted;
117      if (handler != null) handler(this, EventArgs.Empty);
118    }
119
120    public event EventHandler TaskStopped;
121    protected virtual void OnTaskStopped() {
122      EventHandler handler = TaskStopped;
123      if (handler != null) handler(this, EventArgs.Empty);
124    }
125
126    public event EventHandler TaskPaused;
127    protected virtual void OnTaskPaused() {
128      EventHandler handler = TaskPaused;
129      if (handler != null) handler(this, EventArgs.Empty);
130    }
131
132    public event EventHandler TaskFailed;
133    protected virtual void OnTaskFailed(EventArgs<Exception> e) {
134      EventHandler handler = TaskFailed;
135      if (handler != null) handler(this, e);
136    }
137
138    public event EventHandler ComputeInParallelChanged;
139    protected virtual void OnComputeInParallelChanged() {
140      EventHandler handler = ComputeInParallelChanged;
141      if (handler != null) handler(this, EventArgs.Empty);
142    }
143
144    public event EventHandler ItemChanged;
145    protected virtual void OnItemChanged() {
146      EventHandler handler = ItemChanged;
147      if (handler != null) handler(this, EventArgs.Empty);
148    }
149    #endregion
150
151    #region INamedItem Members
152    public abstract new bool CanChangeDescription { get; }
153
154    public abstract new bool CanChangeName { get; }
155
156    public abstract new string Description { get; set; }
157
158    public abstract new string Name { get; set; }
159    #endregion
160
161    #region Events
162    public event EventHandler ExecutionTimeChanged;
163    protected virtual void OnExecutionTimeChanged() {
164      EventHandler handler = ExecutionTimeChanged;
165      if (handler != null) handler(this, EventArgs.Empty);
166    }
167    public event EventHandler ExecutionStateChanged;
168    protected virtual void OnExecutionStateChanged() {
169      EventHandler handler = ExecutionStateChanged;
170      if (handler != null) handler(this, EventArgs.Empty);
171    }
172    #endregion
173
174    #region IItem Members
175    public virtual new string ItemDescription {
176      get {
177        if (item == null)
178          return string.Empty;
179        else
180          return item.ItemDescription;
181      }
182    }
183
184    public virtual new Image ItemImage {
185      get {
186        if (item == null)
187          return HeuristicLab.Common.Resources.VSImageLibrary.Class;
188        else
189          return item.ItemImage;
190      }
191    }
192
193    public virtual new string ItemName {
194      get {
195        if (item == null)
196          return string.Empty;
197        else
198          return item.ItemName;
199      }
200    }
201
202    public virtual new Version ItemVersion {
203      get {
204        if (item == null)
205          return ItemAttribute.GetVersion(this.GetType());
206        else
207          return item.ItemVersion;
208      }
209    }
210    #endregion
211
212    public override string ToString() {
213      return Name;
214    }
215  }
216}
Note: See TracBrowser for help on using the repository browser.