1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Hive.JobBase;
|
---|
6 | using HeuristicLab.Optimization;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 |
|
---|
10 | namespace 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 | public void Prepare() {
|
---|
60 | optimizer.Prepare();
|
---|
61 | }
|
---|
62 |
|
---|
63 | public void Start() {
|
---|
64 | optimizer.Start();
|
---|
65 | }
|
---|
66 |
|
---|
67 | public void Stop() {
|
---|
68 | optimizer.Stop();
|
---|
69 | }
|
---|
70 |
|
---|
71 | public event EventHandler JobStopped;
|
---|
72 |
|
---|
73 | public event EventHandler JobFailed;
|
---|
74 |
|
---|
75 | #endregion
|
---|
76 |
|
---|
77 | #region Events
|
---|
78 | private void RegisterEvents() {
|
---|
79 | optimizer.Stopped += new EventHandler(optimizer_Stopped);
|
---|
80 | optimizer.ExceptionOccurred += new EventHandler<Common.EventArgs<Exception>>(optimizer_ExceptionOccurred);
|
---|
81 | }
|
---|
82 |
|
---|
83 | private void DeregisterEvents() {
|
---|
84 | optimizer.Stopped -= new EventHandler(optimizer_Stopped);
|
---|
85 | optimizer.ExceptionOccurred -= new EventHandler<Common.EventArgs<Exception>>(optimizer_ExceptionOccurred);
|
---|
86 | }
|
---|
87 |
|
---|
88 | void optimizer_ExceptionOccurred(object sender, Common.EventArgs<Exception> e) {
|
---|
89 | if (optimizer.ExecutionState != ExecutionState.Stopped) this.progress = 0.0;
|
---|
90 | else this.progress = 1.0;
|
---|
91 | EventHandler handler = JobFailed;
|
---|
92 | if (handler != null) handler(this, e);
|
---|
93 | }
|
---|
94 |
|
---|
95 | void optimizer_Stopped(object sender, EventArgs e) {
|
---|
96 | if (optimizer.ExecutionState != ExecutionState.Stopped) this.progress = 0.0;
|
---|
97 | else this.progress = 1.0;
|
---|
98 | EventHandler handler = JobStopped;
|
---|
99 | if (handler != null) handler(this, e);
|
---|
100 | }
|
---|
101 | #endregion
|
---|
102 |
|
---|
103 |
|
---|
104 | }
|
---|
105 | }
|
---|