Changeset 6479 for branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/RefreshableHiveExperiment.cs
- Timestamp:
- 06/27/11 15:20:23 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/RefreshableHiveExperiment.cs
r6478 r6479 25 25 using System.Drawing; 26 26 using System.Linq; 27 using HeuristicLab.Collections; 27 28 using HeuristicLab.Common; 28 29 using HeuristicLab.Core; 29 30 30 31 namespace HeuristicLab.Clients.Hive { 31 public class RefreshableHiveExperiment : IHiveItem, IDeepCloneable, IContent, IProgressReporter {32 public class RefreshableHiveExperiment : IHiveItem, IDeepCloneable, IContent, IProgressReporter, IComparable<RefreshableHiveExperiment> { 32 33 private JobResultPoller jobResultPoller; 33 34 private ConcurrentJobDownloader<ItemJob> jobDownloader; … … 44 45 if (hiveExperiment != null) DergisterHiveExperimentEvents(); 45 46 hiveExperiment = value; 46 if (hiveExperiment != null) RegisterHiveExperimentEvents(); 47 if (hiveExperiment != null) { 48 RegisterHiveExperimentEvents(); 49 hiveExperiment_PropertyChanged(hiveExperiment, new PropertyChangedEventArgs("Id")); 50 } 47 51 OnHiveExperimentChanged(); 52 OnToStringChanged(this, EventArgs.Empty); 53 } 54 } 55 } 56 57 private ItemCollection<HiveJob> hiveJobs; 58 public ItemCollection<HiveJob> HiveJobs { 59 get { return hiveJobs; } 60 set { 61 if (hiveJobs != value) { 62 if (hiveJobs != null) DeregisterHiveJobsEvents(); 63 hiveJobs = value; 64 if (hiveJobs != null) RegisterHiveJobsEvents(); 65 OnHiveJobsChanged(); 66 } 67 } 68 } 69 70 private ExecutionState executionState; 71 public ExecutionState ExecutionState { 72 get { return executionState; } 73 internal set { 74 if (executionState != value) { 75 executionState = value; 76 OnExecutionStateChanged(); 77 } 78 } 79 } 80 81 private TimeSpan executionTime; 82 public TimeSpan ExecutionTime { 83 get { return executionTime; } 84 internal set { 85 if (executionTime != value) { 86 executionTime = value; 87 OnExecutionTimeChanged(); 48 88 } 49 89 } … … 60 100 } 61 101 if (RefreshAutomatically) { 62 if ( hiveExperiment.HiveJobs != null && hiveExperiment.HiveJobs.Count > 0 && (jobResultPoller == null || !jobResultPoller.IsPolling)) {102 if (this.HiveJobs != null && this.HiveJobs.Count > 0 && (jobResultPoller == null || !jobResultPoller.IsPolling)) { 63 103 StartResultPolling(); 64 104 } … … 70 110 } 71 111 72 // if true, all control buttons should be enabled. otherwise disabled (used for HiveEngine) 112 // indicates if download button is enabled 113 private bool isDownloadable = true; 114 public bool IsDownloadable { 115 get { return isDownloadable; } 116 set { 117 if (value != isDownloadable) { 118 isDownloadable = value; 119 OnIsDownloadableChanged(); 120 } 121 } 122 } 123 124 // if true, all control buttons should be enabled. otherwise disabled 73 125 private bool isControllable = true; 74 126 public bool IsControllable { 75 127 get { return isControllable; } 76 set {128 private set { 77 129 if (value != isControllable) { 78 130 isControllable = value; 79 131 OnIsControllableChanged(); 80 } 81 } 132 if (this.hiveJobs != null) { 133 foreach (var hiveJob in this.hiveJobs) { 134 hiveJob.IsControllable = value; 135 } 136 } 137 } 138 } 139 } 140 141 // indicates if a user is allowed to share this experiment 142 private bool isSharable = true; 143 public bool IsSharable { 144 get { return isSharable; } 145 private set { 146 if (value != isSharable) { 147 isSharable = value; 148 OnIsSharableChanged(); 149 } 150 } 151 } 152 153 // may execute jobs with privileged permissions on slaves 154 private bool isAllowedPrivileged = true; 155 public bool IsAllowedPrivileged { 156 get { return isAllowedPrivileged; } 157 set { 158 if (value != isAllowedPrivileged) { 159 isAllowedPrivileged = value; 160 OnIsAllowedPrivilegedChanged(); 161 } 162 } 163 } 164 165 private bool isProgressing; 166 public bool IsProgressing { 167 get { return isProgressing; } 168 set { 169 if (isProgressing != value) { 170 isProgressing = value; 171 OnIsProgressingChanged(); 172 } 173 } 174 } 175 176 private IProgress progress; 177 public IProgress Progress { 178 get { return progress; } 179 set { this.progress = value; } 82 180 } 83 181 … … 85 183 public ILog Log { 86 184 get { return log; } 185 } 186 187 public StateLogListList StateLogList { 188 get { return new StateLogListList(this.GetAllHiveJobs().Select(x => x.StateLog)); } 87 189 } 88 190 … … 94 196 this.jobDownloader = new ConcurrentJobDownloader<ItemJob>(2, 2); 95 197 this.jobDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured); 198 this.HiveJobs = new ItemCollection<HiveJob>(); 96 199 } 97 200 public RefreshableHiveExperiment(HiveExperiment hiveExperiment) { … … 101 204 this.jobDownloader = new ConcurrentJobDownloader<ItemJob>(2, 2); 102 205 this.jobDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured); 206 this.HiveJobs = new ItemCollection<HiveJob>(); 103 207 } 104 208 protected RefreshableHiveExperiment(RefreshableHiveExperiment original, Cloner cloner) { … … 110 214 this.jobDownloader = new ConcurrentJobDownloader<ItemJob>(2, 2); 111 215 this.jobDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured); 216 this.HiveJobs = cloner.Clone(original.HiveJobs); 217 this.ExecutionTime = original.ExecutionTime; 218 this.ExecutionState = original.ExecutionState; 112 219 } 113 220 public IDeepCloneable Clone(Cloner cloner) { … … 118 225 } 119 226 #endregion 120 121 private void hiveExperiment_HiveJobsChanged(object sender, EventArgs e) { 122 if (jobResultPoller != null && jobResultPoller.IsPolling) { 123 jobResultPoller.Stop(); 124 DeregisterResultPollingEvents(); 125 } 126 if (hiveExperiment.HiveJobs != null && hiveExperiment.HiveJobs.Count > 0 && hiveExperiment.GetAllHiveJobs().All(x => x.Job.Id != Guid.Empty)) { 127 if (this.RefreshAutomatically) 128 StartResultPolling(); 129 } 130 } 131 227 132 228 #region JobResultPoller Events 133 229 … … 212 308 GC.Collect(); // force GC, because .NET is too lazy here (deserialization takes a lot of memory) 213 309 if (AllJobsFinished()) { 214 hiveExperiment.ExecutionState = Core.ExecutionState.Stopped;310 this.ExecutionState = Core.ExecutionState.Stopped; 215 311 StopResultPolling(); 216 312 } … … 221 317 222 318 public HiveJob GetHiveJobById(Guid jobId) { 223 foreach (HiveJob job in hiveExperiment.HiveJobs) {319 foreach (HiveJob job in this.HiveJobs) { 224 320 var hj = job.GetHiveJobByJobId(jobId); 225 321 if (hj != null) … … 229 325 } 230 326 private void UpdateStatistics() { 231 var jobs = hiveExperiment.GetAllHiveJobs();327 var jobs = this.GetAllHiveJobs(); 232 328 hiveExperiment.JobCount = jobs.Count(); 233 329 hiveExperiment.CalculatingCount = jobs.Count(j => j.Job.State == JobState.Calculating); … … 237 333 238 334 public bool AllJobsFinished() { 239 return hiveExperiment.GetAllHiveJobs().All(j => (j.Job.State == JobState.Finished335 return this.GetAllHiveJobs().All(j => (j.Job.State == JobState.Finished 240 336 || j.Job.State == JobState.Aborted 241 337 || j.Job.State == JobState.Failed) … … 250 346 } 251 347 public void UpdateTotalExecutionTime() { 252 hiveExperiment.ExecutionTime = TimeSpan.FromMilliseconds(hiveExperiment.GetAllHiveJobs().Sum(x => x.Job.ExecutionTime.TotalMilliseconds));348 this.ExecutionTime = TimeSpan.FromMilliseconds(this.GetAllHiveJobs().Sum(x => x.Job.ExecutionTime.TotalMilliseconds)); 253 349 } 254 350 #endregion … … 256 352 #region HiveExperiment Events 257 353 private void RegisterHiveExperimentEvents() { 258 hiveExperiment.HiveJobsChanged += new EventHandler(hiveExperiment_HiveJobsChanged); 259 hiveExperiment.ToStringChanged += new EventHandler(hiveExperiment_ToStringChanged); 354 hiveExperiment.ToStringChanged += new EventHandler(OnToStringChanged); 260 355 hiveExperiment.PropertyChanged += new PropertyChangedEventHandler(hiveExperiment_PropertyChanged); 261 356 hiveExperiment.ItemImageChanged += new EventHandler(hiveExperiment_ItemImageChanged); 262 357 hiveExperiment.ModifiedChanged += new EventHandler(hiveExperiment_ModifiedChanged); 263 hiveExperiment.IsProgressingChanged += new EventHandler(hiveExperiment_IsProgressingChanged);264 hiveExperiment.Loaded += new EventHandler(hiveExperiment_Loaded);265 358 } 266 359 267 360 private void DergisterHiveExperimentEvents() { 268 hiveExperiment.HiveJobsChanged -= new EventHandler(hiveExperiment_HiveJobsChanged); 269 hiveExperiment.ToStringChanged -= new EventHandler(hiveExperiment_ToStringChanged); 361 hiveExperiment.ToStringChanged -= new EventHandler(OnToStringChanged); 270 362 hiveExperiment.PropertyChanged -= new PropertyChangedEventHandler(hiveExperiment_PropertyChanged); 271 363 hiveExperiment.ItemImageChanged -= new EventHandler(hiveExperiment_ItemImageChanged); 272 364 hiveExperiment.ModifiedChanged -= new EventHandler(hiveExperiment_ModifiedChanged); 273 hiveExperiment.IsProgressingChanged -= new EventHandler(hiveExperiment_IsProgressingChanged);274 hiveExperiment.Loaded -= new EventHandler(hiveExperiment_Loaded);275 }276 277 private void hiveExperiment_Loaded(object sender, EventArgs e) {278 this.UpdateTotalExecutionTime();279 280 if (hiveExperiment.ExecutionState != ExecutionState.Stopped) {281 this.RefreshAutomatically = true;282 }283 365 } 284 366 #endregion … … 311 393 public event PropertyChangedEventHandler PropertyChanged; 312 394 private void hiveExperiment_PropertyChanged(object sender, PropertyChangedEventArgs e) { 395 this.IsSharable = hiveExperiment.Permission == Permission.Full; 396 this.IsControllable = hiveExperiment.Permission == Permission.Full; 397 313 398 var handler = PropertyChanged; 314 399 if (handler != null) handler(sender, e); … … 316 401 317 402 public event EventHandler ToStringChanged; 318 private void hiveExperiment_ToStringChanged(object sender, EventArgs e) {403 private void OnToStringChanged(object sender, EventArgs e) { 319 404 var handler = ToStringChanged; 320 405 if (handler != null) handler(this, e); … … 322 407 323 408 public event EventHandler IsProgressingChanged; 324 pr ivate void hiveExperiment_IsProgressingChanged(object sender, EventArgs e) {409 protected virtual void OnIsProgressingChanged() { 325 410 var handler = IsProgressingChanged; 326 if (handler != null) handler(sender, e); 411 if (handler != null) handler(this, EventArgs.Empty); 412 } 413 414 public event EventHandler IsDownloadableChanged; 415 private void OnIsDownloadableChanged() { 416 var handler = IsDownloadableChanged; 417 if (handler != null) handler(this, EventArgs.Empty); 327 418 } 328 419 … … 330 421 private void OnIsControllableChanged() { 331 422 var handler = IsControllableChanged; 423 if (handler != null) handler(this, EventArgs.Empty); 424 } 425 426 public event EventHandler IsSharableChanged; 427 private void OnIsSharableChanged() { 428 var handler = IsSharableChanged; 429 if (handler != null) handler(this, EventArgs.Empty); 430 } 431 432 public event EventHandler IsAllowedPrivilegedChanged; 433 private void OnIsAllowedPrivilegedChanged() { 434 var handler = IsAllowedPrivilegedChanged; 332 435 if (handler != null) handler(this, EventArgs.Empty); 333 436 } … … 351 454 if (handler != null) handler(this, EventArgs.Empty); 352 455 } 456 457 public event EventHandler ExecutionTimeChanged; 458 protected virtual void OnExecutionTimeChanged() { 459 var handler = ExecutionTimeChanged; 460 if (handler != null) handler(this, EventArgs.Empty); 461 } 462 463 public event EventHandler ExecutionStateChanged; 464 protected virtual void OnExecutionStateChanged() { 465 var handler = ExecutionStateChanged; 466 if (handler != null) handler(this, EventArgs.Empty); 467 } 353 468 #endregion 469 470 #region HiveJobs Events 471 private void RegisterHiveJobsEvents() { 472 this.hiveJobs.ItemsAdded += new CollectionItemsChangedEventHandler<HiveJob>(hiveJobs_ItemsAdded); 473 this.hiveJobs.ItemsRemoved += new CollectionItemsChangedEventHandler<HiveJob>(hiveJobs_ItemsRemoved); 474 this.hiveJobs.CollectionReset += new CollectionItemsChangedEventHandler<HiveJob>(hiveJobs_CollectionReset); 475 } 476 477 private void DeregisterHiveJobsEvents() { 478 this.hiveJobs.ItemsAdded -= new CollectionItemsChangedEventHandler<HiveJob>(hiveJobs_ItemsAdded); 479 this.hiveJobs.ItemsRemoved -= new CollectionItemsChangedEventHandler<HiveJob>(hiveJobs_ItemsRemoved); 480 this.hiveJobs.CollectionReset -= new CollectionItemsChangedEventHandler<HiveJob>(hiveJobs_CollectionReset); 481 } 482 483 private void hiveJobs_CollectionReset(object sender, CollectionItemsChangedEventArgs<HiveJob> e) { 484 foreach (var item in e.Items) { 485 item.StateLogChanged -= new EventHandler(item_StateLogChanged); 486 } 487 OnHiveJobsReset(e); 488 } 489 490 private void hiveJobs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<HiveJob> e) { 491 foreach (var item in e.Items) { 492 item.StateLogChanged -= new EventHandler(item_StateLogChanged); 493 } 494 OnHiveJobsRemoved(e); 495 } 496 497 private void hiveJobs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<HiveJob> e) { 498 foreach (var item in e.Items) { 499 item.StateLogChanged += new EventHandler(item_StateLogChanged); 500 item.IsControllable = this.IsControllable; 501 } 502 OnHiveJobsAdded(e); 503 } 504 505 private void item_StateLogChanged(object sender, EventArgs e) { 506 OnStateLogListChanged(); 507 } 508 #endregion 509 510 public event EventHandler HiveJobsChanged; 511 protected virtual void OnHiveJobsChanged() { 512 if (jobResultPoller != null && jobResultPoller.IsPolling) { 513 jobResultPoller.Stop(); 514 DeregisterResultPollingEvents(); 515 } 516 if (this.HiveJobs != null && this.HiveJobs.Count > 0 && this.GetAllHiveJobs().All(x => x.Job.Id != Guid.Empty)) { 517 if (this.RefreshAutomatically) 518 StartResultPolling(); 519 } 520 521 var handler = HiveJobsChanged; 522 if (handler != null) handler(this, EventArgs.Empty); 523 } 524 525 public event EventHandler Loaded; 526 public virtual void OnLoaded() { 527 this.UpdateTotalExecutionTime(); 528 529 if (this.ExecutionState != ExecutionState.Stopped) { 530 this.RefreshAutomatically = true; 531 } 532 533 var handler = Loaded; 534 if (handler != null) handler(this, EventArgs.Empty); 535 } 536 537 public event EventHandler<CollectionItemsChangedEventArgs<HiveJob>> HiveJobsAdded; 538 private void OnHiveJobsAdded(CollectionItemsChangedEventArgs<HiveJob> e) { 539 var handler = HiveJobsAdded; 540 if (handler != null) handler(this, e); 541 } 542 543 public event EventHandler<CollectionItemsChangedEventArgs<HiveJob>> HiveJobsRemoved; 544 private void OnHiveJobsRemoved(CollectionItemsChangedEventArgs<HiveJob> e) { 545 var handler = HiveJobsRemoved; 546 if (handler != null) handler(this, e); 547 } 548 549 public event EventHandler<CollectionItemsChangedEventArgs<HiveJob>> HiveJobsReset; 550 private void OnHiveJobsReset(CollectionItemsChangedEventArgs<HiveJob> e) { 551 var handler = HiveJobsReset; 552 if (handler != null) handler(this, e); 553 } 354 554 355 555 public Guid Id { … … 375 575 get { return hiveExperiment.ItemVersion; } 376 576 } 377 378 #region IProgressReporter Members379 public IProgress Progress {380 get { return HiveExperiment.Progress; }381 }382 383 public bool IsProgressing {384 get { return HiveExperiment.IsProgressing; }385 }386 #endregion387 577 388 578 public override string ToString() { 389 579 return string.Format("{0} {1}", HiveExperiment.DateCreated.ToString("MM.dd.yyyy HH:mm"), HiveExperiment.ToString()); 390 580 } 581 582 public bool IsFinished() { 583 return HiveJobs != null 584 && HiveJobs.All(x => x.Job.DateFinished.HasValue && x.Job.DateCreated.HasValue); 585 } 586 587 public IEnumerable<HiveJob> GetAllHiveJobs() { 588 if (hiveJobs == null) return Enumerable.Empty<HiveJob>(); 589 590 var jobs = new List<HiveJob>(); 591 foreach (HiveJob job in HiveJobs) { 592 jobs.AddRange(job.GetAllHiveJobs()); 593 } 594 return jobs; 595 } 596 597 public int CompareTo(RefreshableHiveExperiment other) { 598 return this.ToString().CompareTo(other.ToString()); 599 } 391 600 } 392 601 }
Note: See TracChangeset
for help on using the changeset viewer.