Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.Clients.OKB-3.3/OKBExperiment.cs @ 4549

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

Worked on OKB (#1174)

File size: 8.0 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.Drawing;
24using System.IO;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Optimization;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Persistence.Default.Xml;
30
31namespace HeuristicLab.Clients.OKB {
32  [Item("OKB Experiment", "...")]
33  [Creatable("OKB")]
34  [StorableClass]
35  public sealed class OKBExperiment : NamedItem, IOptimizer, IStorableContent {
36    public string Filename { get; set; }
37
38    public override Image ItemImage {
39      get {
40        if (Algorithm != null) return Algorithm.ItemImage;
41        else return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event;
42      }
43    }
44
45    [Storable]
46    private long AlgorithmId { get; set; }
47    [Storable]
48    private IAlgorithm algorithm;
49    private IAlgorithm Algorithm {
50      get { return algorithm; }
51      set {
52        DeregisterAlgorithmEvents();
53        algorithm = value;
54        RegisterAlgorithmEvents();
55        algorithm.Problem = Problem;
56        OnChanged();
57      }
58    }
59    private long ProblemId { get; set; }
60    private IProblem problem;
61    public IProblem Problem {
62      get { return problem; }
63      private set {
64        problem = value;
65        if (Algorithm != null) Algorithm.Problem = problem;
66        OnChanged();
67      }
68    }
69
70    public IKeyedItemCollection<string, IParameter> Parameters {
71      get {
72        if (Algorithm != null) return Algorithm.Parameters;
73        else return null;
74      }
75    }
76    public ResultCollection Results {
77      get {
78        if (Algorithm != null) return Algorithm.Results;
79        else return null;
80      }
81    }
82    public RunCollection Runs {
83      get {
84        if (Algorithm != null) return Algorithm.Runs;
85        else return null;
86      }
87    }
88    public ExecutionState ExecutionState {
89      get {
90        if (Algorithm != null) return Algorithm.ExecutionState;
91        else return ExecutionState.Stopped;
92      }
93    }
94    public TimeSpan ExecutionTime {
95      get {
96        if (Algorithm != null) return Algorithm.ExecutionTime;
97        else return TimeSpan.Zero;
98      }
99    }
100
101    public OKBExperiment()
102      : base() {
103      name = ItemName;
104      description = ItemDescription;
105    }
106    [StorableConstructor]
107    private OKBExperiment(bool deserializing) : base(deserializing) { }
108
109    public void LoadAlgorithm(long id) {
110      AlgorithmId = id;
111      AlgorithmData algorithmData = OKBClient.Instance.GetAlgorithmData(id);
112
113      using (MemoryStream stream = new MemoryStream(algorithmData.Data)) {
114        Algorithm = XmlParser.Deserialize<IAlgorithm>(stream);
115      }
116    }
117    public void LoadProblem(long id) {
118      ProblemId = id;
119      ProblemData problemData = OKBClient.Instance.GetProblemData(id);
120
121      using (MemoryStream stream = new MemoryStream(problemData.Data)) {
122        Problem = XmlParser.Deserialize<IProblem>(stream);
123      }
124    }
125
126    public void Pause() {
127      Algorithm.Pause();
128    }
129    public void Prepare(bool clearRuns) {
130      if (Algorithm != null) Algorithm.Prepare(clearRuns);
131    }
132    public void Prepare() {
133      Algorithm.Prepare();
134    }
135    public void Start() {
136      Algorithm.Start();
137    }
138    public void Stop() {
139      Algorithm.Stop();
140    }
141
142    #region Events
143    public event EventHandler Changed;
144    private void OnChanged() {
145      EventHandler handler = Changed;
146      if (handler != null) handler(this, EventArgs.Empty);
147    }
148    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
149    private void OnExceptionOccurred(Exception exception) {
150      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
151      if (handler != null) handler(this, new EventArgs<Exception>(exception));
152    }
153    public event EventHandler ExecutionStateChanged;
154    private void OnExecutionStateChanged() {
155      EventHandler handler = ExecutionStateChanged;
156      if (handler != null) handler(this, EventArgs.Empty);
157    }
158    public event EventHandler ExecutionTimeChanged;
159    private void OnExecutionTimeChanged() {
160      EventHandler handler = ExecutionTimeChanged;
161      if (handler != null) handler(this, EventArgs.Empty);
162    }
163    public event EventHandler Prepared;
164    private void OnPrepared() {
165      EventHandler handler = Prepared;
166      if (handler != null) handler(this, EventArgs.Empty);
167    }
168    public event EventHandler Started;
169    private void OnStarted() {
170      EventHandler handler = Started;
171      if (handler != null) handler(this, EventArgs.Empty);
172    }
173    public event EventHandler Paused;
174    private void OnPaused() {
175      EventHandler handler = Paused;
176      if (handler != null) handler(this, EventArgs.Empty);
177    }
178    public event EventHandler Stopped;
179    private void OnStopped() {
180      EventHandler handler = Stopped;
181      if (handler != null) handler(this, EventArgs.Empty);
182    }
183
184    private void RegisterAlgorithmEvents() {
185      if (Algorithm != null) {
186        Algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
187        Algorithm.ExecutionStateChanged += new EventHandler(Algorithm_ExecutionStateChanged);
188        Algorithm.ExecutionTimeChanged += new EventHandler(Algorithm_ExecutionTimeChanged);
189        Algorithm.ItemImageChanged += new EventHandler(Algorithm_ItemImageChanged);
190        Algorithm.Paused += new EventHandler(Algorithm_Paused);
191        Algorithm.Prepared += new EventHandler(Algorithm_Prepared);
192        Algorithm.Started += new EventHandler(Algorithm_Started);
193        Algorithm.Stopped += new EventHandler(Algorithm_Stopped);
194      }
195    }
196    private void DeregisterAlgorithmEvents() {
197      if (Algorithm != null) {
198        Algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
199        Algorithm.ExecutionStateChanged -= new EventHandler(Algorithm_ExecutionStateChanged);
200        Algorithm.ExecutionTimeChanged -= new EventHandler(Algorithm_ExecutionTimeChanged);
201        Algorithm.ItemImageChanged -= new EventHandler(Algorithm_ItemImageChanged);
202        Algorithm.Paused -= new EventHandler(Algorithm_Paused);
203        Algorithm.Prepared -= new EventHandler(Algorithm_Prepared);
204        Algorithm.Started -= new EventHandler(Algorithm_Started);
205        Algorithm.Stopped -= new EventHandler(Algorithm_Stopped);
206      }
207    }
208    private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
209      OnExceptionOccurred(e.Value);
210    }
211    private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
212      OnExecutionStateChanged();
213    }
214    private void Algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
215      OnExecutionTimeChanged();
216    }
217    private void Algorithm_ItemImageChanged(object sender, EventArgs e) {
218      OnItemImageChanged();
219    }
220    private void Algorithm_Paused(object sender, EventArgs e) {
221      OnPaused();
222    }
223    private void Algorithm_Prepared(object sender, EventArgs e) {
224      OnPrepared();
225    }
226    private void Algorithm_Started(object sender, EventArgs e) {
227      OnStarted();
228    }
229    private void Algorithm_Stopped(object sender, EventArgs e) {
230      OnStopped();
231    }
232    #endregion
233  }
234}
Note: See TracBrowser for help on using the repository browser.