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