Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1174 improved access service handling to work with Hive

File size: 16.0 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      UserId = UserInformation.Instance.User.Id;
203      RegisterRunsEvents();
204    }
205
206    public override IDeepCloneable Clone(Cloner cloner) {
207      return new OKBAlgorithm(this, cloner);
208    }
209
210    public void Load(long algorithmId) {
211      if (this.algorithmId != algorithmId) {
212        IAlgorithm algorithm;
213        byte[] algorithmData = RunCreationClient.GetAlgorithmData(algorithmId);
214        using (MemoryStream stream = new MemoryStream(algorithmData)) {
215          algorithm = XmlParser.Deserialize<IAlgorithm>(stream);
216        }
217        this.algorithmId = algorithmId;
218        Algorithm = algorithm;
219      }
220    }
221
222    public IAlgorithm CloneAlgorithm() {
223      return (IAlgorithm)Algorithm.Clone();
224    }
225
226    public void CollectParameterValues(IDictionary<string, IItem> values) {
227      Algorithm.CollectParameterValues(values);
228    }
229    public void CollectResultValues(IDictionary<string, IItem> values) {
230      Algorithm.CollectResultValues(values);
231    }
232
233    public void Prepare() {
234      Algorithm.Prepare();
235    }
236    public void Prepare(bool clearRuns) {
237      if (clearRuns) runs.Clear();
238      Algorithm.Prepare(clearRuns);
239    }
240    public void Start() {
241      Algorithm.Start();
242    }
243    public void Pause() {
244      Algorithm.Pause();
245    }
246    public void Stop() {
247      Algorithm.Stop();
248    }
249
250    #region Events
251    public event EventHandler AlgorithmChanged;
252    private void OnAlgorithmChanged() {
253      EventHandler handler = AlgorithmChanged;
254      if (handler != null) handler(this, EventArgs.Empty);
255    }
256    public event EventHandler<CancelEventArgs<string>> NameChanging;
257    private void OnNameChanging(CancelEventArgs<string> e) {
258      var handler = NameChanging;
259      if (handler != null) handler(this, e);
260    }
261    public event EventHandler NameChanged;
262    private void OnNameChanged() {
263      var handler = NameChanged;
264      if (handler != null) handler(this, EventArgs.Empty);
265    }
266    public event EventHandler DescriptionChanged;
267    private void OnDescriptionChanged() {
268      var handler = DescriptionChanged;
269      if (handler != null) handler(this, EventArgs.Empty);
270    }
271    public event EventHandler ExecutionStateChanged;
272    private void OnExecutionStateChanged() {
273      var handler = ExecutionStateChanged;
274      if (handler != null) handler(this, EventArgs.Empty);
275    }
276    public event EventHandler ExecutionTimeChanged;
277    private void OnExecutionTimeChanged() {
278      var handler = ExecutionTimeChanged;
279      if (handler != null) handler(this, EventArgs.Empty);
280    }
281    public event EventHandler ProblemChanged;
282    private void OnProblemChanged() {
283      var handler = ProblemChanged;
284      if (handler != null) handler(this, EventArgs.Empty);
285    }
286    public event EventHandler StoreRunsAutomaticallyChanged;
287    private void OnStoreRunsAutomaticallyChanged() {
288      var handler = StoreRunsAutomaticallyChanged;
289      if (handler != null) handler(this, EventArgs.Empty);
290    }
291    public event EventHandler StoreAlgorithmInEachRunChanged;
292    private void OnStoreAlgorithmInEachRunChanged() {
293      var handler = StoreAlgorithmInEachRunChanged;
294      if (handler != null) handler(this, EventArgs.Empty);
295    }
296    public event EventHandler Prepared;
297    private void OnPrepared() {
298      var handler = Prepared;
299      if (handler != null) handler(this, EventArgs.Empty);
300    }
301    public event EventHandler Started;
302    private void OnStarted() {
303      var handler = Started;
304      if (handler != null) handler(this, EventArgs.Empty);
305    }
306    public event EventHandler Paused;
307    private void OnPaused() {
308      var handler = Paused;
309      if (handler != null) handler(this, EventArgs.Empty);
310    }
311    public event EventHandler Stopped;
312    private void OnStopped() {
313      var handler = Stopped;
314      if (handler != null) handler(this, EventArgs.Empty);
315    }
316    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
317    private void OnExceptionOccurred(Exception exception) {
318      var handler = ExceptionOccurred;
319      if (handler != null) handler(this, new EventArgs<Exception>(exception));
320    }
321
322    private void RegisterAlgorithmEvents() {
323      if (Algorithm != null) {
324        Algorithm.ToStringChanged += new EventHandler(Algorithm_ToStringChanged);
325        Algorithm.ItemImageChanged += new EventHandler(Algorithm_ItemImageChanged);
326        Algorithm.NameChanging += new EventHandler<CancelEventArgs<string>>(Algorithm_NameChanging);
327        Algorithm.NameChanged += new EventHandler(Algorithm_NameChanged);
328        Algorithm.DescriptionChanged += new EventHandler(Algorithm_DescriptionChanged);
329        Algorithm.ExecutionStateChanged += new EventHandler(Algorithm_ExecutionStateChanged);
330        Algorithm.ExecutionTimeChanged += new EventHandler(Algorithm_ExecutionTimeChanged);
331        Algorithm.ProblemChanged += new EventHandler(Algorithm_ProblemChanged);
332        Algorithm.Runs.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_ItemsAdded);
333        Algorithm.StoreAlgorithmInEachRunChanged += new EventHandler(Algorithm_StoreAlgorithmInEachRunChanged);
334        Algorithm.Prepared += new EventHandler(Algorithm_Prepared);
335        Algorithm.Started += new EventHandler(Algorithm_Started);
336        Algorithm.Paused += new EventHandler(Algorithm_Paused);
337        Algorithm.Stopped += new EventHandler(Algorithm_Stopped);
338        Algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
339      }
340    }
341    private void DeregisterAlgorithmEvents() {
342      if (Algorithm != null) {
343        Algorithm.ToStringChanged -= new EventHandler(Algorithm_ToStringChanged);
344        Algorithm.ItemImageChanged -= new EventHandler(Algorithm_ItemImageChanged);
345        Algorithm.NameChanging -= new EventHandler<CancelEventArgs<string>>(Algorithm_NameChanging);
346        Algorithm.NameChanged -= new EventHandler(Algorithm_NameChanged);
347        Algorithm.DescriptionChanged -= new EventHandler(Algorithm_DescriptionChanged);
348        Algorithm.ExecutionStateChanged -= new EventHandler(Algorithm_ExecutionStateChanged);
349        Algorithm.ExecutionTimeChanged -= new EventHandler(Algorithm_ExecutionTimeChanged);
350        Algorithm.ProblemChanged -= new EventHandler(Algorithm_ProblemChanged);
351        Algorithm.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_ItemsAdded);
352        Algorithm.StoreAlgorithmInEachRunChanged -= new EventHandler(Algorithm_StoreAlgorithmInEachRunChanged);
353        Algorithm.Prepared -= new EventHandler(Algorithm_Prepared);
354        Algorithm.Started -= new EventHandler(Algorithm_Started);
355        Algorithm.Paused -= new EventHandler(Algorithm_Paused);
356        Algorithm.Stopped -= new EventHandler(Algorithm_Stopped);
357        Algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
358      }
359    }
360
361    private void Algorithm_ToStringChanged(object sender, EventArgs e) {
362      OnToStringChanged();
363    }
364    private void Algorithm_ItemImageChanged(object sender, EventArgs e) {
365      OnItemImageChanged();
366    }
367    private void Algorithm_NameChanging(object sender, CancelEventArgs<string> e) {
368      OnNameChanging(e);
369    }
370    private void Algorithm_NameChanged(object sender, EventArgs e) {
371      OnNameChanged();
372    }
373    private void Algorithm_DescriptionChanged(object sender, EventArgs e) {
374      OnDescriptionChanged();
375    }
376    private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
377      OnExecutionStateChanged();
378    }
379    private void Algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
380      OnExecutionTimeChanged();
381    }
382    private void Algorithm_ProblemChanged(object sender, EventArgs e) {
383      OnProblemChanged();
384    }
385    private void Algorithm_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
386      OKBProblem problem = Problem as OKBProblem;
387      foreach (IRun run in e.Items) {
388        if (problem != null) {
389          OKBRun okbRun = new OKBRun(AlgorithmId, problem.ProblemId, run, UserId);
390          runs.Add(okbRun);
391          if (StoreRunsAutomatically) {
392            try { okbRun.Store(); }
393            catch (Exception) { }
394          }
395        } else {
396          runs.Add(run);
397        }
398      }
399    }
400    private void Algorithm_StoreAlgorithmInEachRunChanged(object sender, EventArgs e) {
401      OnStoreAlgorithmInEachRunChanged();
402    }
403    private void Algorithm_Prepared(object sender, EventArgs e) {
404      OnPrepared();
405    }
406    private void Algorithm_Started(object sender, EventArgs e) {
407      OnStarted();
408    }
409    private void Algorithm_Paused(object sender, EventArgs e) {
410      OnPaused();
411    }
412    private void Algorithm_Stopped(object sender, EventArgs e) {
413      OnStopped();
414    }
415    private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
416      OnExceptionOccurred(e.Value);
417    }
418
419    private void RegisterRunsEvents() {
420      runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(runs_ItemsRemoved);
421      runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
422    }
423    private void runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
424      foreach (IRun run in e.Items) {
425        OKBRun okbRun = run as OKBRun;
426        if (okbRun != null)
427          Algorithm.Runs.Remove(okbRun.WrappedRun);
428        else
429          Algorithm.Runs.Remove(run);
430      }
431    }
432    private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
433      foreach (IRun run in e.OldItems) {
434        OKBRun okbRun = run as OKBRun;
435        if (okbRun != null)
436          Algorithm.Runs.Remove(okbRun.WrappedRun);
437        else
438          Algorithm.Runs.Remove(run);
439      }
440    }
441    #endregion
442  }
443}
Note: See TracBrowser for help on using the repository browser.