Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/27/11 15:20:23 (13 years ago)
Author:
cneumuel
Message:

#1233

  • finished experiment sharing
  • added role for executing privileged jobs
  • refreshing experiments in experimentManager does not delete already downloaded jobs
  • moved some properties from HiveExperiment into RefreshableHiveExperiment
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/RefreshableHiveExperiment.cs

    r6478 r6479  
    2525using System.Drawing;
    2626using System.Linq;
     27using HeuristicLab.Collections;
    2728using HeuristicLab.Common;
    2829using HeuristicLab.Core;
    2930
    3031namespace HeuristicLab.Clients.Hive {
    31   public class RefreshableHiveExperiment : IHiveItem, IDeepCloneable, IContent, IProgressReporter {
     32  public class RefreshableHiveExperiment : IHiveItem, IDeepCloneable, IContent, IProgressReporter, IComparable<RefreshableHiveExperiment> {
    3233    private JobResultPoller jobResultPoller;
    3334    private ConcurrentJobDownloader<ItemJob> jobDownloader;
     
    4445          if (hiveExperiment != null) DergisterHiveExperimentEvents();
    4546          hiveExperiment = value;
    46           if (hiveExperiment != null) RegisterHiveExperimentEvents();
     47          if (hiveExperiment != null) {
     48            RegisterHiveExperimentEvents();
     49            hiveExperiment_PropertyChanged(hiveExperiment, new PropertyChangedEventArgs("Id"));
     50          }
    4751          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();
    4888        }
    4989      }
     
    60100          }
    61101          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)) {
    63103              StartResultPolling();
    64104            }
     
    70110    }
    71111
    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
    73125    private bool isControllable = true;
    74126    public bool IsControllable {
    75127      get { return isControllable; }
    76       set {
     128      private set {
    77129        if (value != isControllable) {
    78130          isControllable = value;
    79131          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; }
    82180    }
    83181
     
    85183    public ILog Log {
    86184      get { return log; }
     185    }
     186
     187    public StateLogListList StateLogList {
     188      get { return new StateLogListList(this.GetAllHiveJobs().Select(x => x.StateLog)); }
    87189    }
    88190
     
    94196      this.jobDownloader = new ConcurrentJobDownloader<ItemJob>(2, 2);
    95197      this.jobDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured);
     198      this.HiveJobs = new ItemCollection<HiveJob>();
    96199    }
    97200    public RefreshableHiveExperiment(HiveExperiment hiveExperiment) {
     
    101204      this.jobDownloader = new ConcurrentJobDownloader<ItemJob>(2, 2);
    102205      this.jobDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured);
     206      this.HiveJobs = new ItemCollection<HiveJob>();
    103207    }
    104208    protected RefreshableHiveExperiment(RefreshableHiveExperiment original, Cloner cloner) {
     
    110214      this.jobDownloader = new ConcurrentJobDownloader<ItemJob>(2, 2);
    111215      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;
    112219    }
    113220    public IDeepCloneable Clone(Cloner cloner) {
     
    118225    }
    119226    #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   
    132228    #region JobResultPoller Events
    133229
     
    212308      GC.Collect(); // force GC, because .NET is too lazy here (deserialization takes a lot of memory)
    213309      if (AllJobsFinished()) {
    214         hiveExperiment.ExecutionState = Core.ExecutionState.Stopped;
     310        this.ExecutionState = Core.ExecutionState.Stopped;
    215311        StopResultPolling();
    216312      }
     
    221317
    222318    public HiveJob GetHiveJobById(Guid jobId) {
    223       foreach (HiveJob job in hiveExperiment.HiveJobs) {
     319      foreach (HiveJob job in this.HiveJobs) {
    224320        var hj = job.GetHiveJobByJobId(jobId);
    225321        if (hj != null)
     
    229325    }
    230326    private void UpdateStatistics() {
    231       var jobs = hiveExperiment.GetAllHiveJobs();
     327      var jobs = this.GetAllHiveJobs();
    232328      hiveExperiment.JobCount = jobs.Count();
    233329      hiveExperiment.CalculatingCount = jobs.Count(j => j.Job.State == JobState.Calculating);
     
    237333
    238334    public bool AllJobsFinished() {
    239       return hiveExperiment.GetAllHiveJobs().All(j => (j.Job.State == JobState.Finished
     335      return this.GetAllHiveJobs().All(j => (j.Job.State == JobState.Finished
    240336                                                   || j.Job.State == JobState.Aborted
    241337                                                   || j.Job.State == JobState.Failed)
     
    250346    }
    251347    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));
    253349    }
    254350    #endregion
     
    256352    #region HiveExperiment Events
    257353    private void RegisterHiveExperimentEvents() {
    258       hiveExperiment.HiveJobsChanged += new EventHandler(hiveExperiment_HiveJobsChanged);
    259       hiveExperiment.ToStringChanged += new EventHandler(hiveExperiment_ToStringChanged);
     354      hiveExperiment.ToStringChanged += new EventHandler(OnToStringChanged);
    260355      hiveExperiment.PropertyChanged += new PropertyChangedEventHandler(hiveExperiment_PropertyChanged);
    261356      hiveExperiment.ItemImageChanged += new EventHandler(hiveExperiment_ItemImageChanged);
    262357      hiveExperiment.ModifiedChanged += new EventHandler(hiveExperiment_ModifiedChanged);
    263       hiveExperiment.IsProgressingChanged += new EventHandler(hiveExperiment_IsProgressingChanged);
    264       hiveExperiment.Loaded += new EventHandler(hiveExperiment_Loaded);
    265358    }
    266359
    267360    private void DergisterHiveExperimentEvents() {
    268       hiveExperiment.HiveJobsChanged -= new EventHandler(hiveExperiment_HiveJobsChanged);
    269       hiveExperiment.ToStringChanged -= new EventHandler(hiveExperiment_ToStringChanged);
     361      hiveExperiment.ToStringChanged -= new EventHandler(OnToStringChanged);
    270362      hiveExperiment.PropertyChanged -= new PropertyChangedEventHandler(hiveExperiment_PropertyChanged);
    271363      hiveExperiment.ItemImageChanged -= new EventHandler(hiveExperiment_ItemImageChanged);
    272364      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       }
    283365    }
    284366    #endregion
     
    311393    public event PropertyChangedEventHandler PropertyChanged;
    312394    private void hiveExperiment_PropertyChanged(object sender, PropertyChangedEventArgs e) {
     395      this.IsSharable = hiveExperiment.Permission == Permission.Full;
     396      this.IsControllable = hiveExperiment.Permission == Permission.Full;
     397
    313398      var handler = PropertyChanged;
    314399      if (handler != null) handler(sender, e);
     
    316401
    317402    public event EventHandler ToStringChanged;
    318     private void hiveExperiment_ToStringChanged(object sender, EventArgs e) {
     403    private void OnToStringChanged(object sender, EventArgs e) {
    319404      var handler = ToStringChanged;
    320405      if (handler != null) handler(this, e);
     
    322407
    323408    public event EventHandler IsProgressingChanged;
    324     private void hiveExperiment_IsProgressingChanged(object sender, EventArgs e) {
     409    protected virtual void OnIsProgressingChanged() {
    325410      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);
    327418    }
    328419
     
    330421    private void OnIsControllableChanged() {
    331422      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;
    332435      if (handler != null) handler(this, EventArgs.Empty);
    333436    }
     
    351454      if (handler != null) handler(this, EventArgs.Empty);
    352455    }
     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    }
    353468    #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    }
    354554
    355555    public Guid Id {
     
    375575      get { return hiveExperiment.ItemVersion; }
    376576    }
    377    
    378     #region IProgressReporter Members
    379     public IProgress Progress {
    380       get { return HiveExperiment.Progress; }
    381     }
    382 
    383     public bool IsProgressing {
    384       get { return HiveExperiment.IsProgressing; }
    385     }
    386     #endregion
    387577
    388578    public override string ToString() {
    389579      return string.Format("{0} {1}", HiveExperiment.DateCreated.ToString("MM.dd.yyyy HH:mm"), HiveExperiment.ToString());
    390580    }
     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    }
    391600  }
    392601}
Note: See TracChangeset for help on using the changeset viewer.