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.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Hive;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Clients.Hive {
|
---|
31 | [Item("Item Job", "Represents a executable hive job which contains a HeuristicLab Item.")]
|
---|
32 | [StorableClass]
|
---|
33 | public abstract class ItemJob : NamedItem, IJob {
|
---|
34 | public virtual bool IsParallelizable {
|
---|
35 | get { return true; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | [Storable]
|
---|
39 | protected IItem item;
|
---|
40 | public IItem Item {
|
---|
41 | get { return item; }
|
---|
42 | set {
|
---|
43 | if (value != item) {
|
---|
44 | if (item != null) DeregisterItemEvents();
|
---|
45 | item = value;
|
---|
46 | if (item != null) RegisterItemEvents();
|
---|
47 | OnItemChanged();
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | [Storable]
|
---|
53 | protected bool computeInParallel;
|
---|
54 | public bool ComputeInParallel {
|
---|
55 | get { return computeInParallel; }
|
---|
56 | set {
|
---|
57 | if (computeInParallel != value) {
|
---|
58 | computeInParallel = value;
|
---|
59 | OnComputeInParallelChanged();
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | #region Constructors and Cloning
|
---|
65 | public ItemJob() { }
|
---|
66 |
|
---|
67 | [StorableConstructor]
|
---|
68 | protected ItemJob(bool deserializing) { }
|
---|
69 | protected ItemJob(ItemJob original, Cloner cloner)
|
---|
70 | : base(original, cloner) {
|
---|
71 | this.ComputeInParallel = original.ComputeInParallel;
|
---|
72 | this.Item = cloner.Clone(original.Item);
|
---|
73 | }
|
---|
74 |
|
---|
75 | [StorableHook(HookType.AfterDeserialization)]
|
---|
76 | protected virtual void AfterDeserialization() {
|
---|
77 | RegisterItemEvents();
|
---|
78 | }
|
---|
79 | #endregion
|
---|
80 |
|
---|
81 | #region Item Events
|
---|
82 | protected virtual void RegisterItemEvents() {
|
---|
83 | item.ItemImageChanged += new EventHandler(item_ItemImageChanged);
|
---|
84 | item.ToStringChanged += new EventHandler(item_ToStringChanged);
|
---|
85 | }
|
---|
86 |
|
---|
87 | protected virtual void DeregisterItemEvents() {
|
---|
88 | item.ItemImageChanged -= new EventHandler(item_ItemImageChanged);
|
---|
89 | item.ToStringChanged -= new EventHandler(item_ToStringChanged);
|
---|
90 | }
|
---|
91 |
|
---|
92 | protected void item_ToStringChanged(object sender, EventArgs e) {
|
---|
93 | this.OnToStringChanged();
|
---|
94 | }
|
---|
95 | protected void item_ItemImageChanged(object sender, EventArgs e) {
|
---|
96 | this.OnItemImageChanged();
|
---|
97 | }
|
---|
98 |
|
---|
99 | #endregion
|
---|
100 |
|
---|
101 | #region IJob Members
|
---|
102 |
|
---|
103 | public abstract ExecutionState ExecutionState { get; }
|
---|
104 |
|
---|
105 | public abstract TimeSpan ExecutionTime { get; }
|
---|
106 |
|
---|
107 | public abstract void Prepare();
|
---|
108 |
|
---|
109 | public abstract void Start();
|
---|
110 |
|
---|
111 | public abstract void Pause();
|
---|
112 |
|
---|
113 | public abstract void Stop();
|
---|
114 |
|
---|
115 | public abstract void Resume(IEnumerable<IJob> childJobs);
|
---|
116 |
|
---|
117 | public event EventHandler JobStarted;
|
---|
118 | protected virtual void OnJobStarted() {
|
---|
119 | EventHandler handler = JobStarted;
|
---|
120 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
121 | }
|
---|
122 |
|
---|
123 | public event EventHandler JobStopped;
|
---|
124 | protected virtual void OnJobStopped() {
|
---|
125 | EventHandler handler = JobStopped;
|
---|
126 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
127 | }
|
---|
128 |
|
---|
129 | public event EventHandler JobPaused;
|
---|
130 | protected virtual void OnJobPaused() {
|
---|
131 | EventHandler handler = JobPaused;
|
---|
132 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
133 | }
|
---|
134 |
|
---|
135 | public event EventHandler JobFailed;
|
---|
136 | protected virtual void OnJobFailed(EventArgs<Exception> e) {
|
---|
137 | EventHandler handler = JobFailed;
|
---|
138 | if (handler != null) handler(this, e);
|
---|
139 | }
|
---|
140 |
|
---|
141 | public event EventHandler<EventArgs<IJob>> NewChildJob;
|
---|
142 | protected virtual void OnNewChildJob(IJob job) {
|
---|
143 | EventHandler<EventArgs<IJob>> handler = NewChildJob;
|
---|
144 | if (handler != null) handler(this, new EventArgs<IJob>(job));
|
---|
145 | }
|
---|
146 |
|
---|
147 | public event EventHandler WaitForChildJobs;
|
---|
148 | protected virtual void OnWaitForChildJobs() {
|
---|
149 | EventHandler handler = WaitForChildJobs;
|
---|
150 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
151 | }
|
---|
152 |
|
---|
153 | public event EventHandler DeleteChildJobs;
|
---|
154 | protected virtual void OnDeleteChildJobs() {
|
---|
155 | EventHandler handler = DeleteChildJobs;
|
---|
156 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
157 | }
|
---|
158 |
|
---|
159 | public event EventHandler ComputeInParallelChanged;
|
---|
160 | protected virtual void OnComputeInParallelChanged() {
|
---|
161 | EventHandler handler = ComputeInParallelChanged;
|
---|
162 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
163 | }
|
---|
164 |
|
---|
165 | public event EventHandler ItemChanged;
|
---|
166 | protected virtual void OnItemChanged() {
|
---|
167 | EventHandler handler = ItemChanged;
|
---|
168 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
169 | }
|
---|
170 | #endregion
|
---|
171 |
|
---|
172 | #region INamedItem Members
|
---|
173 | public abstract bool CanChangeDescription { get; }
|
---|
174 |
|
---|
175 | public abstract bool CanChangeName { get; }
|
---|
176 |
|
---|
177 | public abstract string Description { get; set; }
|
---|
178 |
|
---|
179 | public abstract string Name { get; set; }
|
---|
180 | #endregion
|
---|
181 |
|
---|
182 | #region Events
|
---|
183 | public event EventHandler DescriptionChanged;
|
---|
184 | protected virtual void OnDescriptionChanged() {
|
---|
185 | var handler = DescriptionChanged;
|
---|
186 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
187 | }
|
---|
188 | public event EventHandler ItemImageChanged;
|
---|
189 | protected virtual void OnItemImageChanged() {
|
---|
190 | var handler = ItemImageChanged;
|
---|
191 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
192 | }
|
---|
193 | public event EventHandler ToStringChanged;
|
---|
194 | protected virtual void OnToStringChanged() {
|
---|
195 | var handler = ToStringChanged;
|
---|
196 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
197 | }
|
---|
198 | public event EventHandler NameChanged;
|
---|
199 | protected virtual void OnNameChanged() {
|
---|
200 | var handler = NameChanged;
|
---|
201 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
202 | }
|
---|
203 | public event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
204 | protected virtual void OnNameChanging(string value, bool cancel) {
|
---|
205 | var handler = NameChanging;
|
---|
206 | if (handler != null) handler(this, new CancelEventArgs<string>(value, cancel));
|
---|
207 | }
|
---|
208 | public event EventHandler ExecutionTimeChanged;
|
---|
209 | protected virtual void OnExecutionTimeChanged() {
|
---|
210 | EventHandler handler = ExecutionTimeChanged;
|
---|
211 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
212 | }
|
---|
213 | public event EventHandler ExecutionStateChanged;
|
---|
214 | protected virtual void OnExecutionStateChanged() {
|
---|
215 | EventHandler handler = ExecutionStateChanged;
|
---|
216 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
217 | }
|
---|
218 | #endregion
|
---|
219 |
|
---|
220 | #region IItem Members
|
---|
221 | public virtual string ItemDescription {
|
---|
222 | get { return item.ItemDescription; }
|
---|
223 | }
|
---|
224 |
|
---|
225 | public virtual Image ItemImage {
|
---|
226 | get { return item.ItemImage; }
|
---|
227 | }
|
---|
228 |
|
---|
229 | public virtual string ItemName {
|
---|
230 | get { return item.ItemName; }
|
---|
231 | }
|
---|
232 |
|
---|
233 | public virtual Version ItemVersion {
|
---|
234 | get { return item.ItemVersion; }
|
---|
235 | }
|
---|
236 | #endregion
|
---|
237 |
|
---|
238 | public override string ToString() {
|
---|
239 | return Name;
|
---|
240 | }
|
---|
241 | }
|
---|
242 | }
|
---|