Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Clients.OKB/3.3/RunCreation/OKBAlgorithm.cs @ 16749

Last change on this file since 16749 was 13656, checked in by ascheibe, 9 years ago

#2582 created branch for Hive Web Job Manager

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