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