Changeset 3262 for trunk/sources/HeuristicLab.Core/3.3
- Timestamp:
- 04/04/10 05:22:47 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Core/3.3
- Files:
-
- 3 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Core/3.3/Engine.cs
r3261 r3262 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Drawing;25 24 using System.Threading; 26 using HeuristicLab.Common;27 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 26 29 27 namespace HeuristicLab.Core { 30 /// <summary>31 /// Base class to represent an engine, which is an interpreter, holding the code, the data and32 /// the actual state, which is the runtime stack and a pointer onto the next operation. It represents33 /// one execution and can handle parallel executions.34 /// </summary>35 28 [Item("Engine", "A base class for engines.")] 36 29 [StorableClass] 37 public abstract class Engine : Item, IEngine { 38 public override Image ItemImage { 39 get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event; } 40 } 41 42 [Storable] 43 private TimeSpan executionTime; 44 /// <summary> 45 /// Gets or sets the execution time. 46 /// </summary> 47 /// <remarks>Calls <see cref="OnExecutionTimeChanged"/> in the setter.</remarks> 48 public TimeSpan ExecutionTime { 49 get { return executionTime; } 50 protected set { 51 executionTime = value; 52 OnExecutionTimeChanged(); 53 } 54 } 55 56 /// <summary> 57 /// Field of the current instance that represent the execution stack. 58 /// </summary> 30 public abstract class Engine : Executable, IEngine { 59 31 [Storable] 60 32 private Stack<IOperation> executionStack; 61 /// <summary>62 /// Gets the current execution stack.63 /// </summary>64 33 protected Stack<IOperation> ExecutionStack { 65 34 get { return executionStack; } 66 35 } 67 36 68 /// <summary> 69 /// Flag of the current instance whether it is currently running. 70 /// </summary> 71 private bool running; 72 /// <summary> 73 /// Gets information whether the instance is currently running. 74 /// </summary> 75 public bool Running { 76 get { return running; } 77 private set { 78 if (running != value) { 79 running = value; 80 OnRunningChanged(); 81 } 82 } 37 private bool pausePending, stopPending; 38 private DateTime lastUpdateTime; 39 private System.Timers.Timer timer; 40 41 protected Engine() 42 : base() { 43 executionStack = new Stack<IOperation>(); 44 pausePending = stopPending = false; 45 timer = new System.Timers.Timer(100); 46 timer.AutoReset = true; 47 timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); 83 48 } 84 49 85 /// <summary>86 /// Flag of the current instance whether it is canceled.87 /// </summary>88 private bool canceled;89 /// <summary>90 /// Gets information whether the instance is currently canceled.91 /// </summary>92 protected bool Canceled {93 get { return canceled; }94 private set {95 if (canceled != value) {96 canceled = value;97 OnCanceledChanged();98 }99 }100 }101 /// <summary>102 /// Gets information whether the instance has already terminated.103 /// </summary>104 public bool Finished {105 get { return executionStack.Count == 0; }106 }107 108 /// <summary>109 /// Initializes a new instance of <see cref="EngineBase"/> with a new global scope.110 /// </summary>111 protected Engine() {112 executionStack = new Stack<IOperation>();113 }114 115 /// <summary>116 /// Clones the current instance (deep clone).117 /// </summary>118 /// <remarks>Deep clone through <see cref="cloner.Clone"/> method of helper class119 /// <see cref="Auxiliary"/>.</remarks>120 /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>121 /// <returns>The cloned object as <see cref="EngineBase"/>.</returns>122 50 public override IDeepCloneable Clone(Cloner cloner) { 123 51 Engine clone = (Engine)base.Clone(cloner); 124 clone.executionTime = executionTime;125 52 IOperation[] contexts = executionStack.ToArray(); 126 53 for (int i = contexts.Length - 1; i >= 0; i--) 127 54 clone.executionStack.Push((IOperation)cloner.Clone(contexts[i])); 128 clone. running = running;129 clone. canceled = canceled;55 clone.pausePending = pausePending; 56 clone.stopPending = stopPending; 130 57 return clone; 131 58 } 132 59 60 public sealed override void Prepare() { 61 base.Prepare(); 62 executionStack.Clear(); 63 OnPrepared(); 64 } 133 65 public void Prepare(IOperation initialOperation) { 134 ExecutionTime = new TimeSpan();66 base.Prepare(); 135 67 executionStack.Clear(); 136 68 if (initialOperation != null) … … 138 70 OnPrepared(); 139 71 } 140 /// <inheritdoc/> 141 /// <remarks>Calls <see cref="ThreadPool.QueueUserWorkItem(System.Threading.WaitCallback, object)"/> 142 /// of class <see cref="ThreadPool"/>.</remarks> 143 public void Start() { 72 public override void Start() { 73 base.Start(); 144 74 ThreadPool.QueueUserWorkItem(new WaitCallback(Run), null); 145 75 } 146 /// <inheritdoc/> 147 /// <remarks>Calls <see cref="ThreadPool.QueueUserWorkItem(System.Threading.WaitCallback, object)"/> 148 /// of class <see cref="ThreadPool"/>.</remarks> 149 public void Step() { 150 ThreadPool.QueueUserWorkItem(new WaitCallback(RunStep), null); 76 public override void Pause() { 77 base.Pause(); 78 pausePending = true; 151 79 } 152 /// <inheritdoc/>153 /// <remarks>Sets the protected flag <c>myCanceled</c> to <c>true</c>.</remarks>154 public void Stop() {155 Canceled = true;80 public override void Stop() { 81 base.Stop(); 82 stopPending = true; 83 if (ExecutionState == ExecutionState.Paused) OnStopped(); 156 84 } 157 85 158 86 private void Run(object state) { 159 87 OnStarted(); 160 Running = true;161 Canceled = false; 162 DateTime start= DateTime.Now;163 DateTime end;164 while ( (!Canceled) && (!Finished)) {88 pausePending = stopPending = false; 89 90 lastUpdateTime = DateTime.Now; 91 timer.Start(); 92 while (!pausePending && !stopPending && (executionStack.Count > 0)) { 165 93 ProcessNextOperator(); 166 end = DateTime.Now;167 ExecutionTime += end - start;168 start = end;169 94 } 170 ExecutionTime += DateTime.Now - start; 171 Canceled = false; 172 Running = false; 173 OnStopped(); 174 } 175 private void RunStep(object state) { 176 OnStarted(); 177 Running = true; 178 Canceled = false; 179 DateTime start = DateTime.Now; 180 if ((!Canceled) && (!Finished)) 181 ProcessNextOperator(); 182 ExecutionTime += DateTime.Now - start; 183 Canceled = false; 184 Running = false; 185 OnStopped(); 95 timer.Stop(); 96 ExecutionTime += DateTime.Now - lastUpdateTime; 97 98 if (pausePending) OnPaused(); 99 else OnStopped(); 186 100 } 187 101 188 /// <summary>189 /// Performs the next operation.190 /// </summary>191 102 protected abstract void ProcessNextOperator(); 192 103 193 /// <summary> 194 /// Occurs when the execution time changed. 195 /// </summary> 196 public event EventHandler ExecutionTimeChanged; 197 /// <summary> 198 /// Fires a new <c>ExecutionTimeChanged</c> event. 199 /// </summary> 200 protected virtual void OnExecutionTimeChanged() { 201 if (ExecutionTimeChanged != null) 202 ExecutionTimeChanged(this, EventArgs.Empty); 104 protected override void OnPrepared() { 105 if (executionStack.Count > 0) base.OnPrepared(); 106 else base.OnStopped(); 203 107 } 204 /// <summary> 205 /// Occurs when the running flag changed. 206 /// </summary> 207 public event EventHandler RunningChanged; 208 /// <summary> 209 /// Fires a new <c>RunningChanged</c> event. 210 /// </summary> 211 protected virtual void OnRunningChanged() { 212 if (RunningChanged != null) 213 RunningChanged(this, EventArgs.Empty); 214 } 215 /// <summary> 216 /// Occurs when the execution is prepared for a new run. 217 /// </summary> 218 public event EventHandler Prepared; 219 /// <summary> 220 /// Fires a new <c>Prepared</c> event. 221 /// </summary> 222 protected virtual void OnPrepared() { 223 if (Prepared != null) 224 Prepared(this, EventArgs.Empty); 225 } 226 /// <summary> 227 /// Occurs when the execution is executed. 228 /// </summary> 229 public event EventHandler Started; 230 /// <summary> 231 /// Fires a new <c>Started</c> event. 232 /// </summary> 233 protected virtual void OnStarted() { 234 if (Started != null) 235 Started(this, EventArgs.Empty); 236 } 237 /// <summary> 238 /// Occurs when the execution is finished. 239 /// </summary> 240 public event EventHandler Stopped; 241 /// <summary> 242 /// Fires a new <c>Stopped</c> event. 243 /// </summary> 244 protected virtual void OnStopped() { 245 if (Stopped != null) 246 Stopped(this, EventArgs.Empty); 247 } 248 protected virtual void OnCanceledChanged() { } 249 /// <summary> 250 /// Occurs when an exception occured during the execution. 251 /// </summary> 252 public event EventHandler<EventArgs<Exception>> ExceptionOccurred; 253 /// <summary> 254 /// Aborts the execution and fires a new <c>ExceptionOccurred</c> event. 255 /// </summary> 256 /// <param name="exception">The exception that was thrown.</param> 257 protected virtual void OnExceptionOccurred(Exception exception) { 258 if (ExceptionOccurred != null) 259 ExceptionOccurred(this, new EventArgs<Exception>(exception)); 108 109 private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { 110 DateTime now = DateTime.Now; 111 ExecutionTime += now - lastUpdateTime; 112 lastUpdateTime = now; 260 113 } 261 114 } -
trunk/sources/HeuristicLab.Core/3.3/HeuristicLab.Core-3.3.csproj
r2932 r3262 103 103 <Compile Include="Attributes\CreatableAttribute.cs" /> 104 104 <None Include="HeuristicLabCorePlugin.cs.frame" /> 105 <Compile Include="Executable.cs" /> 106 <Compile Include="ExecutionState.cs" /> 107 <Compile Include="Interfaces\IExecutable.cs" /> 105 108 <Compile Include="Interfaces\IParameterizedNamedItem.cs" /> 106 109 <Compile Include="Interfaces\IParameterizedItem.cs" /> -
trunk/sources/HeuristicLab.Core/3.3/Interfaces/IEngine.cs
r3226 r3262 24 24 25 25 namespace HeuristicLab.Core { 26 /// <summary> 27 /// Interface to represent one run. (An engine is an interpreter, holding the code, 28 /// the data and the actual state, which is the runtime stack and a pointer onto the next operation.). 29 /// It is responsible for operator execution and able to deal with parallelism. 30 /// </summary> 31 public interface IEngine : IItem { 32 /// <summary> 33 /// Gets the execution time of the current instance. 34 /// </summary> 35 TimeSpan ExecutionTime { get; } 36 37 /// <summary> 38 /// Gets information whether the engine is currently running. 39 /// </summary> 40 bool Running { get; } 41 /// <summary> 42 /// Gets information whether the engine has already terminated. 43 /// </summary> 44 bool Finished { get; } 45 46 /// <summary> 47 /// Prepares the engine with a given initial operation. 48 /// </summary> 26 public interface IEngine : IExecutable { 49 27 void Prepare(IOperation initialOperation); 50 /// <summary>51 /// Executes the whole run.52 /// </summary>53 void Start();54 /// <summary>55 /// Executes one step (one operation).56 /// </summary>57 void Step();58 /// <summary>59 /// Aborts the engine run.60 /// </summary>61 void Stop();62 63 /// <summary>64 /// Occurs when the execution time was changed.65 /// </summary>66 event EventHandler ExecutionTimeChanged;67 /// <summary>68 /// Occurs when the running flag was changed.69 /// </summary>70 event EventHandler RunningChanged;71 /// <summary>72 /// Occurs when the engine is prepared for a new run.73 /// </summary>74 event EventHandler Prepared;75 /// <summary>76 /// Occurs when the engine is executed.77 /// </summary>78 event EventHandler Started;79 /// <summary>80 /// Occurs when the engine is finished.81 /// </summary>82 event EventHandler Stopped;83 /// <summary>84 /// Occurs when an exception was thrown.85 /// </summary>86 event EventHandler<EventArgs<Exception>> ExceptionOccurred;87 28 } 88 29 }
Note: See TracChangeset
for help on using the changeset viewer.