Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB (trunk integration)/HeuristicLab.Clients.OKB/3.3/RunCreation/OKBAlgorithm.cs @ 7592

Last change on this file since 7592 was 7592, checked in by ascheibe, 12 years ago

#1174 added client-side security checks

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