source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ServiceClients/HiveExperiment.cs @ 6426

Last change on this file since 6426 was 6426, checked in by ascheibe, 8 years ago

#1233 removed useLocalPlugins

File size: 8.4 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Linq;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29
30namespace HeuristicLab.Clients.Hive {
31  public partial class HiveExperiment : IDeepCloneable, IContent, IProgressReporter {
32
33    private bool isPrivileged;
34    public bool IsPrivileged {
35      get { return isPrivileged; }
36      set { isPrivileged = value; }
37    }
38
39    private ExecutionState executionState;
40    public ExecutionState ExecutionState {
41      get { return executionState; }
42      internal set {
43        if (executionState != value) {
44          executionState = value;
45          OnExecutionStateChanged();
46        }
47      }
48    }
49
50    private TimeSpan executionTime;
51    public TimeSpan ExecutionTime {
52      get { return executionTime; }
53      internal set {
54        if (executionTime != value) {
55          executionTime = value;
56          OnExecutionTimeChanged();
57        }
58      }
59    }
60
61    private ItemCollection<HiveJob> hiveJobs;
62    public ItemCollection<HiveJob> HiveJobs {
63      get { return hiveJobs; }
64      set {
65        if (hiveJobs != value) {
66          if (hiveJobs != null) DeregisterHiveJobsEvents();
67          hiveJobs = value;
68          if (hiveJobs != null) RegisterHiveJobsEvents();
69          OnHiveJobsChanged();
70        }
71      }
72    }
73
74    private bool isProgressing;
75    public bool IsProgressing {
76      get { return isProgressing; }
77      set {
78        if (isProgressing != value) {
79          isProgressing = value;
80          OnIsProgressingChanged();
81        }
82      }
83    }
84
85    private IProgress progress;
86    public IProgress Progress {
87      get { return progress; }
88      set { this.progress = value; }
89    }
90
91    public StateLogListList StateLogList {
92      get { return new StateLogListList(this.GetAllHiveJobs().Select(x => x.StateLog)); }
93    }
94
95    #region Constructors and Cloning
96    public HiveExperiment() {
97      this.ResourceNames = "HEAL";
98      this.HiveJobs = new ItemCollection<HiveJob>();
99      this.DateCreated = DateTime.Now;
100    }
101
102    protected HiveExperiment(HiveExperiment original, Cloner cloner) {
103      cloner.RegisterClonedObject(original, this);
104      this.OwnerUserId = original.OwnerUserId;
105      this.DateCreated = original.DateCreated;
106      this.ResourceNames = original.ResourceNames;
107      this.LastAccessed = original.LastAccessed;
108      this.Name = original.Name;
109      this.Description = original.Description;
110      this.Id = original.Id;
111      this.HiveJobs = cloner.Clone(original.HiveJobs);
112      this.ExecutionTime = original.ExecutionTime;
113      this.IsPrivileged = original.IsPrivileged;
114    }
115    public override IDeepCloneable Clone(Cloner cloner) {
116      return new HiveExperiment(this, cloner);
117    }
118    #endregion
119
120    #region Events
121    public event EventHandler ExecutionTimeChanged;
122    protected virtual void OnExecutionTimeChanged() {
123      var handler = ExecutionTimeChanged;
124      if (handler != null) handler(this, EventArgs.Empty);
125    }
126
127    public event EventHandler ExecutionStateChanged;
128    protected virtual void OnExecutionStateChanged() {
129      var handler = ExecutionStateChanged;
130      if (handler != null) handler(this, EventArgs.Empty);
131    }
132
133    public event EventHandler HiveJobsChanged;
134    protected virtual void OnHiveJobsChanged() {
135      var handler = HiveJobsChanged;
136      if (handler != null) handler(this, EventArgs.Empty);
137    }
138
139    public event EventHandler IsProgressingChanged;
140    protected virtual void OnIsProgressingChanged() {
141      var handler = IsProgressingChanged;
142      if (handler != null) handler(this, EventArgs.Empty);
143    }
144
145    public event EventHandler Loaded;
146    public virtual void OnLoaded() {
147      var handler = Loaded;
148      if (handler != null) handler(this, EventArgs.Empty);
149    }
150
151    public event EventHandler<CollectionItemsChangedEventArgs<HiveJob>> HiveJobsAdded;
152    private void OnHiveJobsAdded(CollectionItemsChangedEventArgs<HiveJob> e) {
153      var handler = HiveJobsAdded;
154      if (handler != null) handler(this, e);
155    }
156
157    public event EventHandler<CollectionItemsChangedEventArgs<HiveJob>> HiveJobsRemoved;
158    private void OnHiveJobsRemoved(CollectionItemsChangedEventArgs<HiveJob> e) {
159      var handler = HiveJobsRemoved;
160      if (handler != null) handler(this, e);
161    }
162
163    public event EventHandler<CollectionItemsChangedEventArgs<HiveJob>> HiveJobsReset;
164    private void OnHiveJobsReset(CollectionItemsChangedEventArgs<HiveJob> e) {
165      var handler = HiveJobsReset;
166      if (handler != null) handler(this, e);
167    }
168
169    public event EventHandler StateLogListChanged;
170    private void OnStateLogListChanged() {
171      var handler = StateLogListChanged;
172      if (handler != null) handler(this, EventArgs.Empty);
173    }
174    #endregion
175
176    #region HiveJobs Events
177    private void RegisterHiveJobsEvents() {
178      this.hiveJobs.ItemsAdded += new CollectionItemsChangedEventHandler<HiveJob>(hiveJobs_ItemsAdded);
179      this.hiveJobs.ItemsRemoved += new CollectionItemsChangedEventHandler<HiveJob>(hiveJobs_ItemsRemoved);
180      this.hiveJobs.CollectionReset += new CollectionItemsChangedEventHandler<HiveJob>(hiveJobs_CollectionReset);
181    }
182
183    private void DeregisterHiveJobsEvents() {
184      this.hiveJobs.ItemsAdded -= new CollectionItemsChangedEventHandler<HiveJob>(hiveJobs_ItemsAdded);
185      this.hiveJobs.ItemsRemoved -= new CollectionItemsChangedEventHandler<HiveJob>(hiveJobs_ItemsRemoved);
186      this.hiveJobs.CollectionReset -= new CollectionItemsChangedEventHandler<HiveJob>(hiveJobs_CollectionReset);
187    }
188
189    private void hiveJobs_CollectionReset(object sender, CollectionItemsChangedEventArgs<HiveJob> e) {
190      foreach (var item in e.Items) {
191        item.StateLogChanged -= new EventHandler(item_StateLogChanged);
192      }
193      OnHiveJobsReset(e);
194    }
195
196    private void hiveJobs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<HiveJob> e) {
197      foreach (var item in e.Items) {
198        item.StateLogChanged -= new EventHandler(item_StateLogChanged);
199      }
200      OnHiveJobsRemoved(e);
201    }
202
203    private void hiveJobs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<HiveJob> e) {
204      foreach (var item in e.Items) {
205        item.StateLogChanged += new EventHandler(item_StateLogChanged);
206      }
207      OnHiveJobsAdded(e);
208    }
209
210    private void item_StateLogChanged(object sender, EventArgs e) {
211      OnStateLogListChanged();
212    }
213    #endregion
214
215    protected override void OnPropertyChanged(PropertyChangedEventArgs e) {
216      base.OnPropertyChanged(e);
217      if (e.PropertyName == "Name") {
218        OnToStringChanged();
219      }
220    }
221
222    protected override void RaisePropertyChanged(string propertyName) {
223      if (!(propertyName == "ExecutionTime")
224        && !(propertyName == "JobCount")
225        && !(propertyName == "CalculatingCount")
226        && !(propertyName == "FinishedCount")) {
227        base.RaisePropertyChanged(propertyName);
228      }
229    }
230
231    public bool IsFinished() {
232      return HiveJobs != null
233        && HiveJobs.All(x => x.Job.DateFinished.HasValue && x.Job.DateCreated.HasValue);
234    }
235
236    public IEnumerable<HiveJob> GetAllHiveJobs() {
237      if (hiveJobs == null) return Enumerable.Empty<HiveJob>();
238
239      var jobs = new List<HiveJob>();
240      foreach (HiveJob job in HiveJobs) {
241        jobs.AddRange(job.GetAllHiveJobs());
242      }
243      return jobs;
244    }
245
246    public override string ToString() {
247      return Name;
248    }
249  }
250}
Note: See TracBrowser for help on using the repository browser.