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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Hive {
|
---|
30 | [Item("Abstract Job", "Represents a executable hive job.")]
|
---|
31 | [StorableClass]
|
---|
32 | public abstract class AbstractJob : DeepCloneable, IJob {
|
---|
33 | public virtual bool IsParallelizable {
|
---|
34 | get { return true; }
|
---|
35 | }
|
---|
36 |
|
---|
37 | [Storable]
|
---|
38 | protected ILog log;
|
---|
39 | public ILog Log {
|
---|
40 | get { return log; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | [Storable]
|
---|
44 | protected bool computeInParallel;
|
---|
45 | public bool ComputeInParallel {
|
---|
46 | get { return computeInParallel; }
|
---|
47 | set {
|
---|
48 | if (computeInParallel != value) {
|
---|
49 | computeInParallel = value;
|
---|
50 | OnComputeInParallelChanged();
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | [Storable]
|
---|
56 | private bool collectChildJobs;
|
---|
57 | public bool CollectChildJobs {
|
---|
58 | get { return collectChildJobs; }
|
---|
59 | set { collectChildJobs = value; }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public AbstractJob() {
|
---|
63 | this.log = new Log();
|
---|
64 | }
|
---|
65 | [StorableConstructor]
|
---|
66 | protected AbstractJob(bool deserializing) { }
|
---|
67 | protected AbstractJob(AbstractJob original, Cloner cloner)
|
---|
68 | : base(original, cloner) {
|
---|
69 | this.log = cloner.Clone(original.Log);
|
---|
70 | this.ComputeInParallel = original.ComputeInParallel;
|
---|
71 | this.CollectChildJobs = original.CollectChildJobs;
|
---|
72 | }
|
---|
73 |
|
---|
74 | #region IJob Members
|
---|
75 |
|
---|
76 | public abstract ExecutionState ExecutionState{ get; }
|
---|
77 |
|
---|
78 | public abstract TimeSpan ExecutionTime{ get; }
|
---|
79 |
|
---|
80 | public abstract void Prepare();
|
---|
81 |
|
---|
82 | public abstract void Start();
|
---|
83 |
|
---|
84 | public abstract void Pause();
|
---|
85 |
|
---|
86 | public abstract void Stop();
|
---|
87 |
|
---|
88 | public abstract void Resume(IEnumerable<IJob> childJobs);
|
---|
89 |
|
---|
90 | public event EventHandler JobStopped;
|
---|
91 | protected virtual void OnJobStopped() {
|
---|
92 | EventHandler handler = JobStopped;
|
---|
93 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
94 | }
|
---|
95 |
|
---|
96 | public event EventHandler JobPaused;
|
---|
97 | protected virtual void OnJobPaused() {
|
---|
98 | EventHandler handler = JobPaused;
|
---|
99 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
100 | }
|
---|
101 |
|
---|
102 | public event EventHandler JobFailed;
|
---|
103 | protected virtual void OnJobFailed(EventArgs<Exception> e) {
|
---|
104 | EventHandler handler = JobFailed;
|
---|
105 | if (handler != null) handler(this, e);
|
---|
106 | }
|
---|
107 |
|
---|
108 | public event EventHandler<EventArgs<IJob>> NewChildJob;
|
---|
109 | protected virtual void OnNewChildJob(IJob job) {
|
---|
110 | EventHandler<EventArgs<IJob>> handler = NewChildJob;
|
---|
111 | if (handler != null) handler(this, new EventArgs<IJob>(job));
|
---|
112 | }
|
---|
113 |
|
---|
114 | public event EventHandler WaitForChildJobs;
|
---|
115 | protected virtual void OnWaitForChildJobs() {
|
---|
116 | EventHandler handler = WaitForChildJobs;
|
---|
117 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
118 | }
|
---|
119 |
|
---|
120 | public event EventHandler DeleteChildJobs;
|
---|
121 | protected virtual void OnDeleteChildJobs() {
|
---|
122 | EventHandler handler = DeleteChildJobs;
|
---|
123 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
124 | }
|
---|
125 |
|
---|
126 | public event EventHandler ComputeInParallelChanged;
|
---|
127 | protected virtual void OnComputeInParallelChanged() {
|
---|
128 | EventHandler handler = ComputeInParallelChanged;
|
---|
129 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
130 | }
|
---|
131 |
|
---|
132 | public event EventHandler OptimizerChanged;
|
---|
133 | protected virtual void OnOptimizerChanged() {
|
---|
134 | EventHandler handler = OptimizerChanged;
|
---|
135 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
136 | }
|
---|
137 | #endregion
|
---|
138 |
|
---|
139 | #region INamedItem Members
|
---|
140 | public abstract bool CanChangeDescription { get; }
|
---|
141 |
|
---|
142 | public abstract bool CanChangeName { get; }
|
---|
143 |
|
---|
144 | public abstract string Description { get; set; }
|
---|
145 |
|
---|
146 | public abstract string Name { get; set; }
|
---|
147 | #endregion
|
---|
148 |
|
---|
149 | #region Events
|
---|
150 | public event EventHandler DescriptionChanged;
|
---|
151 | protected virtual void OnDescriptionChanged() {
|
---|
152 | var handler = DescriptionChanged;
|
---|
153 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
154 | }
|
---|
155 | public event EventHandler ItemImageChanged;
|
---|
156 | protected virtual void OnItemImageChanged() {
|
---|
157 | var handler = ItemImageChanged;
|
---|
158 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
159 | }
|
---|
160 | public event EventHandler ToStringChanged;
|
---|
161 | protected virtual void OnToStringChanged() {
|
---|
162 | var handler = ToStringChanged;
|
---|
163 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
164 | }
|
---|
165 | public event EventHandler NameChanged;
|
---|
166 | protected virtual void OnNameChanged() {
|
---|
167 | var handler = NameChanged;
|
---|
168 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
169 | }
|
---|
170 | public event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
171 | protected virtual void OnNameChanging(string value, bool cancel) {
|
---|
172 | var handler = NameChanging;
|
---|
173 | if (handler != null) handler(this, new CancelEventArgs<string>(value, cancel));
|
---|
174 | }
|
---|
175 | public event EventHandler ExecutionTimeChanged;
|
---|
176 | protected virtual void OnExecutionTimeChanged() {
|
---|
177 | EventHandler handler = ExecutionTimeChanged;
|
---|
178 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
179 | }
|
---|
180 | public event EventHandler ExecutionStateChanged;
|
---|
181 | protected virtual void OnExecutionStateChanged() {
|
---|
182 | EventHandler handler = ExecutionStateChanged;
|
---|
183 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
184 | }
|
---|
185 | #endregion
|
---|
186 |
|
---|
187 | #region IItem Members
|
---|
188 |
|
---|
189 | public abstract string ItemDescription { get; }
|
---|
190 |
|
---|
191 | public abstract Image ItemImage { get; }
|
---|
192 |
|
---|
193 | public abstract string ItemName { get; }
|
---|
194 |
|
---|
195 | public abstract Version ItemVersion { get; }
|
---|
196 |
|
---|
197 | #endregion
|
---|
198 |
|
---|
199 | public override string ToString() {
|
---|
200 | return Name;
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|