Changeset 6033 for branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ServiceClients/HiveExperiment.cs
- Timestamp:
- 04/19/11 18:58:53 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ServiceClients/HiveExperiment.cs
r6006 r6033 24 24 using System.ComponentModel; 25 25 using System.Linq; 26 using HeuristicLab.Clients.Hive.Jobs;27 26 using HeuristicLab.Common; 28 27 using HeuristicLab.Core; 29 using HeuristicLab.Optimization;30 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 29 … … 33 31 [StorableClass] 34 32 public partial class HiveExperiment : IDeepCloneable, IContent, IProgressReporter { 35 private JobResultPoller jobResultPoller;36 37 33 [Storable] 38 34 private bool useLocalPlugins; … … 71 67 get { return hiveJobs; } 72 68 set { 73 DeregisterHiveJobsEvents();74 69 if (hiveJobs != value) { 75 70 hiveJobs = value; 76 RegisterHiveJobsEvents();77 71 OnHiveJobsChanged(); 78 72 } … … 92 86 } 93 87 94 /** include jobs when refreshing **/95 [Storable]96 private bool includeJobs;97 public bool IncludeJobs {98 get { return includeJobs; }99 set { includeJobs = value; }100 }101 102 [Storable]103 private bool refreshAutomatically;104 public bool RefreshAutomatically {105 get { return refreshAutomatically; }106 set {107 if (refreshAutomatically != value) {108 refreshAutomatically = value;109 OnRefreshAutomaticallyChanged();110 if (RefreshAutomatically) {111 StartResultPolling();112 } else {113 StopResultPolling();114 }115 }116 }117 }118 119 88 [Storable] 120 89 private IProgress progress; … … 127 96 public HiveExperiment() { 128 97 this.ResourceNames = "HEAL"; 129 this.includeJobs = true; 130 this.refreshAutomatically = true; 98 this.HiveJobs = new ItemCollection<HiveJob>(); 131 99 } 132 100 … … 140 108 this.Description = original.Description; 141 109 this.Id = original.Id; 142 110 this.HiveJobs = cloner.Clone(original.HiveJobs); 143 111 this.UseLocalPlugins = original.UseLocalPlugins; 144 112 this.ExecutionTime = original.ExecutionTime; … … 148 116 } 149 117 #endregion 150 151 public override string ToString() {152 return Name;153 }154 118 155 119 #region Events … … 167 131 168 132 public event EventHandler HiveJobsChanged; 169 private void OnHiveJobsChanged() { 170 if (jobResultPoller != null && jobResultPoller.IsPolling) { 171 jobResultPoller.Stop(); 172 DeregisterResultPollingEvents(); 173 } 174 if (HiveJobs != null && HiveJobs.Count > 0 && GetAllHiveJobs().All(x => x.Job.Id != Guid.Empty)) { 175 if (this.RefreshAutomatically) 176 StartResultPolling(); 177 } 133 protected virtual void OnHiveJobsChanged() { 178 134 EventHandler handler = HiveJobsChanged; 179 135 if (handler != null) handler(this, EventArgs.Empty); … … 185 141 if (handler != null) handler(this, EventArgs.Empty); 186 142 } 187 188 public event EventHandler RefreshAutomaticallyChanged;189 private void OnRefreshAutomaticallyChanged() {190 EventHandler handler = RefreshAutomaticallyChanged;191 if (handler != null) handler(this, EventArgs.Empty);192 }193 143 #endregion 194 195 private void RegisterHiveJobsEvents() { 196 //if (HiveJobs != null) { 197 // HiveJobs.JobStateChanged += new EventHandler(HiveJob_JobStateChanged); 198 //} 199 } 200 201 private void DeregisterHiveJobsEvents() { 202 //if (HiveJobs != null) { 203 // HiveJobs.JobStateChanged -= new EventHandler(HiveJob_JobStateChanged); 204 //} 205 } 206 207 private void HiveJob_JobStateChanged(object sender, EventArgs e) { 208 //if (this.HiveJobs != null) { 209 // this.RootJobId = HiveJobs.Job.Id; 210 //} 211 } 212 213 public Experiment GetExperiment(int idx) { 214 if (this.HiveJobs != null) { 215 var hj = HiveJobs.ElementAtOrDefault(idx); 216 if (hj != null) 217 return hj.OptimizerJob.OptimizerAsExperiment; 218 } 219 return null; 220 } 221 222 public void AddExperiment(Experiment experiment) { 223 if (this.HiveJobs == null) 224 this.HiveJobs = new ItemCollection<HiveJob>(); 225 this.HiveJobs.Add(new HiveJob(experiment)); 226 } 227 228 public void SetExperiment(Experiment experiment) { 229 if (this.HiveJobs == null) 230 this.HiveJobs = new ItemCollection<HiveJob>(); 231 else 232 this.HiveJobs.Clear(); 233 this.HiveJobs.Add(new HiveJob(experiment)); 234 } 235 144 236 145 protected override void OnPropertyChanged(PropertyChangedEventArgs e) { 237 146 base.OnPropertyChanged(e); … … 240 149 } 241 150 } 242 243 #region JobResultPoller Events 244 245 public void StartResultPolling() { 246 if (jobResultPoller == null) { 247 jobResultPoller = new JobResultPoller(HiveJobs, /*ApplicationConstants.ResultPollingInterval*/new TimeSpan(0, 0, 5)); //TODO: find a better place for ApplicationConstants 248 RegisterResultPollingEvents(); 249 } 250 251 if (!jobResultPoller.IsPolling) { 252 jobResultPoller.Start(); 253 } 254 } 255 256 public void StopResultPolling() { 257 if (jobResultPoller != null && jobResultPoller.IsPolling) { 258 jobResultPoller.Stop(); 259 } 260 } 261 262 private void RegisterResultPollingEvents() { 263 jobResultPoller.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobResultPoller_ExceptionOccured); 264 jobResultPoller.JobResultsReceived += new EventHandler<EventArgs<IEnumerable<LightweightJob>>>(jobResultPoller_JobResultReceived); 265 jobResultPoller.IsPollingChanged += new EventHandler(jobResultPoller_IsPollingChanged); 266 } 267 private void DeregisterResultPollingEvents() { 268 jobResultPoller.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(jobResultPoller_ExceptionOccured); 269 jobResultPoller.JobResultsReceived -= new EventHandler<EventArgs<IEnumerable<LightweightJob>>>(jobResultPoller_JobResultReceived); 270 jobResultPoller.IsPollingChanged -= new EventHandler(jobResultPoller_IsPollingChanged); 271 } 272 private void jobResultPoller_IsPollingChanged(object sender, EventArgs e) { 273 this.refreshAutomatically = jobResultPoller.IsPolling; 274 OnRefreshAutomaticallyChanged(); 275 } 276 private void jobResultPoller_JobResultReceived(object sender, EventArgs<IEnumerable<LightweightJob>> e) { 277 foreach (LightweightJob lightweightJob in e.Value) { 278 HiveJob hj = GetHiveJobById(lightweightJob.Id); 279 if (hj != null) { 280 DateTime lastJobDataUpdate = hj.Job.LastJobDataUpdate; 281 hj.UpdateFromLightweightJob(lightweightJob); 282 283 // lastJobDataUpdate equals DateTime.MinValue right after it was uploaded. When the first results are polled, this value is updated 284 if (lastJobDataUpdate != DateTime.MinValue && lastJobDataUpdate < hj.Job.LastJobDataUpdate) { 285 OptimizerJob optimizerJob = ExperimentManagerClient.LoadOptimizerJob(hj.Job.Id); 286 if (optimizerJob == null) { 287 // something bad happened to this job. bad job, BAAAD job! 288 } else { 289 // if the job is paused, download but don't integrate into parent optimizer (to avoid Prepare) 290 if (hj.Job.State == JobState.Paused) { 291 hj.OptimizerJob = optimizerJob; 292 } else { 293 if (lightweightJob.ParentJobId.HasValue) { 294 HiveJob parentHiveJob = GetHiveJobById(lightweightJob.ParentJobId.Value); 295 parentHiveJob.UpdateChildOptimizer(optimizerJob, hj.Job.Id); 296 } 297 } 298 } 299 } 300 } 301 } 302 GC.Collect(); // force GC, because .NET is too lazy here (deserialization takes a lot of memory) 303 if (AllJobsFinished()) { 304 this.ExecutionState = Core.ExecutionState.Stopped; 305 StopResultPolling(); 306 //OnStopped(); 307 } 308 UpdateTotalExecutionTime(); 309 UpdateStats(); 310 } 311 312 public HiveJob GetHiveJobById(Guid jobId) { 313 foreach (HiveJob job in HiveJobs) { 314 HiveJob hj = job.GetHiveJobByJobId(jobId); 315 if (hj != null) 316 return hj; 317 } 318 return null; 319 } 320 321 private void UpdateStats() { 322 var jobs = GetAllHiveJobs(); 323 this.JobCount = jobs.Count(); 324 this.CalculatingCount = jobs.Count(j => j.Job.State == JobState.Calculating); 325 this.FinishedCount = jobs.Count(j => j.Job.State == JobState.Finished); 326 } 327 328 public IEnumerable<HiveJob> GetAllHiveJobs() { 329 var jobs = new List<HiveJob>(); 330 foreach (HiveJob job in HiveJobs) { 331 jobs.AddRange(job.GetAllHiveJobs()); 332 } 333 return jobs; 334 } 335 336 public bool AllJobsFinished() { 337 return GetAllHiveJobs().All(j => j.Job.State == JobState.Finished 338 || j.Job.State == JobState.Aborted 339 || j.Job.State == JobState.Failed); 340 } 341 342 private void jobResultPoller_ExceptionOccured(object sender, EventArgs<Exception> e) { 343 //OnExceptionOccured(e.Value); 344 } 345 346 public void UpdateTotalExecutionTime() { 347 this.ExecutionTime = TimeSpan.FromMilliseconds(GetAllHiveJobs().Sum(x => x.Job.ExecutionTime.HasValue ? x.Job.ExecutionTime.Value.TotalMilliseconds : 0)); 348 } 349 #endregion 350 151 351 152 protected override void RaisePropertyChanged(string propertyName) { 352 153 if (!(propertyName == "ExecutionTime") … … 362 163 && HiveJobs.All(x => x.Job.DateFinished.HasValue && x.Job.DateCreated.HasValue); 363 164 } 165 166 public IEnumerable<HiveJob> GetAllHiveJobs() { 167 var jobs = new List<HiveJob>(); 168 foreach (HiveJob job in HiveJobs) { 169 jobs.AddRange(job.GetAllHiveJobs()); 170 } 171 return jobs; 172 } 173 174 public override string ToString() { 175 return Name; 176 } 177 178 public virtual void OnLoaded() { } 364 179 } 365 180 }
Note: See TracChangeset
for help on using the changeset viewer.