Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Clients.Hive/3.3/Tasks/ItemTask.cs @ 15229

Last change on this file since 15229 was 7532, checked in by ascheibe, 13 years ago

#1778

  • tried to clean up ItemTask/EngineTask overrides without breaking anything
  • added a unit test to check if ToString methods of IItems work
  • fixed ToString method of the Hive Plugin dto
File size: 6.2 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    #endregion
98
99    #region ITask Members
100    public abstract ExecutionState ExecutionState { get; }
101
102    public abstract TimeSpan ExecutionTime { get; }
103
104    public abstract void Prepare();
105
106    public abstract void Start();
107
108    public abstract void Pause();
109
110    public abstract void Stop();
111
112    public event EventHandler TaskStarted;
113    protected virtual void OnTaskStarted() {
114      EventHandler handler = TaskStarted;
115      if (handler != null) handler(this, EventArgs.Empty);
116    }
117
118    public event EventHandler TaskStopped;
119    protected virtual void OnTaskStopped() {
120      EventHandler handler = TaskStopped;
121      if (handler != null) handler(this, EventArgs.Empty);
122    }
123
124    public event EventHandler TaskPaused;
125    protected virtual void OnTaskPaused() {
126      EventHandler handler = TaskPaused;
127      if (handler != null) handler(this, EventArgs.Empty);
128    }
129
130    public event EventHandler TaskFailed;
131    protected virtual void OnTaskFailed(EventArgs<Exception> e) {
132      EventHandler handler = TaskFailed;
133      if (handler != null) handler(this, e);
134    }
135
136    public event EventHandler ComputeInParallelChanged;
137    protected virtual void OnComputeInParallelChanged() {
138      EventHandler handler = ComputeInParallelChanged;
139      if (handler != null) handler(this, EventArgs.Empty);
140    }
141
142    public event EventHandler ItemChanged;
143    protected virtual void OnItemChanged() {
144      EventHandler handler = ItemChanged;
145      if (handler != null) handler(this, EventArgs.Empty);
146    }
147    #endregion
148
149    #region INamedItem Members
150    public abstract new bool CanChangeDescription { get; }
151
152    public abstract new bool CanChangeName { get; }
153
154    public abstract new string Description { get; set; }
155
156    public abstract new string Name { get; set; }
157    #endregion
158
159    #region Events
160    public event EventHandler ExecutionTimeChanged;
161    protected virtual void OnExecutionTimeChanged() {
162      EventHandler handler = ExecutionTimeChanged;
163      if (handler != null) handler(this, EventArgs.Empty);
164    }
165    public event EventHandler ExecutionStateChanged;
166    protected virtual void OnExecutionStateChanged() {
167      EventHandler handler = ExecutionStateChanged;
168      if (handler != null) handler(this, EventArgs.Empty);
169    }
170    #endregion
171
172    #region IItem Members
173    public override string ItemDescription {
174      get {
175        if (item == null)
176          return string.Empty;
177        else
178          return item.ItemDescription;
179      }
180    }
181
182    public override Image ItemImage {
183      get {
184        if (item == null)
185          return HeuristicLab.Common.Resources.VSImageLibrary.Class;
186        else
187          return item.ItemImage;
188      }
189    }
190
191    public override string ItemName {
192      get {
193        if (item == null)
194          return string.Empty;
195        else
196          return item.ItemName;
197      }
198    }
199
200    public virtual new Version ItemVersion {
201      get {
202        if (item == null)
203          return ItemAttribute.GetVersion(this.GetType());
204        else
205          return item.ItemVersion;
206      }
207    }
208    #endregion
209
210    public override string ToString() {
211      return Name;
212    }
213  }
214}
Note: See TracBrowser for help on using the repository browser.