Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6721 was 6721, checked in by ascheibe, 13 years ago

#1233 Review comments: renamed Job to Task

File size: 6.9 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Hive;
27using HeuristicLab.Optimization;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Clients.Hive.Jobs {
31  [Item("Optimizer Task", "Represents Task which executes a IOptimizer object.")]
32  [StorableClass]
33  public class OptimizerJob : ItemJob {
34    public override bool IsParallelizable {
35      get { return this.Item is Experiment || this.Item is BatchRun; }
36    }
37
38    public new IOptimizer Item {
39      get { return (IOptimizer)base.Item; }
40      set { base.Item = value; }
41    }
42
43    [Storable]
44    private int indexInParentOptimizerList = -1;
45    public int IndexInParentOptimizerList {
46      get { return indexInParentOptimizerList; }
47      set { this.indexInParentOptimizerList = value; }
48    }
49
50    public OptimizerJob() : base() { }
51    public OptimizerJob(IOptimizer optimizer)
52      : this() {
53      this.Item = optimizer;
54
55      if (optimizer is Experiment) {
56        this.ComputeInParallel = true;
57      } else if (optimizer is BatchRun) {
58        this.ComputeInParallel = false;
59      } else {
60        this.ComputeInParallel = false;
61      }
62    }
63    [StorableConstructor]
64    protected OptimizerJob(bool deserializing) { }
65    protected OptimizerJob(OptimizerJob original, Cloner cloner)
66      : base(original, cloner) {
67      this.IndexInParentOptimizerList = original.IndexInParentOptimizerList;
68    }
69    public override IDeepCloneable Clone(Cloner cloner) {
70      return new OptimizerJob(this, cloner);
71    }
72
73    /// <summary>
74    /// Casts the Optimizer to an Experiment. Returns null if cast was not successfull.
75    /// </summary>
76    public Experiment OptimizerAsExperiment {
77      get { return Item as Experiment; }
78    }
79
80    /// <summary>
81    /// Casts the Optimizer to an BatchRun. Returns null if cast was not successfull.
82    /// </summary>
83    public BatchRun OptimizerAsBatchRun {
84      get { return Item as BatchRun; }
85    }
86
87    #region IJob Members
88    public override ExecutionState ExecutionState {
89      get { return Item.ExecutionState; }
90    }
91
92    public override TimeSpan ExecutionTime {
93      get { return Item.ExecutionTime; }
94    }
95
96    public override void Prepare() {
97      Item.Prepare();
98    }
99
100    public override void Start() {
101      if ((Item is Experiment && OptimizerAsExperiment.Optimizers.Count == 0) || // experiment would not fire OnStopped if it has 0 optimizers
102          (Item is BatchRun && OptimizerAsBatchRun.Optimizer == null)) { // batchrun would not fire OnStopped if algorithm == null
103        OnJobStopped();
104      } else {
105        Item.Start();
106      }
107    }
108
109    public override void Pause() {
110      Item.Pause();
111    }
112
113    public override void Stop() {
114      Item.Stop();
115    }
116    #endregion
117
118    #region Optimizer Events
119    protected override void RegisterItemEvents() {
120      base.RegisterItemEvents();
121      Item.Stopped += new EventHandler(optimizer_Stopped);
122      Item.Paused += new EventHandler(optimizer_Paused);
123      Item.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
124      Item.DescriptionChanged += new EventHandler(optimizer_DescriptionChanged);
125      Item.NameChanged += new EventHandler(optimizer_NameChanged);
126      Item.NameChanging += new EventHandler<CancelEventArgs<string>>(optimizer_NameChanging);
127      Item.ExecutionStateChanged += new EventHandler(optimizer_ExecutionStateChanged);
128      Item.ExecutionTimeChanged += new EventHandler(optimizer_ExecutionTimeChanged);
129      Item.Started += new EventHandler(optimizer_Started);
130    }
131
132    protected virtual void DeregisterOptimizerEvents() {
133      Item.Stopped -= new EventHandler(optimizer_Stopped);
134      Item.Paused -= new EventHandler(optimizer_Paused);
135      Item.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
136      Item.DescriptionChanged -= new EventHandler(optimizer_DescriptionChanged);
137      Item.NameChanged -= new EventHandler(optimizer_NameChanged);
138      Item.NameChanging -= new EventHandler<CancelEventArgs<string>>(optimizer_NameChanging);
139      Item.ExecutionStateChanged -= new EventHandler(optimizer_ExecutionStateChanged);
140      Item.ExecutionTimeChanged -= new EventHandler(optimizer_ExecutionTimeChanged);
141      Item.Started -= new EventHandler(optimizer_Started);
142      base.DeregisterItemEvents();
143    }
144
145    protected void optimizer_NameChanging(object sender, CancelEventArgs<string> e) {
146      this.OnNameChanging(e.Value, e.Cancel);
147    }
148
149    protected void optimizer_NameChanged(object sender, EventArgs e) {
150      this.OnNameChanged();
151    }
152
153    protected void optimizer_DescriptionChanged(object sender, EventArgs e) {
154      this.OnDescriptionChanged();
155    }
156
157    protected virtual void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) {
158      OnJobFailed(e);
159    }
160
161    protected virtual void optimizer_Started(object sender, EventArgs e) {
162      OnJobStarted();
163    }
164
165    protected virtual void optimizer_Stopped(object sender, EventArgs e) {
166      OnJobStopped();
167    }
168
169    protected virtual void optimizer_Paused(object sender, EventArgs e) {
170      OnJobPaused();
171    }
172
173    protected virtual void optimizer_ExecutionTimeChanged(object sender, EventArgs e) {
174      OnExecutionTimeChanged();
175    }
176
177    protected virtual void optimizer_ExecutionStateChanged(object sender, EventArgs e) {
178      OnExecutionStateChanged();
179    }
180    #endregion
181
182    #region INamedItem Members
183    public override bool CanChangeDescription {
184      get { return Item.CanChangeDescription; }
185    }
186
187    public override bool CanChangeName {
188      get { return Item.CanChangeName; }
189    }
190
191    public override string Description {
192      get { return Item.Description; }
193      set { Item.Description = value; }
194    }
195
196    public override string Name {
197      get { return Item.Name; }
198      set { Item.Name = value; }
199    }
200    #endregion
201  }
202}
Note: See TracBrowser for help on using the repository browser.