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

Last change on this file since 10130 was 10130, checked in by ascheibe, 9 years ago

#2117

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