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 |
|
---|
62 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
63 | Executable clone = (Executable)base.Clone(cloner);
|
---|
64 | clone.executionState = executionState;
|
---|
65 | clone.executionTime = executionTime;
|
---|
66 | return clone;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public virtual void Prepare() {
|
---|
70 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
|
---|
71 | throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
|
---|
72 | ExecutionTime = TimeSpan.Zero;
|
---|
73 | }
|
---|
74 | public virtual void Start() {
|
---|
75 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
|
---|
76 | throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
|
---|
77 | }
|
---|
78 | public virtual void Pause() {
|
---|
79 | if (ExecutionState != ExecutionState.Started)
|
---|
80 | throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
|
---|
81 | }
|
---|
82 | public virtual void Stop() {
|
---|
83 | if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
|
---|
84 | throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
|
---|
85 | }
|
---|
86 |
|
---|
87 | public event EventHandler ExecutionStateChanged;
|
---|
88 | protected virtual void OnExecutionStateChanged() {
|
---|
89 | EventHandler handler = ExecutionStateChanged;
|
---|
90 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
91 | }
|
---|
92 | public event EventHandler ExecutionTimeChanged;
|
---|
93 | protected virtual void OnExecutionTimeChanged() {
|
---|
94 | EventHandler handler = ExecutionTimeChanged;
|
---|
95 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
96 | }
|
---|
97 | public event EventHandler Prepared;
|
---|
98 | protected virtual void OnPrepared() {
|
---|
99 | ExecutionState = ExecutionState.Prepared;
|
---|
100 | EventHandler handler = Prepared;
|
---|
101 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
102 | }
|
---|
103 | public event EventHandler Started;
|
---|
104 | protected virtual void OnStarted() {
|
---|
105 | ExecutionState = ExecutionState.Started;
|
---|
106 | EventHandler handler = Started;
|
---|
107 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
108 | }
|
---|
109 | public event EventHandler Paused;
|
---|
110 | protected virtual void OnPaused() {
|
---|
111 | ExecutionState = ExecutionState.Paused;
|
---|
112 | EventHandler handler = Paused;
|
---|
113 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
114 | }
|
---|
115 | public event EventHandler Stopped;
|
---|
116 | protected virtual void OnStopped() {
|
---|
117 | ExecutionState = ExecutionState.Stopped;
|
---|
118 | EventHandler handler = Stopped;
|
---|
119 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
120 | }
|
---|
121 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
122 | protected virtual void OnExceptionOccurred(Exception exception) {
|
---|
123 | EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
|
---|
124 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|