Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/Executable.cs @ 3668

Last change on this file since 3668 was 3372, checked in by swagner, 15 years ago

Implemented reviewers' comments (#893).

File size: 5.6 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 HeuristicLab.Common;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Core {
28  [Item("Executable", "A base class for executables.")]
29  [StorableClass]
30  public abstract class Executable : Item, IExecutable {
31    public override Image ItemImage {
32      get {
33        if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutablePrepared;
34        else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutableStarted;
35        else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutablePaused;
36        else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutableStopped;
37        else return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event;
38      }
39    }
40
41    [Storable]
42    private ExecutionState executionState;
43    public ExecutionState ExecutionState {
44      get { return executionState; }
45      private set {
46        if (executionState != value) {
47          executionState = value;
48          OnExecutionStateChanged();
49          OnItemImageChanged();
50        }
51      }
52    }
53
54    [Storable]
55    private TimeSpan executionTime;
56    public TimeSpan ExecutionTime {
57      get { return executionTime; }
58      protected set {
59        executionTime = value;
60        OnExecutionTimeChanged();
61      }
62    }
63
64    protected Executable() {
65      executionState = ExecutionState.Stopped;
66      executionTime = TimeSpan.Zero;
67    }
68    [StorableConstructor]
69    protected Executable(bool deserializing) : base(deserializing) { }
70
71    public override IDeepCloneable Clone(Cloner cloner) {
72      Executable clone = (Executable)base.Clone(cloner);
73      clone.executionState = executionState;
74      clone.executionTime = executionTime;
75      return clone;
76    }
77
78    public virtual void Prepare() {
79      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
80        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
81      ExecutionTime = TimeSpan.Zero;
82    }
83    public virtual void Start() {
84      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
85        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
86    }
87    public virtual void Pause() {
88      if (ExecutionState != ExecutionState.Started)
89        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
90    }
91    public virtual void Stop() {
92      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
93        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
94    }
95
96    public event EventHandler ExecutionStateChanged;
97    protected virtual void OnExecutionStateChanged() {
98      EventHandler handler = ExecutionStateChanged;
99      if (handler != null) handler(this, EventArgs.Empty);
100    }
101    public event EventHandler ExecutionTimeChanged;
102    protected virtual void OnExecutionTimeChanged() {
103      EventHandler handler = ExecutionTimeChanged;
104      if (handler != null) handler(this, EventArgs.Empty);
105    }
106    public event EventHandler Prepared;
107    protected virtual void OnPrepared() {
108      ExecutionState = ExecutionState.Prepared;
109      EventHandler handler = Prepared;
110      if (handler != null) handler(this, EventArgs.Empty);
111    }
112    public event EventHandler Started;
113    protected virtual void OnStarted() {
114      ExecutionState = ExecutionState.Started;
115      EventHandler handler = Started;
116      if (handler != null) handler(this, EventArgs.Empty);
117    }
118    public event EventHandler Paused;
119    protected virtual void OnPaused() {
120      ExecutionState = ExecutionState.Paused;
121      EventHandler handler = Paused;
122      if (handler != null) handler(this, EventArgs.Empty);
123    }
124    public event EventHandler Stopped;
125    protected virtual void OnStopped() {
126      ExecutionState = ExecutionState.Stopped;
127      EventHandler handler = Stopped;
128      if (handler != null) handler(this, EventArgs.Empty);
129    }
130    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
131    protected virtual void OnExceptionOccurred(Exception exception) {
132      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
133      if (handler != null) handler(this, new EventArgs<Exception>(exception));
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.