Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/Jobs/OptimizerJob.cs @ 5062

Last change on this file since 5062 was 5062, checked in by cneumuel, 13 years ago

#1233

  • implemented MockService, MockJob, MockServiceLocator
File size: 11.3 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Optimization;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Hive;
29
30namespace HeuristicLab.Clients.Hive.Jobs {
31  [Item("Optimizer Job", "Represents Job which executes a IOptimizer object.")]
32  [StorableClass]
33  public class OptimizerJob : DeepCloneable, IJob {
34    public virtual bool IsParallelizable {
35      get { return this.Optimizer is Optimization.Experiment || this.Optimizer is BatchRun; }
36    }
37
38    [Storable]
39    protected IOptimizer optimizer;
40    public IOptimizer Optimizer {
41      get { return optimizer; }
42      set {
43        if (value != optimizer) {
44          if (optimizer != null) DeregisterEvents();
45          optimizer = value;
46          if (optimizer != null) RegisterEvents();
47          OnOptimizerChanged();
48        }
49      }
50    }
51
52    [Storable]
53    protected ILog log;
54    public ILog Log {
55      get { return log; }
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    [Storable]
71    private int indexInParentOptimizerList = -1;
72    public int IndexInParentOptimizerList {
73      get { return indexInParentOptimizerList; }
74      set { this.indexInParentOptimizerList = value; }
75    }
76
77    [Storable]
78    private bool collectChildJobs;
79    public bool CollectChildJobs {
80      get { return collectChildJobs; }
81      set { collectChildJobs = value; }
82    }
83
84    public OptimizerJob() {
85      this.log = new Log();
86    }
87    public OptimizerJob(IOptimizer optimizer) : this() {
88      this.Optimizer = optimizer;
89
90      if (optimizer is Optimization.Experiment) {
91        this.ComputeInParallel = true;
92      } else if (optimizer is Optimization.BatchRun) {
93        this.ComputeInParallel = false;
94      } else {
95        this.ComputeInParallel = false;
96      }
97    }
98    [StorableConstructor]
99    protected OptimizerJob(bool deserializing) { }
100    protected OptimizerJob(OptimizerJob original, Cloner cloner) : base(original, cloner) {
101      this.Optimizer = cloner.Clone(original.Optimizer);
102      this.log = cloner.Clone(original.Log);
103      this.ComputeInParallel = original.ComputeInParallel;
104      this.IndexInParentOptimizerList = original.IndexInParentOptimizerList;
105      this.CollectChildJobs = original.CollectChildJobs;
106      this.RegisterEvents();
107    }
108    public override IDeepCloneable Clone(Cloner cloner) {
109      return new OptimizerJob(this, cloner);
110    }
111
112    [StorableHook(HookType.AfterDeserialization)]
113    protected virtual void AfterDeserialization() {
114      RegisterEvents();
115    }
116
117    /// <summary>
118    /// Casts the Optimizer to an Experiment. Returns null if cast was not successfull.
119    /// </summary>
120    public Optimization.Experiment OptimizerAsExperiment {
121      get { return Optimizer as Optimization.Experiment; }
122    }
123
124    /// <summary>
125    /// Casts the Optimizer to an BatchRun. Returns null if cast was not successfull.
126    /// </summary>
127    public Optimization.BatchRun OptimizerAsBatchRun {
128      get { return Optimizer as Optimization.BatchRun; }
129    }
130
131    #region IJob Members
132
133    public virtual ExecutionState ExecutionState {
134      get { return optimizer.ExecutionState; }
135    }
136
137    public TimeSpan ExecutionTime {
138      get { return optimizer.ExecutionTime; }
139    }
140
141    public virtual void Prepare() {
142      optimizer.Prepare();
143    }
144
145    public virtual void Start() {
146      if ((optimizer is Optimization.Experiment && OptimizerAsExperiment.Optimizers.Count == 0) || // experiment would not fire OnStopped if it has 0 optimizers
147          (optimizer is Optimization.BatchRun && OptimizerAsBatchRun.Algorithm == null)) { // batchrun would not fire OnStopped if algorithm == null
148        OnJobStopped();
149      } else {
150        optimizer.Start();
151      }
152    }
153
154    public void Pause() {
155      throw new NotImplementedException();
156    }
157
158    public virtual void Stop() {
159      optimizer.Stop();
160    }
161
162    public virtual void Resume(IEnumerable<IJob> childJobs) {
163      OnJobStopped();
164    }
165
166    public event EventHandler JobStopped;
167    protected virtual void OnJobStopped() {
168      EventHandler handler = JobStopped;
169      if (handler != null) handler(this, EventArgs.Empty);
170    }
171
172    public event EventHandler JobFailed;
173    protected virtual void OnJobFailed(EventArgs<Exception> e) {
174      EventHandler handler = JobFailed;
175      if (handler != null) handler(this, e);
176    }
177
178    public event EventHandler<EventArgs<IJob>> NewChildJob;
179    protected virtual void OnNewChildJob(IJob job) {
180      EventHandler<EventArgs<IJob>> handler = NewChildJob;
181      if (handler != null) handler(this, new EventArgs<IJob>(job));
182    }
183
184    public event EventHandler WaitForChildJobs;
185    protected virtual void OnWaitForChildJobs() {
186      EventHandler handler = WaitForChildJobs;
187      if (handler != null) handler(this, EventArgs.Empty);
188    }
189
190    public event EventHandler DeleteChildJobs;
191    protected virtual void OnDeleteChildJobs() {
192      EventHandler handler = DeleteChildJobs;
193      if (handler != null) handler(this, EventArgs.Empty);
194    }
195
196    public event EventHandler ComputeInParallelChanged;
197    protected virtual void OnComputeInParallelChanged() {
198      EventHandler handler = ComputeInParallelChanged;
199      if (handler != null) handler(this, EventArgs.Empty);
200    }
201
202    public event EventHandler OptimizerChanged;
203    protected virtual void OnOptimizerChanged() {
204      EventHandler handler = OptimizerChanged;
205      if (handler != null) handler(this, EventArgs.Empty);
206    }
207    #endregion
208
209    #region Optimizer Events
210    protected virtual void RegisterEvents() {
211      optimizer.Stopped += new EventHandler(optimizer_Stopped);
212      optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
213      optimizer.DescriptionChanged += new EventHandler(optimizer_DescriptionChanged);
214      optimizer.ItemImageChanged += new EventHandler(optimizer_ItemImageChanged);
215      optimizer.NameChanged += new EventHandler(optimizer_NameChanged);
216      optimizer.NameChanging += new EventHandler<CancelEventArgs<string>>(optimizer_NameChanging);
217      optimizer.ToStringChanged += new EventHandler(optimizer_ToStringChanged);
218    }
219    protected virtual void DeregisterEvents() {
220      optimizer.Stopped -= new EventHandler(optimizer_Stopped);
221      optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
222      optimizer.DescriptionChanged -= this.DescriptionChanged;
223      optimizer.ItemImageChanged -= this.ItemImageChanged;
224      optimizer.NameChanged -= this.NameChanged;
225      optimizer.NameChanging -= this.NameChanging;
226      optimizer.ToStringChanged -= this.ToStringChanged;
227    }
228
229    void optimizer_ToStringChanged(object sender, EventArgs e) {
230      this.OnToStringChanged();
231    }
232
233    void optimizer_NameChanging(object sender, CancelEventArgs<string> e) {
234      this.OnNameChanging(e.Value, e.Cancel);
235    }
236
237    void optimizer_NameChanged(object sender, EventArgs e) {
238      this.OnNameChanged();
239    }
240
241    void optimizer_ItemImageChanged(object sender, EventArgs e) {
242      this.OnItemImageChanged();
243    }
244
245    void optimizer_DescriptionChanged(object sender, EventArgs e) {
246      this.OnDescriptionChanged();
247    }
248
249    protected virtual void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) {
250      OnJobFailed(e);
251    }
252
253    protected virtual void optimizer_Stopped(object sender, EventArgs e) {
254      OnJobStopped();
255    }
256    #endregion
257
258    #region INamedItem Members
259
260    public bool CanChangeDescription {
261      get { return optimizer.CanChangeDescription; }
262    }
263
264    public bool CanChangeName {
265      get { return optimizer.CanChangeName; }
266    }
267
268    public string Description {
269      get { return optimizer.Description; }
270      set { optimizer.Description = value; }
271    }
272
273    public string Name {
274      get { return optimizer.Name; }
275      set { optimizer.Name = value; }
276    }
277    #endregion
278
279    #region Events
280    public event EventHandler DescriptionChanged;
281    protected virtual void OnDescriptionChanged() {
282      var handler = DescriptionChanged;
283      if(handler != null) handler(this, EventArgs.Empty);
284    }
285    public event EventHandler ItemImageChanged;
286    protected virtual void OnItemImageChanged() {
287      var handler = ItemImageChanged;
288      if (handler != null) handler(this, EventArgs.Empty);
289    }
290    public event EventHandler ToStringChanged;
291    protected virtual void OnToStringChanged() {
292      var handler = ToStringChanged;
293      if (handler != null) handler(this, EventArgs.Empty);
294    }
295    public event EventHandler NameChanged;
296    protected virtual void OnNameChanged() {
297      var handler = NameChanged;
298      if (handler != null) handler(this, EventArgs.Empty);
299    }
300    public event EventHandler<CancelEventArgs<string>> NameChanging;
301    protected virtual void OnNameChanging(string value, bool cancel) {
302      var handler = NameChanging;
303      if (handler != null) handler(this, new CancelEventArgs<string>(value, cancel));
304    }
305    public event EventHandler ExecutionTimeChanged;
306    protected virtual void OnExecutionTimeChanged() {
307      EventHandler handler = ExecutionTimeChanged;
308      if (handler != null) handler(this, EventArgs.Empty);
309    }
310    public event EventHandler ExecutionStateChanged;
311    protected virtual void OnExecutionStateChanged() {
312      EventHandler handler = ExecutionStateChanged;
313      if (handler != null) handler(this, EventArgs.Empty);
314    }
315    #endregion
316
317    #region IItem Members
318
319    public string ItemDescription {
320      get { return optimizer.ItemDescription; }
321    }
322
323    public System.Drawing.Image ItemImage {
324      get { return optimizer.ItemImage; }
325    }
326
327    public string ItemName {
328      get { return optimizer.ItemName; }
329    }
330
331    public Version ItemVersion {
332      get { return optimizer.ItemVersion; }
333    }
334
335    #endregion
336
337    /// <summary>
338    /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
339    /// </summary>
340    /// <returns>The current instance as a string.</returns>
341    public override string ToString() {
342      return Name;
343    }
344  }
345}
Note: See TracBrowser for help on using the repository browser.