1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System;
|
---|
23 | using System.Drawing;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Hive;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace 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 bool CanChangeDescription { get; }
|
---|
153 |
|
---|
154 | public abstract bool CanChangeName { get; }
|
---|
155 |
|
---|
156 | public abstract string Description { get; set; }
|
---|
157 |
|
---|
158 | public abstract string Name { get; set; }
|
---|
159 | #endregion
|
---|
160 |
|
---|
161 | #region Events
|
---|
162 | public event EventHandler DescriptionChanged;
|
---|
163 | protected virtual void OnDescriptionChanged() {
|
---|
164 | var handler = DescriptionChanged;
|
---|
165 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
166 | }
|
---|
167 | public event EventHandler ItemImageChanged;
|
---|
168 | protected virtual void OnItemImageChanged() {
|
---|
169 | var handler = ItemImageChanged;
|
---|
170 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
171 | }
|
---|
172 | public event EventHandler ToStringChanged;
|
---|
173 | protected virtual void OnToStringChanged() {
|
---|
174 | var handler = ToStringChanged;
|
---|
175 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
176 | }
|
---|
177 | public event EventHandler NameChanged;
|
---|
178 | protected virtual void OnNameChanged() {
|
---|
179 | var handler = NameChanged;
|
---|
180 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
181 | }
|
---|
182 | public event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
183 | protected virtual void OnNameChanging(string value, bool cancel) {
|
---|
184 | var handler = NameChanging;
|
---|
185 | if (handler != null) handler(this, new CancelEventArgs<string>(value, cancel));
|
---|
186 | }
|
---|
187 | public event EventHandler ExecutionTimeChanged;
|
---|
188 | protected virtual void OnExecutionTimeChanged() {
|
---|
189 | EventHandler handler = ExecutionTimeChanged;
|
---|
190 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
191 | }
|
---|
192 | public event EventHandler ExecutionStateChanged;
|
---|
193 | protected virtual void OnExecutionStateChanged() {
|
---|
194 | EventHandler handler = ExecutionStateChanged;
|
---|
195 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
196 | }
|
---|
197 | #endregion
|
---|
198 |
|
---|
199 | #region IItem Members
|
---|
200 | public virtual string ItemDescription {
|
---|
201 | get { return item.ItemDescription; }
|
---|
202 | }
|
---|
203 |
|
---|
204 | public virtual Image ItemImage {
|
---|
205 | get { return item.ItemImage; }
|
---|
206 | }
|
---|
207 |
|
---|
208 | public virtual string ItemName {
|
---|
209 | get { return item.ItemName; }
|
---|
210 | }
|
---|
211 |
|
---|
212 | public virtual Version ItemVersion {
|
---|
213 | get { return item.ItemVersion; }
|
---|
214 | }
|
---|
215 | #endregion
|
---|
216 |
|
---|
217 | public override string ToString() {
|
---|
218 | return Name;
|
---|
219 | }
|
---|
220 | }
|
---|
221 | }
|
---|