Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12449 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

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