1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Hive {
|
---|
30 | [Item("Abstract Job", "Represents a executable hive job.")]
|
---|
31 | [StorableClass]
|
---|
32 | public abstract class ItemJob : DeepCloneable, IJob {
|
---|
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 | [Storable]
|
---|
64 | private bool collectChildJobs;
|
---|
65 | public bool CollectChildJobs {
|
---|
66 | get { return collectChildJobs; }
|
---|
67 | set { collectChildJobs = value; }
|
---|
68 | }
|
---|
69 |
|
---|
70 | #region Constructors and Cloning
|
---|
71 | public ItemJob() { }
|
---|
72 |
|
---|
73 | [StorableConstructor]
|
---|
74 | protected ItemJob(bool deserializing) { }
|
---|
75 | protected ItemJob(ItemJob original, Cloner cloner)
|
---|
76 | : base(original, cloner) {
|
---|
77 | this.ComputeInParallel = original.ComputeInParallel;
|
---|
78 | this.CollectChildJobs = original.CollectChildJobs;
|
---|
79 | this.Item = cloner.Clone(original.Item);
|
---|
80 | }
|
---|
81 |
|
---|
82 | [StorableHook(HookType.AfterDeserialization)]
|
---|
83 | protected virtual void AfterDeserialization() {
|
---|
84 | RegisterItemEvents();
|
---|
85 | }
|
---|
86 | #endregion
|
---|
87 |
|
---|
88 | #region Optimizer Events
|
---|
89 | protected virtual void RegisterItemEvents() {
|
---|
90 | item.ItemImageChanged += new EventHandler(item_ItemImageChanged);
|
---|
91 | item.ToStringChanged += new EventHandler(item_ToStringChanged);
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected virtual void DeregisterItemEvents() {
|
---|
95 | item.ItemImageChanged -= new EventHandler(item_ItemImageChanged);
|
---|
96 | item.ToStringChanged -= new EventHandler(item_ToStringChanged);
|
---|
97 | }
|
---|
98 |
|
---|
99 | protected void item_ToStringChanged(object sender, EventArgs e) {
|
---|
100 | this.OnToStringChanged();
|
---|
101 | }
|
---|
102 | protected void item_ItemImageChanged(object sender, EventArgs e) {
|
---|
103 | this.OnItemImageChanged();
|
---|
104 | }
|
---|
105 |
|
---|
106 | #endregion
|
---|
107 |
|
---|
108 | #region IJob Members
|
---|
109 |
|
---|
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 abstract void Resume(IEnumerable<IJob> childJobs);
|
---|
123 |
|
---|
124 | public event EventHandler JobStarted;
|
---|
125 | protected virtual void OnJobStarted() {
|
---|
126 | EventHandler handler = JobStarted;
|
---|
127 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
128 | }
|
---|
129 |
|
---|
130 | public event EventHandler JobStopped;
|
---|
131 | protected virtual void OnJobStopped() {
|
---|
132 | EventHandler handler = JobStopped;
|
---|
133 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public event EventHandler JobPaused;
|
---|
137 | protected virtual void OnJobPaused() {
|
---|
138 | EventHandler handler = JobPaused;
|
---|
139 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
140 | }
|
---|
141 |
|
---|
142 | public event EventHandler JobFailed;
|
---|
143 | protected virtual void OnJobFailed(EventArgs<Exception> e) {
|
---|
144 | EventHandler handler = JobFailed;
|
---|
145 | if (handler != null) handler(this, e);
|
---|
146 | }
|
---|
147 |
|
---|
148 | public event EventHandler<EventArgs<IJob>> NewChildJob;
|
---|
149 | protected virtual void OnNewChildJob(IJob job) {
|
---|
150 | EventHandler<EventArgs<IJob>> handler = NewChildJob;
|
---|
151 | if (handler != null) handler(this, new EventArgs<IJob>(job));
|
---|
152 | }
|
---|
153 |
|
---|
154 | public event EventHandler WaitForChildJobs;
|
---|
155 | protected virtual void OnWaitForChildJobs() {
|
---|
156 | EventHandler handler = WaitForChildJobs;
|
---|
157 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
158 | }
|
---|
159 |
|
---|
160 | public event EventHandler DeleteChildJobs;
|
---|
161 | protected virtual void OnDeleteChildJobs() {
|
---|
162 | EventHandler handler = DeleteChildJobs;
|
---|
163 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
164 | }
|
---|
165 |
|
---|
166 | public event EventHandler ComputeInParallelChanged;
|
---|
167 | protected virtual void OnComputeInParallelChanged() {
|
---|
168 | EventHandler handler = ComputeInParallelChanged;
|
---|
169 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
170 | }
|
---|
171 |
|
---|
172 | public event EventHandler ItemChanged;
|
---|
173 | protected virtual void OnItemChanged() {
|
---|
174 | EventHandler handler = ItemChanged;
|
---|
175 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
176 | }
|
---|
177 | #endregion
|
---|
178 |
|
---|
179 | #region INamedItem Members
|
---|
180 | public abstract bool CanChangeDescription { get; }
|
---|
181 |
|
---|
182 | public abstract bool CanChangeName { get; }
|
---|
183 |
|
---|
184 | public abstract string Description { get; set; }
|
---|
185 |
|
---|
186 | public abstract string Name { get; set; }
|
---|
187 | #endregion
|
---|
188 |
|
---|
189 | #region Events
|
---|
190 | public event EventHandler DescriptionChanged;
|
---|
191 | protected virtual void OnDescriptionChanged() {
|
---|
192 | var handler = DescriptionChanged;
|
---|
193 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
194 | }
|
---|
195 | public event EventHandler ItemImageChanged;
|
---|
196 | protected virtual void OnItemImageChanged() {
|
---|
197 | var handler = ItemImageChanged;
|
---|
198 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
199 | }
|
---|
200 | public event EventHandler ToStringChanged;
|
---|
201 | protected virtual void OnToStringChanged() {
|
---|
202 | var handler = ToStringChanged;
|
---|
203 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
204 | }
|
---|
205 | public event EventHandler NameChanged;
|
---|
206 | protected virtual void OnNameChanged() {
|
---|
207 | var handler = NameChanged;
|
---|
208 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
209 | }
|
---|
210 | public event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
211 | protected virtual void OnNameChanging(string value, bool cancel) {
|
---|
212 | var handler = NameChanging;
|
---|
213 | if (handler != null) handler(this, new CancelEventArgs<string>(value, cancel));
|
---|
214 | }
|
---|
215 | public event EventHandler ExecutionTimeChanged;
|
---|
216 | protected virtual void OnExecutionTimeChanged() {
|
---|
217 | EventHandler handler = ExecutionTimeChanged;
|
---|
218 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
219 | }
|
---|
220 | public event EventHandler ExecutionStateChanged;
|
---|
221 | protected virtual void OnExecutionStateChanged() {
|
---|
222 | EventHandler handler = ExecutionStateChanged;
|
---|
223 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
224 | }
|
---|
225 | #endregion
|
---|
226 |
|
---|
227 | #region IItem Members
|
---|
228 | public virtual string ItemDescription {
|
---|
229 | get { return item.ItemDescription; }
|
---|
230 | }
|
---|
231 |
|
---|
232 | public virtual Image ItemImage {
|
---|
233 | get { return item.ItemImage; }
|
---|
234 | }
|
---|
235 |
|
---|
236 | public virtual string ItemName {
|
---|
237 | get { return item.ItemName; }
|
---|
238 | }
|
---|
239 |
|
---|
240 | public virtual Version ItemVersion {
|
---|
241 | get { return item.ItemVersion; }
|
---|
242 | }
|
---|
243 | #endregion
|
---|
244 |
|
---|
245 | public override string ToString() {
|
---|
246 | return Name;
|
---|
247 | }
|
---|
248 | }
|
---|
249 | }
|
---|