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

Last change on this file since 6033 was 6033, checked in by cneumuel, 9 years ago

#1233

  • created baseclass for jobs (ItemJob) which derives OperatorJobs and EngineJobs
  • created special view for OptimizerJobs which derives from a more general view
  • removed logic from domain class HiveExperiment and moved it into RefreshableHiveExperiment
  • improved ItemTreeView
  • corrected plugin dependencies
  • fixed bug in database trigger when deleting HiveExperiments
  • added delete cascade for Plugin and PluginData
  • lots of fixes
File size: 6.2 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.Hive;
27using HeuristicLab.Optimization;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Clients.Hive.Jobs {
31  [Item("Optimizer Job", "Represents Job 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      this.CollectChildJobs = original.CollectChildJobs;
69    }
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new OptimizerJob(this, cloner);
72    }
73
74    /// <summary>
75    /// Casts the Optimizer to an Experiment. Returns null if cast was not successfull.
76    /// </summary>
77    public Experiment OptimizerAsExperiment {
78      get { return Item as Experiment; }
79    }
80
81    /// <summary>
82    /// Casts the Optimizer to an BatchRun. Returns null if cast was not successfull.
83    /// </summary>
84    public BatchRun OptimizerAsBatchRun {
85      get { return Item as BatchRun; }
86    }
87
88    #region IJob Members
89
90    public override ExecutionState ExecutionState {
91      get { return Item.ExecutionState; }
92    }
93
94    public override TimeSpan ExecutionTime {
95      get { return Item.ExecutionTime; }
96    }
97
98    public override void Prepare() {
99      Item.Prepare();
100    }
101
102    public override void Start() {
103      if ((Item is Experiment && OptimizerAsExperiment.Optimizers.Count == 0) || // experiment would not fire OnStopped if it has 0 optimizers
104          (Item is BatchRun && OptimizerAsBatchRun.Optimizer == null)) { // batchrun would not fire OnStopped if algorithm == null
105        OnJobStopped();
106      } else {
107        Item.Start();
108      }
109    }
110
111    public override void Pause() {
112      Item.Pause();
113    }
114
115    public override void Stop() {
116      Item.Stop();
117    }
118
119    public override void Resume(IEnumerable<IJob> childJobs) {
120      OnJobStopped();
121    }
122    #endregion
123
124    #region Optimizer Events
125    protected override void RegisterItemEvents() {
126      base.RegisterItemEvents();
127      Item.Stopped += new EventHandler(optimizer_Stopped);
128      Item.Paused += new EventHandler(optimizer_Paused);
129      Item.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
130      Item.DescriptionChanged += new EventHandler(optimizer_DescriptionChanged);
131      Item.NameChanged += new EventHandler(optimizer_NameChanged);
132      Item.NameChanging += new EventHandler<CancelEventArgs<string>>(optimizer_NameChanging);
133    }
134
135    protected virtual void DeregisterOptimizerEvents() {
136      Item.Stopped -= new EventHandler(optimizer_Stopped);
137      Item.Paused -= new EventHandler(optimizer_Paused);
138      Item.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
139      Item.DescriptionChanged -= new EventHandler(optimizer_DescriptionChanged);
140      Item.NameChanged -= new EventHandler(optimizer_NameChanged);
141      Item.NameChanging -= new EventHandler<CancelEventArgs<string>>(optimizer_NameChanging);
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_Stopped(object sender, EventArgs e) {
162      OnJobStopped();
163    }
164
165    protected virtual void optimizer_Paused(object sender, EventArgs e) {
166      OnJobPaused();
167    }
168    #endregion
169
170    #region INamedItem Members
171    public override bool CanChangeDescription {
172      get { return Item.CanChangeDescription; }
173    }
174
175    public override bool CanChangeName {
176      get { return Item.CanChangeName; }
177    }
178
179    public override string Description {
180      get { return Item.Description; }
181      set { Item.Description = value; }
182    }
183
184    public override string Name {
185      get { return Item.Name; }
186      set { Item.Name = value; }
187    }
188    #endregion
189  }
190}
Note: See TracBrowser for help on using the repository browser.