source: trunk/sources/HeuristicLab.Clients.Hive/3.3/Tasks/OptimizerTask.cs @ 10150

Last change on this file since 10150 was 10150, checked in by ascheibe, 9 years ago

#2117

  • improved creation of ItemTasks from Items and moved common functionality to ItemTask
  • added a MenuItem for directly uploading tasks (e.g. from experiments, batchruns,...)
  • added a MenuItem for creating RefreshableHiveJobs
File size: 7.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Optimization;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Clients.Hive.Jobs {
29  [Item("Optimizer Task", "Represents Task which executes a IOptimizer object.")]
30  [StorableClass]
31  public class OptimizerTask : ItemTask {
32    public override HiveTask CreateHiveTask() {
33      return new OptimizerHiveTask(this);
34    }
35
36    public override bool IsParallelizable {
37      get { return this.Item is Experiment || this.Item is BatchRun; }
38    }
39
40    public new IOptimizer Item {
41      get { return (IOptimizer)base.Item; }
42      set { base.Item = value; }
43    }
44
45    [Storable]
46    private int indexInParentOptimizerList = -1;
47    public int IndexInParentOptimizerList {
48      get { return indexInParentOptimizerList; }
49      set { this.indexInParentOptimizerList = value; }
50    }
51
52    public OptimizerTask(IOptimizer optimizer)
53      : base(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 OptimizerTask(bool deserializing) { }
65    protected OptimizerTask(OptimizerTask original, Cloner cloner)
66      : base(original, cloner) {
67      this.IndexInParentOptimizerList = original.IndexInParentOptimizerList;
68    }
69    public override IDeepCloneable Clone(Cloner cloner) {
70      return new OptimizerTask(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 ITask 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        OnTaskStopped();
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      OnNameChanging(new CancelEventArgs<string>(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      OnTaskFailed(e);
159    }
160
161    protected virtual void optimizer_Started(object sender, EventArgs e) {
162      OnTaskStarted();
163    }
164
165    protected virtual void optimizer_Stopped(object sender, EventArgs e) {
166      OnTaskStopped();
167    }
168
169    protected virtual void optimizer_Paused(object sender, EventArgs e) {
170      OnTaskPaused();
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 {
185        if (Item == null)
186          return false;
187        else
188          return Item.CanChangeDescription;
189      }
190    }
191
192    public override bool CanChangeName {
193      get {
194        if (Item == null)
195          return false;
196        else
197          return Item.CanChangeName;
198      }
199    }
200
201    public override string Description {
202      get {
203        if (Item == null)
204          return string.Empty;
205        else
206          return Item.Description;
207      }
208      set { Item.Description = value; }
209    }
210
211    public override string Name {
212      get {
213        if (Item == null)
214          return "Optimizer Task";
215        else
216          return Item.Name;
217      }
218      set { Item.Name = value; }
219    }
220    #endregion
221  }
222}
Note: See TracBrowser for help on using the repository browser.