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 | using HeuristicLab.Hive;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Clients.Hive {
|
---|
33 | [StorableClass]
|
---|
34 | public class RefreshableHiveExperiment : IHiveItem, IDeepCloneable, IContent, IProgressReporter {
|
---|
35 | private JobResultPoller jobResultPoller;
|
---|
36 | private JobDownloader<ItemJob> jobDownloader = new JobDownloader<ItemJob>(2, 2);
|
---|
37 | private static object locker = new object();
|
---|
38 |
|
---|
39 | [Storable]
|
---|
40 | private HiveExperiment hiveExperiment;
|
---|
41 | public HiveExperiment HiveExperiment {
|
---|
42 | get { return hiveExperiment; }
|
---|
43 | set {
|
---|
44 | if (value != hiveExperiment) {
|
---|
45 | if (value == null)
|
---|
46 | throw new ArgumentNullException();
|
---|
47 |
|
---|
48 | if (hiveExperiment != null) DergisterHiveExperimentEvents();
|
---|
49 | hiveExperiment = value;
|
---|
50 | if (hiveExperiment != null) RegisterHiveExperimentEvents();
|
---|
51 | OnHiveExperimentChanged();
|
---|
52 | }
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | /** include jobs when refreshing **/
|
---|
57 | [Storable]
|
---|
58 | private bool includeJobs;
|
---|
59 | public bool IncludeJobs {
|
---|
60 | get { return includeJobs; }
|
---|
61 | set { includeJobs = value; }
|
---|
62 | }
|
---|
63 |
|
---|
64 | [Storable]
|
---|
65 | private bool refreshAutomatically;
|
---|
66 | public bool RefreshAutomatically {
|
---|
67 | get { return refreshAutomatically; }
|
---|
68 | set {
|
---|
69 | lock (locker) {
|
---|
70 | if (refreshAutomatically != value) {
|
---|
71 | refreshAutomatically = value;
|
---|
72 | OnRefreshAutomaticallyChanged();
|
---|
73 | }
|
---|
74 | if (RefreshAutomatically) {
|
---|
75 | if (hiveExperiment.HiveJobs != null && hiveExperiment.HiveJobs.Count > 0 && (jobResultPoller == null || !jobResultPoller.IsPolling)) {
|
---|
76 | StartResultPolling();
|
---|
77 | }
|
---|
78 | } else {
|
---|
79 | StopResultPolling();
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | #region Constructors and Cloning
|
---|
86 | public RefreshableHiveExperiment() {
|
---|
87 | this.includeJobs = true;
|
---|
88 | this.refreshAutomatically = true;
|
---|
89 | this.HiveExperiment = new HiveExperiment();
|
---|
90 | }
|
---|
91 | public RefreshableHiveExperiment(HiveExperiment hiveExperiment) {
|
---|
92 | this.includeJobs = true;
|
---|
93 | this.refreshAutomatically = true;
|
---|
94 | this.HiveExperiment = hiveExperiment;
|
---|
95 | }
|
---|
96 | protected RefreshableHiveExperiment(RefreshableHiveExperiment original, Cloner cloner) {
|
---|
97 | cloner.RegisterClonedObject(original, this);
|
---|
98 | this.HiveExperiment = original.HiveExperiment;
|
---|
99 | this.RefreshAutomatically = original.RefreshAutomatically;
|
---|
100 | this.IncludeJobs = original.IncludeJobs;
|
---|
101 | }
|
---|
102 | public IDeepCloneable Clone(Cloner cloner) {
|
---|
103 | return new RefreshableHiveExperiment(this, cloner);
|
---|
104 | }
|
---|
105 | public object Clone() {
|
---|
106 | return this.Clone(new Cloner());
|
---|
107 | }
|
---|
108 | #endregion
|
---|
109 |
|
---|
110 | private void hiveExperiment_HiveJobsChanged(object sender, EventArgs e) {
|
---|
111 | if (jobResultPoller != null && jobResultPoller.IsPolling) {
|
---|
112 | jobResultPoller.Stop();
|
---|
113 | DeregisterResultPollingEvents();
|
---|
114 | }
|
---|
115 | if (hiveExperiment.HiveJobs != null && hiveExperiment.HiveJobs.Count > 0 && hiveExperiment.GetAllHiveJobs().All(x => x.Job.Id != Guid.Empty)) {
|
---|
116 | if (this.RefreshAutomatically)
|
---|
117 | StartResultPolling();
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | #region JobResultPoller Events
|
---|
122 |
|
---|
123 | public void StartResultPolling() {
|
---|
124 | if (jobResultPoller == null) {
|
---|
125 | jobResultPoller = new JobResultPoller(hiveExperiment.Id, /*ApplicationConstants.ResultPollingInterval*/new TimeSpan(0, 0, 5)); //TODO: find a better place for ApplicationConstants
|
---|
126 | RegisterResultPollingEvents();
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (!jobResultPoller.IsPolling) {
|
---|
130 | jobResultPoller.Start();
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | public void StopResultPolling() {
|
---|
135 | if (jobResultPoller != null && jobResultPoller.IsPolling) {
|
---|
136 | jobResultPoller.Stop();
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | private void RegisterResultPollingEvents() {
|
---|
141 | jobResultPoller.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobResultPoller_ExceptionOccured);
|
---|
142 | jobResultPoller.JobResultsReceived += new EventHandler<EventArgs<IEnumerable<LightweightJob>>>(jobResultPoller_JobResultReceived);
|
---|
143 | jobResultPoller.IsPollingChanged += new EventHandler(jobResultPoller_IsPollingChanged);
|
---|
144 | }
|
---|
145 | private void DeregisterResultPollingEvents() {
|
---|
146 | jobResultPoller.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(jobResultPoller_ExceptionOccured);
|
---|
147 | jobResultPoller.JobResultsReceived -= new EventHandler<EventArgs<IEnumerable<LightweightJob>>>(jobResultPoller_JobResultReceived);
|
---|
148 | jobResultPoller.IsPollingChanged -= new EventHandler(jobResultPoller_IsPollingChanged);
|
---|
149 | }
|
---|
150 | private void jobResultPoller_IsPollingChanged(object sender, EventArgs e) {
|
---|
151 | this.refreshAutomatically = jobResultPoller.IsPolling;
|
---|
152 | OnRefreshAutomaticallyChanged();
|
---|
153 | }
|
---|
154 | private void jobResultPoller_JobResultReceived(object sender, EventArgs<IEnumerable<LightweightJob>> e) {
|
---|
155 | foreach (LightweightJob lightweightJob in e.Value) {
|
---|
156 | HiveJob hj = GetHiveJobById(lightweightJob.Id);
|
---|
157 | if (hj != null) {
|
---|
158 | DateTime lastJobDataUpdate = hj.Job.LastJobDataUpdate;
|
---|
159 | hj.UpdateFromLightweightJob(lightweightJob);
|
---|
160 |
|
---|
161 | // lastJobDataUpdate equals DateTime.MinValue right after it was uploaded. When the first results are polled, this value is updated
|
---|
162 | if (lastJobDataUpdate != DateTime.MinValue && lastJobDataUpdate < hj.Job.LastJobDataUpdate) {
|
---|
163 | jobDownloader.DownloadJob(hj.Job.Id, (id, itemJob, exception) => {
|
---|
164 | if (exception != null) {
|
---|
165 | throw new JobDownloaderException("Downloading job failed.", exception);
|
---|
166 | }
|
---|
167 |
|
---|
168 | if (itemJob == null) {
|
---|
169 | // something bad happened to this job. bad job, BAAAD job!
|
---|
170 | } else {
|
---|
171 | // if the job is paused, download but don't integrate into parent optimizer (to avoid Prepare)
|
---|
172 | if (hj.Job.State == JobState.Paused) {
|
---|
173 | hj.JobItem = itemJob;
|
---|
174 | } else {
|
---|
175 | if (lightweightJob.ParentJobId.HasValue) {
|
---|
176 | HiveJob parentHiveJob = GetHiveJobById(lightweightJob.ParentJobId.Value);
|
---|
177 | parentHiveJob.IntegrateChild(itemJob, hj.Job.Id);
|
---|
178 | } else {
|
---|
179 | hj.JobItem = itemJob;
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 | });
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 | GC.Collect(); // force GC, because .NET is too lazy here (deserialization takes a lot of memory)
|
---|
188 | if (AllJobsFinished()) {
|
---|
189 | hiveExperiment.ExecutionState = Core.ExecutionState.Stopped;
|
---|
190 | StopResultPolling();
|
---|
191 | }
|
---|
192 | UpdateTotalExecutionTime();
|
---|
193 | UpdateStats();
|
---|
194 | }
|
---|
195 |
|
---|
196 | public HiveJob GetHiveJobById(Guid jobId) {
|
---|
197 | foreach (HiveJob job in hiveExperiment.HiveJobs) {
|
---|
198 | var hj = job.GetHiveJobByJobId(jobId);
|
---|
199 | if (hj != null)
|
---|
200 | return hj;
|
---|
201 | }
|
---|
202 | return null;
|
---|
203 | }
|
---|
204 |
|
---|
205 | private void UpdateStats() {
|
---|
206 | var jobs = hiveExperiment.GetAllHiveJobs();
|
---|
207 | hiveExperiment.JobCount = jobs.Count();
|
---|
208 | hiveExperiment.CalculatingCount = jobs.Count(j => j.Job.State == JobState.Calculating);
|
---|
209 | hiveExperiment.FinishedCount = jobs.Count(j => j.Job.State == JobState.Finished);
|
---|
210 | }
|
---|
211 |
|
---|
212 | public bool AllJobsFinished() {
|
---|
213 | return hiveExperiment.GetAllHiveJobs().All(j => j.Job.State == JobState.Finished
|
---|
214 | || j.Job.State == JobState.Aborted
|
---|
215 | || j.Job.State == JobState.Failed);
|
---|
216 | }
|
---|
217 |
|
---|
218 | //public bool AllJobsFinishedAndDownloaded() {
|
---|
219 | // return this.AllJobsFinished() && hiveExperiment.GetAllHiveJobs().All(j => j.JobItem.;
|
---|
220 | //}
|
---|
221 |
|
---|
222 | private void jobResultPoller_ExceptionOccured(object sender, EventArgs<Exception> e) {
|
---|
223 | //OnExceptionOccured(e.Value);
|
---|
224 | }
|
---|
225 |
|
---|
226 | public void UpdateTotalExecutionTime() {
|
---|
227 | hiveExperiment.ExecutionTime = TimeSpan.FromMilliseconds(hiveExperiment.GetAllHiveJobs().Sum(x => x.Job.ExecutionTime.HasValue ? x.Job.ExecutionTime.Value.TotalMilliseconds : 0));
|
---|
228 | }
|
---|
229 | #endregion
|
---|
230 |
|
---|
231 | private void RegisterHiveExperimentEvents() {
|
---|
232 | hiveExperiment.HiveJobsChanged += new EventHandler(hiveExperiment_HiveJobsChanged);
|
---|
233 | hiveExperiment.ToStringChanged += new EventHandler(hiveExperiment_ToStringChanged);
|
---|
234 | hiveExperiment.PropertyChanged += new PropertyChangedEventHandler(hiveExperiment_PropertyChanged);
|
---|
235 | hiveExperiment.ItemImageChanged += new EventHandler(hiveExperiment_ItemImageChanged);
|
---|
236 | hiveExperiment.ModifiedChanged += new EventHandler(hiveExperiment_ModifiedChanged);
|
---|
237 | hiveExperiment.IsProgressingChanged += new EventHandler(hiveExperiment_IsProgressingChanged);
|
---|
238 | hiveExperiment.Loaded += new EventHandler(hiveExperiment_Loaded);
|
---|
239 | }
|
---|
240 |
|
---|
241 | private void DergisterHiveExperimentEvents() {
|
---|
242 | hiveExperiment.HiveJobsChanged -= new EventHandler(hiveExperiment_HiveJobsChanged);
|
---|
243 | hiveExperiment.ToStringChanged -= new EventHandler(hiveExperiment_ToStringChanged);
|
---|
244 | hiveExperiment.PropertyChanged -= new PropertyChangedEventHandler(hiveExperiment_PropertyChanged);
|
---|
245 | hiveExperiment.ItemImageChanged -= new EventHandler(hiveExperiment_ItemImageChanged);
|
---|
246 | hiveExperiment.ModifiedChanged -= new EventHandler(hiveExperiment_ModifiedChanged);
|
---|
247 | hiveExperiment.IsProgressingChanged -= new EventHandler(hiveExperiment_IsProgressingChanged);
|
---|
248 | hiveExperiment.Loaded -= new EventHandler(hiveExperiment_Loaded);
|
---|
249 | }
|
---|
250 |
|
---|
251 | private void hiveExperiment_Loaded(object sender, EventArgs e) {
|
---|
252 | this.UpdateTotalExecutionTime();
|
---|
253 |
|
---|
254 | if (hiveExperiment.ExecutionState != ExecutionState.Stopped) {
|
---|
255 | this.RefreshAutomatically = true;
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | #region Events
|
---|
260 | public event EventHandler RefreshAutomaticallyChanged;
|
---|
261 | private void OnRefreshAutomaticallyChanged() {
|
---|
262 | var handler = RefreshAutomaticallyChanged;
|
---|
263 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
264 | }
|
---|
265 |
|
---|
266 | public event EventHandler HiveExperimentChanged;
|
---|
267 | private void OnHiveExperimentChanged() {
|
---|
268 | var handler = HiveExperimentChanged;
|
---|
269 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
270 | }
|
---|
271 |
|
---|
272 | public event EventHandler ModifiedChanged;
|
---|
273 | private void hiveExperiment_ModifiedChanged(object sender, EventArgs e) {
|
---|
274 | var handler = ModifiedChanged;
|
---|
275 | if (handler != null) handler(sender, e);
|
---|
276 | }
|
---|
277 |
|
---|
278 | public event EventHandler ItemImageChanged;
|
---|
279 | private void hiveExperiment_ItemImageChanged(object sender, EventArgs e) {
|
---|
280 | var handler = ItemImageChanged;
|
---|
281 | if (handler != null) handler(this, e);
|
---|
282 | }
|
---|
283 |
|
---|
284 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
285 | private void hiveExperiment_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
286 | var handler = PropertyChanged;
|
---|
287 | if (handler != null) handler(sender, e);
|
---|
288 | }
|
---|
289 |
|
---|
290 | public event EventHandler ToStringChanged;
|
---|
291 | private void hiveExperiment_ToStringChanged(object sender, EventArgs e) {
|
---|
292 | var handler = ToStringChanged;
|
---|
293 | if (handler != null) handler(this, e);
|
---|
294 | }
|
---|
295 |
|
---|
296 | public event EventHandler IsProgressingChanged;
|
---|
297 | private void hiveExperiment_IsProgressingChanged(object sender, EventArgs e) {
|
---|
298 | var handler = IsProgressingChanged;
|
---|
299 | if (handler != null) handler(sender, e);
|
---|
300 | }
|
---|
301 | #endregion
|
---|
302 |
|
---|
303 | public Guid Id {
|
---|
304 | get { return hiveExperiment.Id; }
|
---|
305 | set { hiveExperiment.Id = value; }
|
---|
306 | }
|
---|
307 | public bool Modified {
|
---|
308 | get { return hiveExperiment.Modified; }
|
---|
309 | }
|
---|
310 | public void Store() {
|
---|
311 | hiveExperiment.Store();
|
---|
312 | }
|
---|
313 | public string ItemDescription {
|
---|
314 | get { return hiveExperiment.ItemDescription; }
|
---|
315 | }
|
---|
316 | public Image ItemImage {
|
---|
317 | get { return hiveExperiment.ItemImage; }
|
---|
318 | }
|
---|
319 | public string ItemName {
|
---|
320 | get { return hiveExperiment.ItemName; }
|
---|
321 | }
|
---|
322 | public Version ItemVersion {
|
---|
323 | get { return hiveExperiment.ItemVersion; }
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 | #region IProgressReporter Members
|
---|
328 | public IProgress Progress {
|
---|
329 | get { return HiveExperiment.Progress; }
|
---|
330 | }
|
---|
331 |
|
---|
332 | public bool IsProgressing {
|
---|
333 | get { return HiveExperiment.IsProgressing; }
|
---|
334 | }
|
---|
335 | #endregion
|
---|
336 |
|
---|
337 | public override string ToString() {
|
---|
338 | return HiveExperiment.ToString();
|
---|
339 | }
|
---|
340 | }
|
---|
341 | }
|
---|