[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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.Collections.Generic;
|
---|
[2796] | 24 | using System.Drawing;
|
---|
[2] | 25 | using System.Threading;
|
---|
[2796] | 26 | using HeuristicLab.Common;
|
---|
[1823] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 28 |
|
---|
| 29 | namespace HeuristicLab.Core {
|
---|
[776] | 30 | /// <summary>
|
---|
| 31 | /// Base class to represent an engine, which is an interpreter, holding the code, the data and
|
---|
| 32 | /// the actual state, which is the runtime stack and a pointer onto the next operation. It represents
|
---|
| 33 | /// one execution and can handle parallel executions.
|
---|
| 34 | /// </summary>
|
---|
[2664] | 35 | [Item("Engine", "A base class for engines.")]
|
---|
[3017] | 36 | [StorableClass]
|
---|
[2664] | 37 | public abstract class Engine : Item, IEngine {
|
---|
[2653] | 38 | public override Image ItemImage {
|
---|
| 39 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event; }
|
---|
| 40 | }
|
---|
[1667] | 41 |
|
---|
[2653] | 42 | [Storable]
|
---|
| 43 | private TimeSpan executionTime;
|
---|
[776] | 44 | /// <summary>
|
---|
| 45 | /// Gets or sets the execution time.
|
---|
| 46 | /// </summary>
|
---|
| 47 | /// <remarks>Calls <see cref="OnExecutionTimeChanged"/> in the setter.</remarks>
|
---|
[2] | 48 | public TimeSpan ExecutionTime {
|
---|
[2653] | 49 | get { return executionTime; }
|
---|
[2] | 50 | protected set {
|
---|
[2653] | 51 | executionTime = value;
|
---|
[2] | 52 | OnExecutionTimeChanged();
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[776] | 56 | /// <summary>
|
---|
| 57 | /// Field of the current instance that represent the execution stack.
|
---|
| 58 | /// </summary>
|
---|
[1667] | 59 | [Storable]
|
---|
[2834] | 60 | private Stack<IOperation> executionStack;
|
---|
[776] | 61 | /// <summary>
|
---|
| 62 | /// Gets the current execution stack.
|
---|
| 63 | /// </summary>
|
---|
[2834] | 64 | protected Stack<IOperation> ExecutionStack {
|
---|
[2653] | 65 | get { return executionStack; }
|
---|
[2] | 66 | }
|
---|
[1667] | 67 |
|
---|
[776] | 68 | /// <summary>
|
---|
| 69 | /// Flag of the current instance whether it is currently running.
|
---|
| 70 | /// </summary>
|
---|
[2653] | 71 | private bool running;
|
---|
[776] | 72 | /// <summary>
|
---|
| 73 | /// Gets information whether the instance is currently running.
|
---|
| 74 | /// </summary>
|
---|
[2] | 75 | public bool Running {
|
---|
[2653] | 76 | get { return running; }
|
---|
[3226] | 77 | private set {
|
---|
| 78 | if (running != value) {
|
---|
| 79 | running = value;
|
---|
| 80 | OnRunningChanged();
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
[2] | 83 | }
|
---|
[776] | 84 |
|
---|
| 85 | /// <summary>
|
---|
| 86 | /// Flag of the current instance whether it is canceled.
|
---|
| 87 | /// </summary>
|
---|
[2653] | 88 | private bool canceled;
|
---|
[776] | 89 | /// <summary>
|
---|
| 90 | /// Gets information whether the instance is currently canceled.
|
---|
| 91 | /// </summary>
|
---|
[2653] | 92 | protected bool Canceled {
|
---|
| 93 | get { return canceled; }
|
---|
[2851] | 94 | private set {
|
---|
| 95 | if (canceled != value) {
|
---|
| 96 | canceled = value;
|
---|
| 97 | OnCanceledChanged();
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
[2] | 100 | }
|
---|
[776] | 101 | /// <summary>
|
---|
| 102 | /// Gets information whether the instance has already terminated.
|
---|
| 103 | /// </summary>
|
---|
[2653] | 104 | public bool Finished {
|
---|
| 105 | get { return executionStack.Count == 0; }
|
---|
[2] | 106 | }
|
---|
| 107 |
|
---|
[776] | 108 | /// <summary>
|
---|
| 109 | /// Initializes a new instance of <see cref="EngineBase"/> with a new global scope.
|
---|
| 110 | /// </summary>
|
---|
[2664] | 111 | protected Engine() {
|
---|
[2834] | 112 | executionStack = new Stack<IOperation>();
|
---|
[2] | 113 | }
|
---|
| 114 |
|
---|
[776] | 115 | /// <summary>
|
---|
| 116 | /// Clones the current instance (deep clone).
|
---|
| 117 | /// </summary>
|
---|
[2526] | 118 | /// <remarks>Deep clone through <see cref="cloner.Clone"/> method of helper class
|
---|
[776] | 119 | /// <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>
|
---|
[2653] | 122 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[2664] | 123 | Engine clone = (Engine)base.Clone(cloner);
|
---|
[2653] | 124 | clone.executionTime = executionTime;
|
---|
[2834] | 125 | IOperation[] contexts = executionStack.ToArray();
|
---|
[2653] | 126 | for (int i = contexts.Length - 1; i >= 0; i--)
|
---|
[2834] | 127 | clone.executionStack.Push((IOperation)cloner.Clone(contexts[i]));
|
---|
[2653] | 128 | clone.running = running;
|
---|
| 129 | clone.canceled = canceled;
|
---|
[2] | 130 | return clone;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[2834] | 133 | public void Prepare(IOperation initialOperation) {
|
---|
[2653] | 134 | ExecutionTime = new TimeSpan();
|
---|
| 135 | executionStack.Clear();
|
---|
[2834] | 136 | if (initialOperation != null)
|
---|
| 137 | executionStack.Push(initialOperation);
|
---|
[2790] | 138 | OnPrepared();
|
---|
[2] | 139 | }
|
---|
[776] | 140 | /// <inheritdoc/>
|
---|
| 141 | /// <remarks>Calls <see cref="ThreadPool.QueueUserWorkItem(System.Threading.WaitCallback, object)"/>
|
---|
| 142 | /// of class <see cref="ThreadPool"/>.</remarks>
|
---|
[2653] | 143 | public void Start() {
|
---|
[3226] | 144 | Running = true;
|
---|
[2851] | 145 | Canceled = false;
|
---|
[2653] | 146 | ThreadPool.QueueUserWorkItem(new WaitCallback(Run), null);
|
---|
[2] | 147 | }
|
---|
[776] | 148 | /// <inheritdoc/>
|
---|
| 149 | /// <remarks>Calls <see cref="ThreadPool.QueueUserWorkItem(System.Threading.WaitCallback, object)"/>
|
---|
| 150 | /// of class <see cref="ThreadPool"/>.</remarks>
|
---|
[2653] | 151 | public void Step() {
|
---|
[3226] | 152 | Running = true;
|
---|
[2851] | 153 | Canceled = false;
|
---|
[2653] | 154 | ThreadPool.QueueUserWorkItem(new WaitCallback(RunStep), null);
|
---|
[2] | 155 | }
|
---|
[776] | 156 | /// <inheritdoc/>
|
---|
| 157 | /// <remarks>Sets the protected flag <c>myCanceled</c> to <c>true</c>.</remarks>
|
---|
[2851] | 158 | public void Stop() {
|
---|
| 159 | Canceled = true;
|
---|
[2] | 160 | }
|
---|
| 161 |
|
---|
| 162 | private void Run(object state) {
|
---|
[2653] | 163 | OnStarted();
|
---|
[2] | 164 | DateTime start = DateTime.Now;
|
---|
| 165 | DateTime end;
|
---|
[2653] | 166 | while ((!Canceled) && (!Finished)) {
|
---|
| 167 | ProcessNextOperator();
|
---|
[2] | 168 | end = DateTime.Now;
|
---|
| 169 | ExecutionTime += end - start;
|
---|
| 170 | start = end;
|
---|
| 171 | }
|
---|
| 172 | ExecutionTime += DateTime.Now - start;
|
---|
[2653] | 173 | OnStopped();
|
---|
[2] | 174 | }
|
---|
[2653] | 175 | private void RunStep(object state) {
|
---|
| 176 | OnStarted();
|
---|
[2] | 177 | DateTime start = DateTime.Now;
|
---|
[2653] | 178 | if ((!Canceled) && (!Finished))
|
---|
| 179 | ProcessNextOperator();
|
---|
[2] | 180 | ExecutionTime += DateTime.Now - start;
|
---|
[2653] | 181 | OnStopped();
|
---|
[2] | 182 | }
|
---|
| 183 |
|
---|
[776] | 184 | /// <summary>
|
---|
| 185 | /// Performs the next operation.
|
---|
| 186 | /// </summary>
|
---|
[2653] | 187 | protected abstract void ProcessNextOperator();
|
---|
[2] | 188 |
|
---|
[2796] | 189 | /// <summary>
|
---|
[2653] | 190 | /// Occurs when the execution time changed.
|
---|
[776] | 191 | /// </summary>
|
---|
[2653] | 192 | public event EventHandler ExecutionTimeChanged;
|
---|
| 193 | /// <summary>
|
---|
| 194 | /// Fires a new <c>ExecutionTimeChanged</c> event.
|
---|
| 195 | /// </summary>
|
---|
| 196 | protected virtual void OnExecutionTimeChanged() {
|
---|
| 197 | if (ExecutionTimeChanged != null)
|
---|
[2793] | 198 | ExecutionTimeChanged(this, EventArgs.Empty);
|
---|
[2653] | 199 | }
|
---|
| 200 | /// <summary>
|
---|
[3226] | 201 | /// Occurs when the running flag changed.
|
---|
| 202 | /// </summary>
|
---|
| 203 | public event EventHandler RunningChanged;
|
---|
| 204 | /// <summary>
|
---|
| 205 | /// Fires a new <c>RunningChanged</c> event.
|
---|
| 206 | /// </summary>
|
---|
| 207 | protected virtual void OnRunningChanged() {
|
---|
| 208 | if (RunningChanged != null)
|
---|
| 209 | RunningChanged(this, EventArgs.Empty);
|
---|
| 210 | }
|
---|
| 211 | /// <summary>
|
---|
[2790] | 212 | /// Occurs when the execution is prepared for a new run.
|
---|
[2653] | 213 | /// </summary>
|
---|
[2790] | 214 | public event EventHandler Prepared;
|
---|
[776] | 215 | /// <summary>
|
---|
[2790] | 216 | /// Fires a new <c>Prepared</c> event.
|
---|
[776] | 217 | /// </summary>
|
---|
[2790] | 218 | protected virtual void OnPrepared() {
|
---|
| 219 | if (Prepared != null)
|
---|
[2793] | 220 | Prepared(this, EventArgs.Empty);
|
---|
[2] | 221 | }
|
---|
[776] | 222 | /// <summary>
|
---|
[2653] | 223 | /// Occurs when the execution is executed.
|
---|
[776] | 224 | /// </summary>
|
---|
[2653] | 225 | public event EventHandler Started;
|
---|
[776] | 226 | /// <summary>
|
---|
[2653] | 227 | /// Fires a new <c>Started</c> event.
|
---|
[776] | 228 | /// </summary>
|
---|
[2653] | 229 | protected virtual void OnStarted() {
|
---|
| 230 | if (Started != null)
|
---|
[2793] | 231 | Started(this, EventArgs.Empty);
|
---|
[2] | 232 | }
|
---|
[776] | 233 | /// <summary>
|
---|
[2653] | 234 | /// Occurs when the execution is finished.
|
---|
| 235 | /// </summary>
|
---|
| 236 | public event EventHandler Stopped;
|
---|
| 237 | /// <summary>
|
---|
| 238 | /// Fires a new <c>Stopped</c> event.
|
---|
| 239 | /// </summary>
|
---|
| 240 | protected virtual void OnStopped() {
|
---|
| 241 | if (Stopped != null)
|
---|
[2793] | 242 | Stopped(this, EventArgs.Empty);
|
---|
[3226] | 243 | Canceled = false;
|
---|
| 244 | Running = false;
|
---|
[2653] | 245 | }
|
---|
[2851] | 246 | protected virtual void OnCanceledChanged() { }
|
---|
[2653] | 247 | /// <summary>
|
---|
[776] | 248 | /// Occurs when an exception occured during the execution.
|
---|
| 249 | /// </summary>
|
---|
[2474] | 250 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
[776] | 251 | /// <summary>
|
---|
| 252 | /// Aborts the execution and fires a new <c>ExceptionOccurred</c> event.
|
---|
| 253 | /// </summary>
|
---|
| 254 | /// <param name="exception">The exception that was thrown.</param>
|
---|
[2] | 255 | protected virtual void OnExceptionOccurred(Exception exception) {
|
---|
| 256 | if (ExceptionOccurred != null)
|
---|
[2474] | 257 | ExceptionOccurred(this, new EventArgs<Exception>(exception));
|
---|
[2] | 258 | }
|
---|
| 259 | }
|
---|
| 260 | }
|
---|