Free cookie consent management tool by TermsFeed Policy Generator

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

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

Merged cloning refactoring branch back into trunk (#922)

File size: 5.5 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    [StorableConstructor]
65    protected Executable(bool deserializing) : base(deserializing) { }
66    protected Executable(Executable original, Cloner cloner)
67      : base(original, cloner) {
68      executionState = original.executionState;
69      executionTime = original.executionTime;
70    }
71    protected Executable() {
72      executionState = ExecutionState.Stopped;
73      executionTime = TimeSpan.Zero;
74    }
75
76    public virtual void Prepare() {
77      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
78        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
79      ExecutionTime = TimeSpan.Zero;
80    }
81    public virtual void Start() {
82      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
83        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
84    }
85    public virtual void Pause() {
86      if (ExecutionState != ExecutionState.Started)
87        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
88    }
89    public virtual void Stop() {
90      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
91        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
92    }
93
94    public event EventHandler ExecutionStateChanged;
95    protected virtual void OnExecutionStateChanged() {
96      EventHandler handler = ExecutionStateChanged;
97      if (handler != null) handler(this, EventArgs.Empty);
98    }
99    public event EventHandler ExecutionTimeChanged;
100    protected virtual void OnExecutionTimeChanged() {
101      EventHandler handler = ExecutionTimeChanged;
102      if (handler != null) handler(this, EventArgs.Empty);
103    }
104    public event EventHandler Prepared;
105    protected virtual void OnPrepared() {
106      ExecutionState = ExecutionState.Prepared;
107      EventHandler handler = Prepared;
108      if (handler != null) handler(this, EventArgs.Empty);
109    }
110    public event EventHandler Started;
111    protected virtual void OnStarted() {
112      ExecutionState = ExecutionState.Started;
113      EventHandler handler = Started;
114      if (handler != null) handler(this, EventArgs.Empty);
115    }
116    public event EventHandler Paused;
117    protected virtual void OnPaused() {
118      ExecutionState = ExecutionState.Paused;
119      EventHandler handler = Paused;
120      if (handler != null) handler(this, EventArgs.Empty);
121    }
122    public event EventHandler Stopped;
123    protected virtual void OnStopped() {
124      ExecutionState = ExecutionState.Stopped;
125      EventHandler handler = Stopped;
126      if (handler != null) handler(this, EventArgs.Empty);
127    }
128    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
129    protected virtual void OnExceptionOccurred(Exception exception) {
130      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
131      if (handler != null) handler(this, new EventArgs<Exception>(exception));
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.