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