Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Clients.Hive/3.3/Tasks/ItemTask.cs @ 7403

Last change on this file since 7403 was 7268, checked in by gkronber, 13 years ago

#1081: merged r7214:7266 from trunk into time series branch.

File size: 5.9 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 { return item.ItemDescription; }
177    }
178
179    public virtual new Image ItemImage
180    {
181      get { return item.ItemImage; }
182    }
183
184    public virtual new string ItemName
185    {
186      get { return item.ItemName; }
187    }
188
189    public virtual new Version ItemVersion
190    {
191      get { return item.ItemVersion; }
192    }
193    #endregion
194
195    public override string ToString() {
196      return Name;
197    }
198  }
199}
Note: See TracBrowser for help on using the repository browser.