Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5352 was 5344, checked in by swagner, 14 years ago

Worked on OKB (#1174)

File size: 12.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Optimization;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Persistence.Default.Xml;
31
32namespace HeuristicLab.Clients.OKB {
33  [Item("OKB Algorithm", "An algorithm which is stored in the OKB.")]
34  [Creatable("Optimization Knowledge Base (OKB)")]
35  [StorableClass]
36  public sealed class OKBAlgorithm : Item, IAlgorithm {
37    private long algorithmId;
38    public long AlgorithmId {
39      get { return algorithmId; }
40    }
41    private IAlgorithm algorithm;
42    public IAlgorithm Algorithm {
43      get { return algorithm; }
44      private set {
45        if (value == null) throw new ArgumentNullException("Algorithm", "Algorithm cannot be null.");
46        if (value != algorithm) {
47          CancelEventArgs<string> e = new CancelEventArgs<string>(value.Name);
48          OnNameChanging(e);
49          if (!e.Cancel) {
50            DeregisterAlgorithmEvents();
51            algorithm = value;
52            RegisterAlgorithmEvents();
53
54            OnToStringChanged();
55            OnItemImageChanged();
56            OnNameChanged();
57            OnDescriptionChanged();
58            OnAlgorithmChanged();
59          }
60        }
61      }
62    }
63
64    public override Image ItemImage {
65      get { return Algorithm.ItemImage; }
66    }
67
68    public string Name {
69      get { return Algorithm.Name; }
70      set { throw new NotSupportedException("Name cannot be changed."); }
71    }
72    public string Description {
73      get { return Algorithm.Description; }
74      set { throw new NotSupportedException("Description cannot be changed."); }
75    }
76    public bool CanChangeName {
77      get { return false; }
78    }
79    public bool CanChangeDescription {
80      get { return false; }
81    }
82
83    public IKeyedItemCollection<string, IParameter> Parameters {
84      get { return Algorithm.Parameters; }
85    }
86
87    public ExecutionState ExecutionState {
88      get { return Algorithm.ExecutionState; }
89    }
90    public TimeSpan ExecutionTime {
91      get { return Algorithm.ExecutionTime; }
92    }
93
94    public Type ProblemType {
95      get { return Algorithm.ProblemType; }
96    }
97    public IProblem Problem {
98      get { return Algorithm.Problem; }
99      set { Algorithm.Problem = value; }
100    }
101
102    public ResultCollection Results {
103      get { return Algorithm.Results; }
104    }
105
106    public RunCollection Runs {
107      get { return Algorithm.Runs; }
108    }
109    public bool StoreAlgorithmInEachRun {
110      get { return Algorithm.StoreAlgorithmInEachRun; }
111      set { Algorithm.StoreAlgorithmInEachRun = value; }
112    }
113
114    #region Persistence Properties
115    [Storable(Name = "AlgorithmId")]
116    private long StorableAlgorithmId {
117      get { return algorithmId; }
118      set { algorithmId = value; }
119    }
120    [Storable(Name = "Algorithm")]
121    private IAlgorithm StorableAlgorithm {
122      get { return algorithm; }
123      set {
124        algorithm = value;
125        RegisterAlgorithmEvents();
126      }
127    }
128    #endregion
129
130    [StorableConstructor]
131    private OKBAlgorithm(bool deserializing) : base(deserializing) { }
132    private OKBAlgorithm(OKBAlgorithm original, Cloner cloner)
133      : base(original, cloner) {
134      algorithmId = original.algorithmId;
135      algorithm = cloner.Clone(original.algorithm);
136      RegisterAlgorithmEvents();
137    }
138    public OKBAlgorithm()
139      : base() {
140      algorithmId = -1;
141      algorithm = new EmptyAlgorithm("No algorithm selected. Please choose an algorithm from the OKB.");
142      RegisterAlgorithmEvents();
143    }
144
145    public override IDeepCloneable Clone(Cloner cloner) {
146      return new OKBAlgorithm(this, cloner);
147    }
148
149    public void Load(long algorithmId) {
150      if (this.algorithmId != algorithmId) {
151        IAlgorithm algorithm;
152        AlgorithmData algorithmData = OKBClient.Instance.GetAlgorithmData(algorithmId);
153        using (MemoryStream stream = new MemoryStream(algorithmData.Data)) {
154          algorithm = XmlParser.Deserialize<IAlgorithm>(stream);
155        }
156        this.algorithmId = algorithmId;
157        Algorithm = algorithm;
158      }
159    }
160
161    public void CollectParameterValues(IDictionary<string, IItem> values) {
162      Algorithm.CollectParameterValues(values);
163    }
164    public void CollectResultValues(IDictionary<string, IItem> values) {
165      Algorithm.CollectResultValues(values);
166    }
167
168    public void Prepare() {
169      Algorithm.Prepare();
170    }
171    public void Prepare(bool clearRuns) {
172      Algorithm.Prepare(clearRuns);
173    }
174    public void Start() {
175      Algorithm.Start();
176    }
177    public void Pause() {
178      Algorithm.Pause();
179    }
180    public void Stop() {
181      Algorithm.Stop();
182    }
183
184    #region Events
185    public event EventHandler AlgorithmChanged;
186    private void OnAlgorithmChanged() {
187      EventHandler handler = AlgorithmChanged;
188      if (handler != null) handler(this, EventArgs.Empty);
189    }
190    public event EventHandler<CancelEventArgs<string>> NameChanging;
191    private void OnNameChanging(CancelEventArgs<string> e) {
192      var handler = NameChanging;
193      if (handler != null) handler(this, e);
194    }
195    public event EventHandler NameChanged;
196    private void OnNameChanged() {
197      var handler = NameChanged;
198      if (handler != null) handler(this, EventArgs.Empty);
199    }
200    public event EventHandler DescriptionChanged;
201    private void OnDescriptionChanged() {
202      var handler = DescriptionChanged;
203      if (handler != null) handler(this, EventArgs.Empty);
204    }
205    public event EventHandler ExecutionStateChanged;
206    private void OnExecutionStateChanged() {
207      var handler = ExecutionStateChanged;
208      if (handler != null) handler(this, EventArgs.Empty);
209    }
210    public event EventHandler ExecutionTimeChanged;
211    private void OnExecutionTimeChanged() {
212      var handler = ExecutionTimeChanged;
213      if (handler != null) handler(this, EventArgs.Empty);
214    }
215    public event EventHandler ProblemChanged;
216    private void OnProblemChanged() {
217      var handler = ProblemChanged;
218      if (handler != null) handler(this, EventArgs.Empty);
219    }
220    public event EventHandler StoreAlgorithmInEachRunChanged;
221    private void OnStoreAlgorithmInEachRunChanged() {
222      var handler = StoreAlgorithmInEachRunChanged;
223      if (handler != null) handler(this, EventArgs.Empty);
224    }
225    public event EventHandler Prepared;
226    private void OnPrepared() {
227      var handler = Prepared;
228      if (handler != null) handler(this, EventArgs.Empty);
229    }
230    public event EventHandler Started;
231    private void OnStarted() {
232      var handler = Started;
233      if (handler != null) handler(this, EventArgs.Empty);
234    }
235    public event EventHandler Paused;
236    private void OnPaused() {
237      var handler = Paused;
238      if (handler != null) handler(this, EventArgs.Empty);
239    }
240    public event EventHandler Stopped;
241    private void OnStopped() {
242      var handler = Stopped;
243      if (handler != null) handler(this, EventArgs.Empty);
244    }
245    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
246    private void OnExceptionOccurred(Exception exception) {
247      var handler = ExceptionOccurred;
248      if (handler != null) handler(this, new EventArgs<Exception>(exception));
249    }
250
251    private void RegisterAlgorithmEvents() {
252      if (Algorithm != null) {
253        Algorithm.ToStringChanged += new EventHandler(Algorithm_ToStringChanged);
254        Algorithm.ItemImageChanged += new EventHandler(Algorithm_ItemImageChanged);
255        Algorithm.NameChanging += new EventHandler<CancelEventArgs<string>>(Algorithm_NameChanging);
256        Algorithm.NameChanged += new EventHandler(Algorithm_NameChanged);
257        Algorithm.DescriptionChanged += new EventHandler(Algorithm_DescriptionChanged);
258        Algorithm.ExecutionStateChanged += new EventHandler(Algorithm_ExecutionStateChanged);
259        Algorithm.ExecutionTimeChanged += new EventHandler(Algorithm_ExecutionTimeChanged);
260        Algorithm.ProblemChanged += new EventHandler(Algorithm_ProblemChanged);
261        Algorithm.StoreAlgorithmInEachRunChanged += new EventHandler(Algorithm_StoreAlgorithmInEachRunChanged);
262        Algorithm.Prepared += new EventHandler(Algorithm_Prepared);
263        Algorithm.Started += new EventHandler(Algorithm_Started);
264        Algorithm.Paused += new EventHandler(Algorithm_Paused);
265        Algorithm.Stopped += new EventHandler(Algorithm_Stopped);
266        Algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
267      }
268    }
269    private void DeregisterAlgorithmEvents() {
270      if (Algorithm != null) {
271        Algorithm.ToStringChanged -= new EventHandler(Algorithm_ToStringChanged);
272        Algorithm.ItemImageChanged -= new EventHandler(Algorithm_ItemImageChanged);
273        Algorithm.NameChanging -= new EventHandler<CancelEventArgs<string>>(Algorithm_NameChanging);
274        Algorithm.NameChanged -= new EventHandler(Algorithm_NameChanged);
275        Algorithm.DescriptionChanged -= new EventHandler(Algorithm_DescriptionChanged);
276        Algorithm.ExecutionStateChanged -= new EventHandler(Algorithm_ExecutionStateChanged);
277        Algorithm.ExecutionTimeChanged -= new EventHandler(Algorithm_ExecutionTimeChanged);
278        Algorithm.ProblemChanged -= new EventHandler(Algorithm_ProblemChanged);
279        Algorithm.StoreAlgorithmInEachRunChanged -= new EventHandler(Algorithm_StoreAlgorithmInEachRunChanged);
280        Algorithm.Prepared -= new EventHandler(Algorithm_Prepared);
281        Algorithm.Started -= new EventHandler(Algorithm_Started);
282        Algorithm.Paused -= new EventHandler(Algorithm_Paused);
283        Algorithm.Stopped -= new EventHandler(Algorithm_Stopped);
284        Algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
285      }
286    }
287
288    private void Algorithm_ToStringChanged(object sender, EventArgs e) {
289      OnToStringChanged();
290    }
291    private void Algorithm_ItemImageChanged(object sender, EventArgs e) {
292      OnItemImageChanged();
293    }
294    private void Algorithm_NameChanging(object sender, CancelEventArgs<string> e) {
295      OnNameChanging(e);
296    }
297    private void Algorithm_NameChanged(object sender, EventArgs e) {
298      OnNameChanged();
299    }
300    private void Algorithm_DescriptionChanged(object sender, EventArgs e) {
301      OnDescriptionChanged();
302    }
303    private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
304      OnExecutionStateChanged();
305    }
306    private void Algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
307      OnExecutionTimeChanged();
308    }
309    private void Algorithm_ProblemChanged(object sender, EventArgs e) {
310      OnProblemChanged();
311    }
312    private void Algorithm_StoreAlgorithmInEachRunChanged(object sender, EventArgs e) {
313      OnStoreAlgorithmInEachRunChanged();
314    }
315    private void Algorithm_Prepared(object sender, EventArgs e) {
316      OnPrepared();
317    }
318    private void Algorithm_Started(object sender, EventArgs e) {
319      OnStarted();
320    }
321    private void Algorithm_Paused(object sender, EventArgs e) {
322      OnPaused();
323    }
324    private void Algorithm_Stopped(object sender, EventArgs e) {
325      OnStopped();
326    }
327    private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
328      OnExceptionOccurred(e.Value);
329    }
330    #endregion
331  }
332}
Note: See TracBrowser for help on using the repository browser.