source: branches/Async/HeuristicLab.Clients.OKB/3.3/RunCreation/OKBAlgorithm.cs @ 13349

Last change on this file since 13349 was 13349, checked in by jkarder, 4 years ago

#2258: added StartAsync to IExecutable

File size: 16.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Drawing;
25using System.IO;
26using System.Linq;
27using System.Threading;
28using System.Threading.Tasks;
29using HeuristicLab.Clients.Access;
30using HeuristicLab.Collections;
31using HeuristicLab.Common;
32using HeuristicLab.Core;
33using HeuristicLab.Optimization;
34using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
35using HeuristicLab.Persistence.Default.Xml;
36
37namespace HeuristicLab.Clients.OKB.RunCreation {
38  [Item("OKB Algorithm", "An algorithm which is stored in the OKB.")]
39  [Creatable(CreatableAttribute.Categories.TestingAndAnalysisOKB, Priority = 100)]
40  [StorableClass]
41  public sealed class OKBAlgorithm : Item, IAlgorithm, IStorableContent {
42    public string Filename { get; set; }
43
44    private long algorithmId;
45    public long AlgorithmId {
46      get { return algorithmId; }
47    }
48    private IAlgorithm algorithm;
49    private IAlgorithm Algorithm {
50      get { return algorithm; }
51      set {
52        if (value == null) throw new ArgumentNullException("Algorithm", "Algorithm cannot be null.");
53        if (value != algorithm) {
54          CancelEventArgs<string> e = new CancelEventArgs<string>(value.Name);
55          OnNameChanging(e);
56          if (!e.Cancel) {
57            IProblem problem = algorithm.Problem;
58            DeregisterAlgorithmEvents();
59            algorithm = value;
60            RegisterAlgorithmEvents();
61
62            OnToStringChanged();
63            OnItemImageChanged();
64            OnNameChanged();
65            OnDescriptionChanged();
66            OnAlgorithmChanged();
67            OnStoreAlgorithmInEachRunChanged();
68
69            try {
70              algorithm.Problem = problem;
71            }
72            catch (ArgumentException) {
73              algorithm.Problem = null;
74            }
75            algorithm.Prepare(true);
76          }
77        }
78      }
79    }
80
81    public IEnumerable<IOptimizer> NestedOptimizers {
82      get {
83        // inner algorithm cannot be accessed directly
84        return Enumerable.Empty<IOptimizer>();
85      }
86    }
87
88    public override Image ItemImage {
89      get { return Algorithm.ItemImage; }
90    }
91
92    public static new Image StaticItemImage {
93      get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; }
94    }
95
96    public string Name {
97      get { return Algorithm.Name; }
98      set { throw new NotSupportedException("Name cannot be changed."); }
99    }
100    public string Description {
101      get { return Algorithm.Description; }
102      set { throw new NotSupportedException("Description cannot be changed."); }
103    }
104    public bool CanChangeName {
105      get { return false; }
106    }
107    public bool CanChangeDescription {
108      get { return false; }
109    }
110
111    public IKeyedItemCollection<string, IParameter> Parameters {
112      get { return Algorithm.Parameters; }
113    }
114
115    public ExecutionState ExecutionState {
116      get { return Algorithm.ExecutionState; }
117    }
118    public TimeSpan ExecutionTime {
119      get { return Algorithm.ExecutionTime; }
120    }
121
122    public Type ProblemType {
123      get { return Algorithm.ProblemType; }
124    }
125    public IProblem Problem {
126      get { return Algorithm.Problem; }
127      set { Algorithm.Problem = value; }
128    }
129
130    public ResultCollection Results {
131      get { return Algorithm.Results; }
132    }
133
134    private RunCollection runs;
135    public RunCollection Runs {
136      get { return runs; }
137    }
138    private bool storeRunsAutomatically;
139    public bool StoreRunsAutomatically {
140      get { return storeRunsAutomatically; }
141      set {
142        if (value != storeRunsAutomatically) {
143          storeRunsAutomatically = value;
144          OnStoreRunsAutomaticallyChanged();
145        }
146      }
147    }
148    public bool StoreAlgorithmInEachRun {
149      get { return Algorithm.StoreAlgorithmInEachRun; }
150      set { Algorithm.StoreAlgorithmInEachRun = value; }
151    }
152
153    #region Persistence Properties
154    [Storable]
155    private Guid UserId;
156
157    [Storable(Name = "AlgorithmId")]
158    private long StorableAlgorithmId {
159      get { return algorithmId; }
160      set { algorithmId = value; }
161    }
162    [Storable(Name = "Algorithm")]
163    private IAlgorithm StorableAlgorithm {
164      get { return algorithm; }
165      set {
166        algorithm = value;
167        RegisterAlgorithmEvents();
168      }
169    }
170    [Storable(Name = "Runs")]
171    private RunCollection StorableRuns {
172      get { return runs; }
173      set {
174        runs = value;
175        RegisterRunsEvents();
176      }
177    }
178    [Storable(Name = "StoreRunsAutomatically")]
179    private bool StorableStoreRunsAutomatically {
180      get { return storeRunsAutomatically; }
181      set { storeRunsAutomatically = value; }
182    }
183    #endregion
184
185    [StorableConstructor]
186    private OKBAlgorithm(bool deserializing) : base(deserializing) { }
187    private OKBAlgorithm(OKBAlgorithm original, Cloner cloner)
188      : base(original, cloner) {
189      algorithmId = original.algorithmId;
190      algorithm = cloner.Clone(original.algorithm);
191      RegisterAlgorithmEvents();
192      runs = cloner.Clone(original.runs);
193      storeRunsAutomatically = original.storeRunsAutomatically;
194      UserId = original.UserId;
195      RegisterRunsEvents();
196    }
197    public OKBAlgorithm()
198      : base() {
199      algorithmId = -1;
200      algorithm = new EmptyAlgorithm("No algorithm selected. Please choose an algorithm from the OKB.");
201      RegisterAlgorithmEvents();
202      runs = new RunCollection();
203      storeRunsAutomatically = true;
204      RegisterRunsEvents();
205    }
206
207    private void CheckUserPermissions() {
208      if (UserInformation.Instance.UserExists) {
209        if (UserInformation.Instance.User.Roles.Count(x => x.Name == OKBRoles.OKBUser || x.Name == OKBRoles.OKBAdministrator) > 0) {
210          UserId = UserInformation.Instance.User.Id;
211        } else {
212          throw new Exception("You don't have the appropriate roles for executing OKB Algorithms.");
213        }
214      } else {
215        throw new Exception("You need an user account for executing OKB Algorithms.");
216      }
217    }
218
219    public override IDeepCloneable Clone(Cloner cloner) {
220      return new OKBAlgorithm(this, cloner);
221    }
222
223    public void Load(long algorithmId) {
224      if (this.algorithmId != algorithmId) {
225        IAlgorithm algorithm;
226        byte[] algorithmData = RunCreationClient.GetAlgorithmData(algorithmId);
227        using (MemoryStream stream = new MemoryStream(algorithmData)) {
228          algorithm = XmlParser.Deserialize<IAlgorithm>(stream);
229        }
230        this.algorithmId = algorithmId;
231        Algorithm = algorithm;
232      }
233    }
234
235    public IAlgorithm CloneAlgorithm() {
236      return (IAlgorithm)Algorithm.Clone();
237    }
238
239    public void CollectParameterValues(IDictionary<string, IItem> values) {
240      Algorithm.CollectParameterValues(values);
241    }
242    public void CollectResultValues(IDictionary<string, IItem> values) {
243      Algorithm.CollectResultValues(values);
244    }
245
246    public void Prepare() {
247      Algorithm.Prepare();
248    }
249    public void Prepare(bool clearRuns) {
250      if (clearRuns) runs.Clear();
251      Algorithm.Prepare(clearRuns);
252    }
253    public void Start() {
254      StartAsync().Wait();
255    }
256    public async Task StartAsync() {
257      await StartAsync(new CancellationToken());
258    }
259    public async Task StartAsync(CancellationToken cancellationToken) {
260      CheckUserPermissions();
261      if (!ClientInformation.Instance.ClientExists && storeRunsAutomatically) {
262        throw new MissingClientRegistrationException();
263      }
264      await Algorithm.StartAsync(cancellationToken);
265    }
266    public void Pause() {
267      Algorithm.Pause();
268    }
269    public void Stop() {
270      Algorithm.Stop();
271    }
272
273    #region Events
274    public event EventHandler AlgorithmChanged;
275    private void OnAlgorithmChanged() {
276      EventHandler handler = AlgorithmChanged;
277      if (handler != null) handler(this, EventArgs.Empty);
278    }
279    public event EventHandler<CancelEventArgs<string>> NameChanging;
280    private void OnNameChanging(CancelEventArgs<string> e) {
281      var handler = NameChanging;
282      if (handler != null) handler(this, e);
283    }
284    public event EventHandler NameChanged;
285    private void OnNameChanged() {
286      var handler = NameChanged;
287      if (handler != null) handler(this, EventArgs.Empty);
288    }
289    public event EventHandler DescriptionChanged;
290    private void OnDescriptionChanged() {
291      var handler = DescriptionChanged;
292      if (handler != null) handler(this, EventArgs.Empty);
293    }
294    public event EventHandler ExecutionStateChanged;
295    private void OnExecutionStateChanged() {
296      var handler = ExecutionStateChanged;
297      if (handler != null) handler(this, EventArgs.Empty);
298    }
299    public event EventHandler ExecutionTimeChanged;
300    private void OnExecutionTimeChanged() {
301      var handler = ExecutionTimeChanged;
302      if (handler != null) handler(this, EventArgs.Empty);
303    }
304    public event EventHandler ProblemChanged;
305    private void OnProblemChanged() {
306      var handler = ProblemChanged;
307      if (handler != null) handler(this, EventArgs.Empty);
308    }
309    public event EventHandler StoreRunsAutomaticallyChanged;
310    private void OnStoreRunsAutomaticallyChanged() {
311      var handler = StoreRunsAutomaticallyChanged;
312      if (handler != null) handler(this, EventArgs.Empty);
313    }
314    public event EventHandler StoreAlgorithmInEachRunChanged;
315    private void OnStoreAlgorithmInEachRunChanged() {
316      var handler = StoreAlgorithmInEachRunChanged;
317      if (handler != null) handler(this, EventArgs.Empty);
318    }
319    public event EventHandler Prepared;
320    private void OnPrepared() {
321      var handler = Prepared;
322      if (handler != null) handler(this, EventArgs.Empty);
323    }
324    public event EventHandler Started;
325    private void OnStarted() {
326      var handler = Started;
327      if (handler != null) handler(this, EventArgs.Empty);
328    }
329    public event EventHandler Paused;
330    private void OnPaused() {
331      var handler = Paused;
332      if (handler != null) handler(this, EventArgs.Empty);
333    }
334    public event EventHandler Stopped;
335    private void OnStopped() {
336      var handler = Stopped;
337      if (handler != null) handler(this, EventArgs.Empty);
338    }
339    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
340    private void OnExceptionOccurred(Exception exception) {
341      var handler = ExceptionOccurred;
342      if (handler != null) handler(this, new EventArgs<Exception>(exception));
343    }
344
345    private void RegisterAlgorithmEvents() {
346      if (Algorithm != null) {
347        Algorithm.ToStringChanged += new EventHandler(Algorithm_ToStringChanged);
348        Algorithm.ItemImageChanged += new EventHandler(Algorithm_ItemImageChanged);
349        Algorithm.NameChanging += new EventHandler<CancelEventArgs<string>>(Algorithm_NameChanging);
350        Algorithm.NameChanged += new EventHandler(Algorithm_NameChanged);
351        Algorithm.DescriptionChanged += new EventHandler(Algorithm_DescriptionChanged);
352        Algorithm.ExecutionStateChanged += new EventHandler(Algorithm_ExecutionStateChanged);
353        Algorithm.ExecutionTimeChanged += new EventHandler(Algorithm_ExecutionTimeChanged);
354        Algorithm.ProblemChanged += new EventHandler(Algorithm_ProblemChanged);
355        Algorithm.Runs.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_ItemsAdded);
356        Algorithm.StoreAlgorithmInEachRunChanged += new EventHandler(Algorithm_StoreAlgorithmInEachRunChanged);
357        Algorithm.Prepared += new EventHandler(Algorithm_Prepared);
358        Algorithm.Started += new EventHandler(Algorithm_Started);
359        Algorithm.Paused += new EventHandler(Algorithm_Paused);
360        Algorithm.Stopped += new EventHandler(Algorithm_Stopped);
361        Algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
362      }
363    }
364    private void DeregisterAlgorithmEvents() {
365      if (Algorithm != null) {
366        Algorithm.ToStringChanged -= new EventHandler(Algorithm_ToStringChanged);
367        Algorithm.ItemImageChanged -= new EventHandler(Algorithm_ItemImageChanged);
368        Algorithm.NameChanging -= new EventHandler<CancelEventArgs<string>>(Algorithm_NameChanging);
369        Algorithm.NameChanged -= new EventHandler(Algorithm_NameChanged);
370        Algorithm.DescriptionChanged -= new EventHandler(Algorithm_DescriptionChanged);
371        Algorithm.ExecutionStateChanged -= new EventHandler(Algorithm_ExecutionStateChanged);
372        Algorithm.ExecutionTimeChanged -= new EventHandler(Algorithm_ExecutionTimeChanged);
373        Algorithm.ProblemChanged -= new EventHandler(Algorithm_ProblemChanged);
374        Algorithm.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_ItemsAdded);
375        Algorithm.StoreAlgorithmInEachRunChanged -= new EventHandler(Algorithm_StoreAlgorithmInEachRunChanged);
376        Algorithm.Prepared -= new EventHandler(Algorithm_Prepared);
377        Algorithm.Started -= new EventHandler(Algorithm_Started);
378        Algorithm.Paused -= new EventHandler(Algorithm_Paused);
379        Algorithm.Stopped -= new EventHandler(Algorithm_Stopped);
380        Algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
381      }
382    }
383
384    private void Algorithm_ToStringChanged(object sender, EventArgs e) {
385      OnToStringChanged();
386    }
387    private void Algorithm_ItemImageChanged(object sender, EventArgs e) {
388      OnItemImageChanged();
389    }
390    private void Algorithm_NameChanging(object sender, CancelEventArgs<string> e) {
391      OnNameChanging(e);
392    }
393    private void Algorithm_NameChanged(object sender, EventArgs e) {
394      OnNameChanged();
395    }
396    private void Algorithm_DescriptionChanged(object sender, EventArgs e) {
397      OnDescriptionChanged();
398    }
399    private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
400      OnExecutionStateChanged();
401    }
402    private void Algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
403      OnExecutionTimeChanged();
404    }
405    private void Algorithm_ProblemChanged(object sender, EventArgs e) {
406      OnProblemChanged();
407    }
408    private void Algorithm_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
409      OKBProblem problem = Problem as OKBProblem;
410      foreach (IRun run in e.Items) {
411        if (problem != null) {
412          OKBRun okbRun = new OKBRun(AlgorithmId, problem.ProblemId, run, UserId);
413          runs.Add(okbRun);
414          if (StoreRunsAutomatically) {
415            okbRun.Store();
416          }
417        } else {
418          runs.Add(run);
419        }
420      }
421    }
422    private void Algorithm_StoreAlgorithmInEachRunChanged(object sender, EventArgs e) {
423      OnStoreAlgorithmInEachRunChanged();
424    }
425    private void Algorithm_Prepared(object sender, EventArgs e) {
426      OnPrepared();
427    }
428    private void Algorithm_Started(object sender, EventArgs e) {
429      OnStarted();
430    }
431    private void Algorithm_Paused(object sender, EventArgs e) {
432      OnPaused();
433    }
434    private void Algorithm_Stopped(object sender, EventArgs e) {
435      OnStopped();
436    }
437    private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
438      OnExceptionOccurred(e.Value);
439    }
440
441    private void RegisterRunsEvents() {
442      runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(runs_ItemsRemoved);
443      runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
444    }
445    private void runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
446      foreach (IRun run in e.Items) {
447        OKBRun okbRun = run as OKBRun;
448        if (okbRun != null)
449          Algorithm.Runs.Remove(okbRun.WrappedRun);
450        else
451          Algorithm.Runs.Remove(run);
452      }
453    }
454    private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
455      foreach (IRun run in e.OldItems) {
456        OKBRun okbRun = run as OKBRun;
457        if (okbRun != null)
458          Algorithm.Runs.Remove(okbRun.WrappedRun);
459        else
460          Algorithm.Runs.Remove(run);
461      }
462    }
463    #endregion
464  }
465}
Note: See TracBrowser for help on using the repository browser.