1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Hive;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Common;
|
---|
9 | using System.Drawing;
|
---|
10 | using System.Threading;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Clients.Hive.Slave.Tests {
|
---|
13 | [Item("Mock Job", "Represents mocked Job which just SpinWaits.")]
|
---|
14 | [StorableClass]
|
---|
15 | public class MockJob : NamedItem, IJob {
|
---|
16 | public virtual bool IsParallelizable {
|
---|
17 | get { return false; }
|
---|
18 | }
|
---|
19 |
|
---|
20 | [Storable]
|
---|
21 | protected bool computeInParallel;
|
---|
22 | public bool ComputeInParallel {
|
---|
23 | get { return computeInParallel; }
|
---|
24 | set {
|
---|
25 | if (computeInParallel != value) {
|
---|
26 | computeInParallel = value;
|
---|
27 | OnComputeInParallelChanged();
|
---|
28 | }
|
---|
29 | }
|
---|
30 | }
|
---|
31 |
|
---|
32 | [Storable]
|
---|
33 | private int indexInParentOptimizerList = -1;
|
---|
34 | public int IndexInParentOptimizerList {
|
---|
35 | get { return indexInParentOptimizerList; }
|
---|
36 | set { this.indexInParentOptimizerList = value; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | [Storable]
|
---|
40 | private bool collectChildJobs;
|
---|
41 | public bool CollectChildJobs {
|
---|
42 | get { return collectChildJobs; }
|
---|
43 | set { collectChildJobs = value; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | private int ms = 1000; //ms
|
---|
47 | public MockJob(int ms) {
|
---|
48 | this.ms = ms;
|
---|
49 | }
|
---|
50 | public MockJob() { }
|
---|
51 | [StorableConstructor]
|
---|
52 | protected MockJob(bool deserializing) { }
|
---|
53 | protected MockJob(MockJob original, Cloner cloner) : base(original, cloner) {
|
---|
54 | this.ComputeInParallel = original.ComputeInParallel;
|
---|
55 | this.IndexInParentOptimizerList = original.IndexInParentOptimizerList;
|
---|
56 | this.CollectChildJobs = original.CollectChildJobs;
|
---|
57 | this.ExecutionTime = original.executionTime;
|
---|
58 | this.ExecutionState = original.executionState;
|
---|
59 | this.ms = original.ms;
|
---|
60 | }
|
---|
61 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
62 | return new MockJob(this, cloner);
|
---|
63 | }
|
---|
64 |
|
---|
65 | [Storable]
|
---|
66 | private ExecutionState executionState;
|
---|
67 | public virtual ExecutionState ExecutionState {
|
---|
68 | get { return executionState; }
|
---|
69 | set {
|
---|
70 | if (executionState != value) {
|
---|
71 | this.executionState = value;
|
---|
72 | OnExecutionStateChanged();
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | [Storable]
|
---|
78 | private TimeSpan executionTime;
|
---|
79 | public TimeSpan ExecutionTime {
|
---|
80 | get { return executionTime; }
|
---|
81 | set {
|
---|
82 | if (executionTime != value) {
|
---|
83 | executionTime = value;
|
---|
84 | OnExecutionTimeChanged();
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | public virtual void Prepare() {
|
---|
90 | this.ExecutionState = Core.ExecutionState.Prepared;
|
---|
91 | }
|
---|
92 |
|
---|
93 | public virtual void Start() {
|
---|
94 | DateTime start = DateTime.Now;
|
---|
95 | do {
|
---|
96 | Thread.SpinWait(1000);
|
---|
97 | this.ExecutionTime = DateTime.Now - start;
|
---|
98 | } while (ExecutionTime.TotalMilliseconds < ms);
|
---|
99 | Stop();
|
---|
100 | OnJobStopped();
|
---|
101 | }
|
---|
102 |
|
---|
103 | public virtual void Pause() {
|
---|
104 | this.ExecutionState = Core.ExecutionState.Paused;
|
---|
105 | }
|
---|
106 |
|
---|
107 | public virtual void Stop() {
|
---|
108 | ExecutionState = Core.ExecutionState.Stopped;
|
---|
109 | }
|
---|
110 |
|
---|
111 | public virtual void Resume(IEnumerable<IJob> childJobs) {
|
---|
112 | ExecutionState = Core.ExecutionState.Stopped;
|
---|
113 | }
|
---|
114 |
|
---|
115 | #region Events
|
---|
116 | public event EventHandler<EventArgs<IJob>> NewChildJob;
|
---|
117 | protected virtual void OnNewChildJob(IJob job) {
|
---|
118 | EventHandler<EventArgs<IJob>> handler = NewChildJob;
|
---|
119 | if (handler != null) handler(this, new EventArgs<IJob>(job));
|
---|
120 | }
|
---|
121 |
|
---|
122 | public event EventHandler WaitForChildJobs;
|
---|
123 | protected virtual void OnWaitForChildJobs() {
|
---|
124 | EventHandler handler = WaitForChildJobs;
|
---|
125 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
126 | }
|
---|
127 |
|
---|
128 | public event EventHandler DeleteChildJobs;
|
---|
129 | protected virtual void OnDeleteChildJobs() {
|
---|
130 | EventHandler handler = DeleteChildJobs;
|
---|
131 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
132 | }
|
---|
133 |
|
---|
134 | public event EventHandler ComputeInParallelChanged;
|
---|
135 | protected virtual void OnComputeInParallelChanged() {
|
---|
136 | EventHandler handler = ComputeInParallelChanged;
|
---|
137 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
138 | }
|
---|
139 |
|
---|
140 | public event EventHandler ExecutionTimeChanged;
|
---|
141 | protected virtual void OnExecutionTimeChanged() {
|
---|
142 | EventHandler handler = ExecutionTimeChanged;
|
---|
143 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
144 | }
|
---|
145 |
|
---|
146 | public event EventHandler ExecutionStateChanged;
|
---|
147 | protected virtual void OnExecutionStateChanged() {
|
---|
148 | EventHandler handler = ExecutionStateChanged;
|
---|
149 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
150 | }
|
---|
151 |
|
---|
152 | public event EventHandler JobFailed;
|
---|
153 | protected virtual void OnJobFailed() {
|
---|
154 | EventHandler handler = JobFailed;
|
---|
155 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
156 | }
|
---|
157 |
|
---|
158 | public event EventHandler JobStopped;
|
---|
159 | protected virtual void OnJobStopped() {
|
---|
160 | EventHandler handler = JobStopped;
|
---|
161 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
162 | }
|
---|
163 |
|
---|
164 | #endregion
|
---|
165 | }
|
---|
166 | }
|
---|