Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment/3.3/OptimizerJob.cs @ 4144

Last change on this file since 4144 was 4120, checked in by cneumuel, 14 years ago

further improvement and stabilisation of HiveExperiment (#1115)

File size: 2.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Hive.JobBase;
6using HeuristicLab.Optimization;
7using HeuristicLab.Core;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9
10namespace HeuristicLab.Hive.Experiment {
11  [StorableClass]
12  public class OptimizerJob : IJob {
13
14    [Storable]
15    private IOptimizer optimizer;
16    public IOptimizer Optimizer {
17      get { return optimizer;  }
18      set {
19        if (optimizer != null && value != optimizer) {
20          DeregisterEvents();
21        }
22        optimizer = value;
23        if (optimizer != null) {
24          RegisterEvents();
25        }
26      }
27    }
28
29    [Storable]
30    private double progress = 0.0;
31
32    public OptimizerJob() {
33    }
34
35    [StorableHook(HookType.AfterDeserialization)]
36    private void AfterDeserialization() {
37      RegisterEvents();
38    }
39
40    #region IJob Members
41
42    public long JobId {
43      get;
44      set;
45    }
46
47    public double Progress {
48      get { return optimizer.ExecutionState == ExecutionState.Stopped ? 1.0 : this.progress; }
49    }
50
51    public ExecutionState ExecutionState {
52      get { return optimizer.ExecutionState; }
53    }
54
55    public void Run() {
56      throw new NotSupportedException();
57    }
58
59    // [chn] needed?
60    public void Prepare(Core.IOperation initialOperation) {
61      throw new NotImplementedException();
62    }
63
64    public void Start() {
65      optimizer.Start();
66    }
67
68    public void Stop() {
69      optimizer.Stop();
70    }
71
72    public event EventHandler JobStopped;
73
74    public event EventHandler JobFailed;
75
76    #endregion
77
78    #region Events
79    private void RegisterEvents() {
80      optimizer.Stopped += new EventHandler(optimizer_Stopped);
81      optimizer.ExceptionOccurred += new EventHandler<Common.EventArgs<Exception>>(optimizer_ExceptionOccurred);
82    }
83
84    private void DeregisterEvents() {
85      optimizer.Stopped -= new EventHandler(optimizer_Stopped);
86      optimizer.ExceptionOccurred -= new EventHandler<Common.EventArgs<Exception>>(optimizer_ExceptionOccurred);
87    }
88
89    void optimizer_ExceptionOccurred(object sender, Common.EventArgs<Exception> e) {
90      if (optimizer.ExecutionState != ExecutionState.Stopped) this.progress = 0.0;
91      else this.progress = 1.0;
92      EventHandler handler = JobFailed;
93      if (handler != null) handler(this, e);
94    }
95
96    void optimizer_Stopped(object sender, EventArgs e) {
97      if (optimizer.ExecutionState != ExecutionState.Stopped) this.progress = 0.0;
98      else this.progress = 1.0;
99      EventHandler handler = JobStopped;
100      if (handler != null) handler(this, e);
101    }
102    #endregion
103
104
105  }
106}
Note: See TracBrowser for help on using the repository browser.