Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Breadcrumbs/HeuristicLab.Clients.Hive/3.3/Tasks/ItemTask.cs @ 11594

Last change on this file since 11594 was 11594, checked in by jkarder, 9 years ago

#2116: merged r10041-r11593 from trunk into branch

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