1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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.ComponentModel;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Linq;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Clients.Hive {
|
---|
31 | public class RefreshableHiveExperiment : IHiveItem, IDeepCloneable, IContent, IProgressReporter {
|
---|
32 | private JobResultPoller jobResultPoller;
|
---|
33 | private ConcurrentJobDownloader<ItemJob> jobDownloader;
|
---|
34 | private static object locker = new object();
|
---|
35 |
|
---|
36 | private HiveExperiment hiveExperiment;
|
---|
37 | public HiveExperiment HiveExperiment {
|
---|
38 | get { return hiveExperiment; }
|
---|
39 | set {
|
---|
40 | if (value != hiveExperiment) {
|
---|
41 | if (value == null)
|
---|
42 | throw new ArgumentNullException();
|
---|
43 |
|
---|
44 | if (hiveExperiment != null) DergisterHiveExperimentEvents();
|
---|
45 | hiveExperiment = value;
|
---|
46 | if (hiveExperiment != null) RegisterHiveExperimentEvents();
|
---|
47 | OnHiveExperimentChanged();
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | private bool refreshAutomatically;
|
---|
53 | public bool RefreshAutomatically {
|
---|
54 | get { return refreshAutomatically; }
|
---|
55 | set {
|
---|
56 | lock (locker) {
|
---|
57 | if (refreshAutomatically != value) {
|
---|
58 | refreshAutomatically = value;
|
---|
59 | OnRefreshAutomaticallyChanged();
|
---|
60 | }
|
---|
61 | if (RefreshAutomatically) {
|
---|
62 | if (hiveExperiment.HiveJobs != null && hiveExperiment.HiveJobs.Count > 0 && (jobResultPoller == null || !jobResultPoller.IsPolling)) {
|
---|
63 | StartResultPolling();
|
---|
64 | }
|
---|
65 | } else {
|
---|
66 | StopResultPolling();
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | // if true, all control buttons should be enabled. otherwise disabled (used for HiveEngine)
|
---|
73 | private bool isControllable = true;
|
---|
74 | public bool IsControllable {
|
---|
75 | get { return isControllable; }
|
---|
76 | set {
|
---|
77 | if (value != isControllable) {
|
---|
78 | isControllable = value;
|
---|
79 | OnIsControllableChanged();
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | private ILog log;
|
---|
85 | public ILog Log {
|
---|
86 | get { return log; }
|
---|
87 | set { log = value; }
|
---|
88 | }
|
---|
89 | private static object logLocker = new object();
|
---|
90 |
|
---|
91 |
|
---|
92 | #region Constructors and Cloning
|
---|
93 | public RefreshableHiveExperiment() {
|
---|
94 | this.refreshAutomatically = true;
|
---|
95 | this.HiveExperiment = new HiveExperiment();
|
---|
96 | this.log = new Log();
|
---|
97 | this.jobDownloader = new ConcurrentJobDownloader<ItemJob>(2, 2);
|
---|
98 | this.jobDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured);
|
---|
99 | }
|
---|
100 | public RefreshableHiveExperiment(HiveExperiment hiveExperiment) {
|
---|
101 | this.refreshAutomatically = true;
|
---|
102 | this.HiveExperiment = hiveExperiment;
|
---|
103 | this.log = new Log();
|
---|
104 | this.jobDownloader = new ConcurrentJobDownloader<ItemJob>(2, 2);
|
---|
105 | this.jobDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured);
|
---|
106 | }
|
---|
107 | protected RefreshableHiveExperiment(RefreshableHiveExperiment original, Cloner cloner) {
|
---|
108 | cloner.RegisterClonedObject(original, this);
|
---|
109 | this.HiveExperiment = original.HiveExperiment;
|
---|
110 | this.IsControllable = original.IsControllable;
|
---|
111 | this.Log = cloner.Clone(original.Log);
|
---|
112 | this.RefreshAutomatically = false; // do not start results polling automatically
|
---|
113 | this.jobDownloader = new ConcurrentJobDownloader<ItemJob>(2, 2);
|
---|
114 | this.jobDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured);
|
---|
115 | }
|
---|
116 | public IDeepCloneable Clone(Cloner cloner) {
|
---|
117 | return new RefreshableHiveExperiment(this, cloner);
|
---|
118 | }
|
---|
119 | public object Clone() {
|
---|
120 | return this.Clone(new Cloner());
|
---|
121 | }
|
---|
122 | #endregion
|
---|
123 |
|
---|
124 | private void hiveExperiment_HiveJobsChanged(object sender, EventArgs e) {
|
---|
125 | if (jobResultPoller != null && jobResultPoller.IsPolling) {
|
---|
126 | jobResultPoller.Stop();
|
---|
127 | DeregisterResultPollingEvents();
|
---|
128 | }
|
---|
129 | if (hiveExperiment.HiveJobs != null && hiveExperiment.HiveJobs.Count > 0 && hiveExperiment.GetAllHiveJobs().All(x => x.Job.Id != Guid.Empty)) {
|
---|
130 | if (this.RefreshAutomatically)
|
---|
131 | StartResultPolling();
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | #region JobResultPoller Events
|
---|
136 |
|
---|
137 | public void StartResultPolling() {
|
---|
138 | if (jobResultPoller == null) {
|
---|
139 | jobResultPoller = new JobResultPoller(hiveExperiment.Id, /*ApplicationConstants.ResultPollingInterval*/new TimeSpan(0, 0, 5)); //TODO: find a better place for ApplicationConstants
|
---|
140 | RegisterResultPollingEvents();
|
---|
141 | jobResultPoller.AutoResumeOnException = !IsControllable;
|
---|
142 | }
|
---|
143 |
|
---|
144 | if (!jobResultPoller.IsPolling) {
|
---|
145 | jobResultPoller.Start();
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | public void StopResultPolling() {
|
---|
150 | if (jobResultPoller != null && jobResultPoller.IsPolling) {
|
---|
151 | jobResultPoller.Stop();
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | private void RegisterResultPollingEvents() {
|
---|
156 | jobResultPoller.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobResultPoller_ExceptionOccured);
|
---|
157 | jobResultPoller.JobResultsReceived += new EventHandler<EventArgs<IEnumerable<LightweightJob>>>(jobResultPoller_JobResultReceived);
|
---|
158 | jobResultPoller.IsPollingChanged += new EventHandler(jobResultPoller_IsPollingChanged);
|
---|
159 | }
|
---|
160 | private void DeregisterResultPollingEvents() {
|
---|
161 | jobResultPoller.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(jobResultPoller_ExceptionOccured);
|
---|
162 | jobResultPoller.JobResultsReceived -= new EventHandler<EventArgs<IEnumerable<LightweightJob>>>(jobResultPoller_JobResultReceived);
|
---|
163 | jobResultPoller.IsPollingChanged -= new EventHandler(jobResultPoller_IsPollingChanged);
|
---|
164 | }
|
---|
165 | private void jobResultPoller_IsPollingChanged(object sender, EventArgs e) {
|
---|
166 | if (this.refreshAutomatically != jobResultPoller.IsPolling) {
|
---|
167 | this.refreshAutomatically = jobResultPoller.IsPolling;
|
---|
168 | OnRefreshAutomaticallyChanged();
|
---|
169 | }
|
---|
170 | }
|
---|
171 | private void jobResultPoller_JobResultReceived(object sender, EventArgs<IEnumerable<LightweightJob>> e) {
|
---|
172 | foreach (LightweightJob lightweightJob in e.Value) {
|
---|
173 | HiveJob hiveJob = GetHiveJobById(lightweightJob.Id);
|
---|
174 | if (hiveJob != null) {
|
---|
175 | // lastJobDataUpdate equals DateTime.MinValue right after it was uploaded. When the first results are polled, this value is updated
|
---|
176 | if (hiveJob.Job.State == JobState.Offline && lightweightJob.State != JobState.Finished && lightweightJob.State != JobState.Failed && lightweightJob.State != JobState.Aborted) {
|
---|
177 | hiveJob.Job.LastJobDataUpdate = lightweightJob.LastJobDataUpdate;
|
---|
178 | }
|
---|
179 |
|
---|
180 | hiveJob.UpdateFromLightweightJob(lightweightJob);
|
---|
181 |
|
---|
182 | if (!hiveJob.IsFinishedJobDownloaded && !hiveJob.IsDownloading && hiveJob.Job.LastJobDataUpdate < lightweightJob.LastJobDataUpdate) {
|
---|
183 | LogMessage(string.Format("Downloading job {0}", lightweightJob.Id));
|
---|
184 | hiveJob.IsDownloading = true;
|
---|
185 | jobDownloader.DownloadJob(hiveJob.Job, (localJob, itemJob) => {
|
---|
186 | LogMessage(string.Format("Finished downloading job {0}", localJob.Id));
|
---|
187 | HiveJob localHiveJob = GetHiveJobById(localJob.Id);
|
---|
188 |
|
---|
189 | if (itemJob == null) {
|
---|
190 | localHiveJob.IsDownloading = false;
|
---|
191 | }
|
---|
192 |
|
---|
193 | if (itemJob == null) {
|
---|
194 | // something bad happened to this job. bad job, BAAAD job!
|
---|
195 | } else {
|
---|
196 | // if the job is paused, download but don't integrate into parent optimizer (to avoid Prepare)
|
---|
197 |
|
---|
198 | if (localJob.State == JobState.Paused) {
|
---|
199 | localHiveJob.ItemJob = itemJob;
|
---|
200 | } else {
|
---|
201 | if (localJob.ParentJobId.HasValue) {
|
---|
202 | HiveJob parentHiveJob = GetHiveJobById(localJob.ParentJobId.Value);
|
---|
203 | parentHiveJob.IntegrateChild(itemJob, localJob.Id);
|
---|
204 | } else {
|
---|
205 | localHiveJob.ItemJob = itemJob;
|
---|
206 | }
|
---|
207 | }
|
---|
208 | localHiveJob.IsDownloading = false;
|
---|
209 | localHiveJob.Job.LastJobDataUpdate = localJob.LastJobDataUpdate;
|
---|
210 | }
|
---|
211 | });
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|
215 | GC.Collect(); // force GC, because .NET is too lazy here (deserialization takes a lot of memory)
|
---|
216 | if (AllJobsFinished()) {
|
---|
217 | hiveExperiment.ExecutionState = Core.ExecutionState.Stopped;
|
---|
218 | StopResultPolling();
|
---|
219 | }
|
---|
220 | UpdateTotalExecutionTime();
|
---|
221 | UpdateStatistics();
|
---|
222 | }
|
---|
223 |
|
---|
224 | // synchronized logging
|
---|
225 | private void LogException(Exception exception) {
|
---|
226 | lock (logLocker) {
|
---|
227 | this.log.LogException(exception);
|
---|
228 | }
|
---|
229 | }
|
---|
230 | // synchronized logging
|
---|
231 | private void LogMessage(string message) {
|
---|
232 | lock (logLocker) {
|
---|
233 | this.log.LogMessage(message);
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | public HiveJob GetHiveJobById(Guid jobId) {
|
---|
238 | foreach (HiveJob job in hiveExperiment.HiveJobs) {
|
---|
239 | var hj = job.GetHiveJobByJobId(jobId);
|
---|
240 | if (hj != null)
|
---|
241 | return hj;
|
---|
242 | }
|
---|
243 | return null;
|
---|
244 | }
|
---|
245 | private void UpdateStatistics() {
|
---|
246 | var jobs = hiveExperiment.GetAllHiveJobs();
|
---|
247 | hiveExperiment.JobCount = jobs.Count();
|
---|
248 | hiveExperiment.CalculatingCount = jobs.Count(j => j.Job.State == JobState.Calculating);
|
---|
249 | hiveExperiment.FinishedCount = jobs.Count(j => j.Job.State == JobState.Finished);
|
---|
250 | OnJobStatisticsChanged();
|
---|
251 | }
|
---|
252 |
|
---|
253 | public bool AllJobsFinished() {
|
---|
254 | return hiveExperiment.GetAllHiveJobs().All(j => (j.Job.State == JobState.Finished
|
---|
255 | || j.Job.State == JobState.Aborted
|
---|
256 | || j.Job.State == JobState.Failed)
|
---|
257 | && j.IsFinishedJobDownloaded);
|
---|
258 | }
|
---|
259 |
|
---|
260 | private void jobResultPoller_ExceptionOccured(object sender, EventArgs<Exception> e) {
|
---|
261 | OnExceptionOccured(e.Value);
|
---|
262 | }
|
---|
263 | private void jobDownloader_ExceptionOccured(object sender, EventArgs<Exception> e) {
|
---|
264 | OnExceptionOccured(e.Value);
|
---|
265 | }
|
---|
266 | public void UpdateTotalExecutionTime() {
|
---|
267 | hiveExperiment.ExecutionTime = TimeSpan.FromMilliseconds(hiveExperiment.GetAllHiveJobs().Sum(x => x.Job.ExecutionTime.TotalMilliseconds));
|
---|
268 | }
|
---|
269 | #endregion
|
---|
270 |
|
---|
271 | private void RegisterHiveExperimentEvents() {
|
---|
272 | hiveExperiment.HiveJobsChanged += new EventHandler(hiveExperiment_HiveJobsChanged);
|
---|
273 | hiveExperiment.ToStringChanged += new EventHandler(hiveExperiment_ToStringChanged);
|
---|
274 | hiveExperiment.PropertyChanged += new PropertyChangedEventHandler(hiveExperiment_PropertyChanged);
|
---|
275 | hiveExperiment.ItemImageChanged += new EventHandler(hiveExperiment_ItemImageChanged);
|
---|
276 | hiveExperiment.ModifiedChanged += new EventHandler(hiveExperiment_ModifiedChanged);
|
---|
277 | hiveExperiment.IsProgressingChanged += new EventHandler(hiveExperiment_IsProgressingChanged);
|
---|
278 | hiveExperiment.Loaded += new EventHandler(hiveExperiment_Loaded);
|
---|
279 | }
|
---|
280 |
|
---|
281 | private void DergisterHiveExperimentEvents() {
|
---|
282 | hiveExperiment.HiveJobsChanged -= new EventHandler(hiveExperiment_HiveJobsChanged);
|
---|
283 | hiveExperiment.ToStringChanged -= new EventHandler(hiveExperiment_ToStringChanged);
|
---|
284 | hiveExperiment.PropertyChanged -= new PropertyChangedEventHandler(hiveExperiment_PropertyChanged);
|
---|
285 | hiveExperiment.ItemImageChanged -= new EventHandler(hiveExperiment_ItemImageChanged);
|
---|
286 | hiveExperiment.ModifiedChanged -= new EventHandler(hiveExperiment_ModifiedChanged);
|
---|
287 | hiveExperiment.IsProgressingChanged -= new EventHandler(hiveExperiment_IsProgressingChanged);
|
---|
288 | hiveExperiment.Loaded -= new EventHandler(hiveExperiment_Loaded);
|
---|
289 | }
|
---|
290 |
|
---|
291 | private void hiveExperiment_Loaded(object sender, EventArgs e) {
|
---|
292 | this.UpdateTotalExecutionTime();
|
---|
293 |
|
---|
294 | if (hiveExperiment.ExecutionState != ExecutionState.Stopped) {
|
---|
295 | this.RefreshAutomatically = true;
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 | #region Events
|
---|
300 | public event EventHandler RefreshAutomaticallyChanged;
|
---|
301 | private void OnRefreshAutomaticallyChanged() {
|
---|
302 | var handler = RefreshAutomaticallyChanged;
|
---|
303 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
304 | }
|
---|
305 |
|
---|
306 | public event EventHandler HiveExperimentChanged;
|
---|
307 | private void OnHiveExperimentChanged() {
|
---|
308 | var handler = HiveExperimentChanged;
|
---|
309 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
310 | }
|
---|
311 |
|
---|
312 | public event EventHandler ModifiedChanged;
|
---|
313 | private void hiveExperiment_ModifiedChanged(object sender, EventArgs e) {
|
---|
314 | var handler = ModifiedChanged;
|
---|
315 | if (handler != null) handler(sender, e);
|
---|
316 | }
|
---|
317 |
|
---|
318 | public event EventHandler ItemImageChanged;
|
---|
319 | private void hiveExperiment_ItemImageChanged(object sender, EventArgs e) {
|
---|
320 | var handler = ItemImageChanged;
|
---|
321 | if (handler != null) handler(this, e);
|
---|
322 | }
|
---|
323 |
|
---|
324 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
325 | private void hiveExperiment_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
326 | var handler = PropertyChanged;
|
---|
327 | if (handler != null) handler(sender, e);
|
---|
328 | }
|
---|
329 |
|
---|
330 | public event EventHandler ToStringChanged;
|
---|
331 | private void hiveExperiment_ToStringChanged(object sender, EventArgs e) {
|
---|
332 | var handler = ToStringChanged;
|
---|
333 | if (handler != null) handler(this, e);
|
---|
334 | }
|
---|
335 |
|
---|
336 | public event EventHandler IsProgressingChanged;
|
---|
337 | private void hiveExperiment_IsProgressingChanged(object sender, EventArgs e) {
|
---|
338 | var handler = IsProgressingChanged;
|
---|
339 | if (handler != null) handler(sender, e);
|
---|
340 | }
|
---|
341 |
|
---|
342 | public event EventHandler IsControllableChanged;
|
---|
343 | private void OnIsControllableChanged() {
|
---|
344 | var handler = IsControllableChanged;
|
---|
345 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
346 | }
|
---|
347 |
|
---|
348 | public event EventHandler JobStatisticsChanged;
|
---|
349 | private void OnJobStatisticsChanged() {
|
---|
350 | var handler = JobStatisticsChanged;
|
---|
351 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
352 | }
|
---|
353 |
|
---|
354 | public event EventHandler<EventArgs<Exception>> ExceptionOccured;
|
---|
355 | private void OnExceptionOccured(Exception exception) {
|
---|
356 | LogException(exception);
|
---|
357 | var handler = ExceptionOccured;
|
---|
358 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
359 | }
|
---|
360 | #endregion
|
---|
361 |
|
---|
362 | public Guid Id {
|
---|
363 | get { return hiveExperiment.Id; }
|
---|
364 | set { hiveExperiment.Id = value; }
|
---|
365 | }
|
---|
366 | public bool Modified {
|
---|
367 | get { return hiveExperiment.Modified; }
|
---|
368 | }
|
---|
369 | public void Store() {
|
---|
370 | hiveExperiment.Store();
|
---|
371 | }
|
---|
372 | public string ItemDescription {
|
---|
373 | get { return hiveExperiment.ItemDescription; }
|
---|
374 | }
|
---|
375 | public Image ItemImage {
|
---|
376 | get { return hiveExperiment.ItemImage; }
|
---|
377 | }
|
---|
378 | public string ItemName {
|
---|
379 | get { return hiveExperiment.ItemName; }
|
---|
380 | }
|
---|
381 | public Version ItemVersion {
|
---|
382 | get { return hiveExperiment.ItemVersion; }
|
---|
383 | }
|
---|
384 |
|
---|
385 |
|
---|
386 | #region IProgressReporter Members
|
---|
387 | public IProgress Progress {
|
---|
388 | get { return HiveExperiment.Progress; }
|
---|
389 | }
|
---|
390 |
|
---|
391 | public bool IsProgressing {
|
---|
392 | get { return HiveExperiment.IsProgressing; }
|
---|
393 | }
|
---|
394 | #endregion
|
---|
395 |
|
---|
396 | public override string ToString() {
|
---|
397 | return HiveExperiment.ToString();
|
---|
398 | }
|
---|
399 | }
|
---|
400 | }
|
---|