Changeset 2653 for trunk/sources/HeuristicLab.Core/3.3/EngineBase.cs
- Timestamp:
- 01/20/10 05:00:28 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Core/3.3/EngineBase.cs
r2526 r2653 25 25 using System.Xml; 26 26 using System.Threading; 27 using System.Drawing; 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 29 using HeuristicLab.Common; … … 34 35 /// one execution and can handle parallel executions. 35 36 /// </summary> 37 [Item("EngineBase", "A base class for engines.")] 36 38 public abstract class EngineBase : ItemBase, IEngine { 39 public override Image ItemImage { 40 get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event; } 41 } 37 42 38 43 /// <summary> 39 44 /// Field of the current instance that represent the operator graph. 40 45 /// </summary> 41 [Storable] 42 protected IOperatorGraph myOperatorGraph; 46 private OperatorGraph operatorGraph; 43 47 /// <summary> 44 48 /// Gets the current operator graph. 45 49 /// </summary> 46 public IOperatorGraph OperatorGraph { 47 get { return myOperatorGraph; } 48 } 50 [Storable] 51 public OperatorGraph OperatorGraph { 52 get { return operatorGraph; } 53 set { 54 if (value == null) throw new ArgumentNullException(); 55 if (value != operatorGraph) { 56 if (operatorGraph != null) operatorGraph.InitialOperatorChanged -= new EventHandler(operatorGraph_InitialOperatorChanged); 57 operatorGraph = value; 58 if (operatorGraph != null) operatorGraph.InitialOperatorChanged += new EventHandler(operatorGraph_InitialOperatorChanged); 59 OnOperatorGraphChanged(); 60 Initialize(); 61 } 62 } 63 } 64 49 65 /// <summary> 50 66 /// Field of the current instance that represent the global scope. 51 67 /// </summary> 52 68 [Storable] 53 pr otected IScope myGlobalScope;69 private Scope globalScope; 54 70 /// <summary> 55 71 /// Gets the current global scope. 56 72 /// </summary> 57 public IScope GlobalScope {58 get { return myGlobalScope; }59 } 60 61 [Storable] 62 private TimeSpan myExecutionTime;73 public Scope GlobalScope { 74 get { return globalScope; } 75 } 76 77 [Storable] 78 private TimeSpan executionTime; 63 79 /// <summary> 64 80 /// Gets or sets the execution time. … … 66 82 /// <remarks>Calls <see cref="OnExecutionTimeChanged"/> in the setter.</remarks> 67 83 public TimeSpan ExecutionTime { 68 get { return myExecutionTime; }84 get { return executionTime; } 69 85 protected set { 70 myExecutionTime = value;86 executionTime = value; 71 87 OnExecutionTimeChanged(); 72 88 } … … 77 93 /// </summary> 78 94 [Storable] 79 pr otected Stack<IOperation> myExecutionStack;95 private Stack<ExecutionContext> executionStack; 80 96 /// <summary> 81 97 /// Gets the current execution stack. 82 98 /// </summary> 83 p ublic Stack<IOperation> ExecutionStack {84 get { return myExecutionStack; }99 protected Stack<ExecutionContext> ExecutionStack { 100 get { return executionStack; } 85 101 } 86 102 … … 88 104 /// Flag of the current instance whether it is currently running. 89 105 /// </summary> 90 pr otected bool myRunning;106 private bool running; 91 107 /// <summary> 92 108 /// Gets information whether the instance is currently running. 93 109 /// </summary> 94 110 public bool Running { 95 get { return myRunning; }111 get { return running; } 96 112 } 97 113 … … 99 115 /// Flag of the current instance whether it is canceled. 100 116 /// </summary> 101 pr otected bool myCanceled;117 private bool canceled; 102 118 /// <summary> 103 119 /// Gets information whether the instance is currently canceled. 104 120 /// </summary> 105 p ublicbool Canceled {106 get { return myCanceled; }121 protected bool Canceled { 122 get { return canceled; } 107 123 } 108 124 /// <summary> 109 125 /// Gets information whether the instance has already terminated. 110 126 /// </summary> 111 public virtual bool Terminated {112 get { return ExecutionStack.Count == 0; }127 public bool Finished { 128 get { return executionStack.Count == 0; } 113 129 } 114 130 … … 118 134 /// <remarks>Calls <see cref="Reset"/>.</remarks> 119 135 protected EngineBase() { 120 myOperatorGraph = new OperatorGraph(); 121 myGlobalScope = new Scope("Global"); 122 myExecutionStack = new Stack<IOperation>(); 123 Reset(); 136 globalScope = new Scope("Global"); 137 executionStack = new Stack<ExecutionContext>(); 138 OperatorGraph = new OperatorGraph(); 124 139 } 125 140 … … 131 146 /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param> 132 147 /// <returns>The cloned object as <see cref="EngineBase"/>.</returns> 133 public override I Item Clone(ICloner cloner) {148 public override IDeepCloneable Clone(Cloner cloner) { 134 149 EngineBase clone = (EngineBase)base.Clone(cloner); 135 clone.myOperatorGraph = (IOperatorGraph)cloner.Clone(OperatorGraph); 136 clone.myGlobalScope = (IScope)cloner.Clone(GlobalScope); 137 clone.myExecutionTime = ExecutionTime; 138 IOperation[] operations = new IOperation[ExecutionStack.Count]; 139 ExecutionStack.CopyTo(operations, 0); 140 for (int i = operations.Length - 1; i >= 0; i--) 141 clone.myExecutionStack.Push((IOperation)cloner.Clone(operations[i])); 142 clone.myRunning = Running; 143 clone.myCanceled = Canceled; 150 clone.OperatorGraph = (OperatorGraph)cloner.Clone(operatorGraph); 151 clone.globalScope = (Scope)cloner.Clone(globalScope); 152 clone.executionTime = executionTime; 153 ExecutionContext[] contexts = executionStack.ToArray(); 154 for (int i = contexts.Length - 1; i >= 0; i--) 155 clone.executionStack.Push((ExecutionContext)cloner.Clone(contexts[i])); 156 clone.running = running; 157 clone.canceled = canceled; 144 158 return clone; 145 159 } 146 160 147 /// <inheritdoc/>148 /// <remarks>Calls <see cref="ThreadPool.QueueUserWorkItem(System.Threading.WaitCallback, object)"/>149 /// of class <see cref="ThreadPool"/>.</remarks>150 public virtual void Execute() {151 myRunning = true;152 myCanceled = false;153 ThreadPool.QueueUserWorkItem(new WaitCallback(Run), null);154 }155 /// <inheritdoc/>156 /// <remarks>Calls <see cref="ThreadPool.QueueUserWorkItem(System.Threading.WaitCallback, object)"/>157 /// of class <see cref="ThreadPool"/>.</remarks>158 public virtual void ExecuteSteps(int steps) {159 myRunning = true;160 myCanceled = false;161 ThreadPool.QueueUserWorkItem(new WaitCallback(Run), steps);162 }163 /// <inheritdoc/>164 /// <remarks>Calls <see cref="ThreadPool.QueueUserWorkItem(System.Threading.WaitCallback, object)"/>165 /// of class <see cref="ThreadPool"/>.</remarks>166 public void ExecuteStep() {167 ExecuteSteps(1);168 }169 /// <inheritdoc/>170 /// <remarks>Sets the protected flag <c>myCanceled</c> to <c>true</c>.</remarks>171 public virtual void Abort() {172 myCanceled = true;173 }174 161 /// <inheritdoc/> 175 162 /// <remarks>Sets <c>myCanceled</c> and <c>myRunning</c> to <c>false</c>. The global scope is cleared, … … 177 164 /// with the initial operator is added. <br/> 178 165 /// Calls <see cref="OnInitialized"/>.</remarks> 179 public v irtual void Reset() {180 myCanceled = false;181 myRunning = false;166 public void Initialize() { 167 canceled = false; 168 running = false; 182 169 GlobalScope.Clear(); 183 170 ExecutionTime = new TimeSpan(); 184 myExecutionStack.Clear();171 executionStack.Clear(); 185 172 if (OperatorGraph.InitialOperator != null) 186 myExecutionStack.Push(new AtomicOperation(OperatorGraph.InitialOperator, GlobalScope));173 executionStack.Push(new ExecutionContext(null, OperatorGraph.InitialOperator, GlobalScope)); 187 174 OnInitialized(); 188 175 } 176 /// <inheritdoc/> 177 /// <remarks>Calls <see cref="ThreadPool.QueueUserWorkItem(System.Threading.WaitCallback, object)"/> 178 /// of class <see cref="ThreadPool"/>.</remarks> 179 public void Start() { 180 running = true; 181 canceled = false; 182 ThreadPool.QueueUserWorkItem(new WaitCallback(Run), null); 183 } 184 /// <inheritdoc/> 185 /// <remarks>Calls <see cref="ThreadPool.QueueUserWorkItem(System.Threading.WaitCallback, object)"/> 186 /// of class <see cref="ThreadPool"/>.</remarks> 187 public void Step() { 188 running = true; 189 canceled = false; 190 ThreadPool.QueueUserWorkItem(new WaitCallback(RunStep), null); 191 } 192 /// <inheritdoc/> 193 /// <remarks>Sets the protected flag <c>myCanceled</c> to <c>true</c>.</remarks> 194 public virtual void Stop() { 195 canceled = true; 196 } 189 197 190 198 private void Run(object state) { 191 if (state == null) Run(); 192 else RunSteps((int)state); 193 myRunning = false; 194 OnFinished(); 195 } 196 private void Run() { 199 OnStarted(); 197 200 DateTime start = DateTime.Now; 198 201 DateTime end; 199 while ((!Canceled) && (! Terminated)) {200 ProcessNextOperat ion();202 while ((!Canceled) && (!Finished)) { 203 ProcessNextOperator(); 201 204 end = DateTime.Now; 202 205 ExecutionTime += end - start; … … 204 207 } 205 208 ExecutionTime += DateTime.Now - start; 206 } 207 private void RunSteps(int steps) { 209 running = false; 210 OnStopped(); 211 } 212 private void RunStep(object state) { 213 OnStarted(); 208 214 DateTime start = DateTime.Now; 209 DateTime end; 210 int step = 0; 211 while ((!Canceled) && (!Terminated) && (step < steps)) { 212 ProcessNextOperation(); 213 step++; 214 end = DateTime.Now; 215 ExecutionTime += end - start; 216 start = end; 217 } 215 if ((!Canceled) && (!Finished)) 216 ProcessNextOperator(); 218 217 ExecutionTime += DateTime.Now - start; 218 running = false; 219 OnStopped(); 219 220 } 220 221 … … 222 223 /// Performs the next operation. 223 224 /// </summary> 224 protected abstract void ProcessNextOperation(); 225 226 /// <summary> 227 /// Occurs when the current instance is initialized. 225 protected abstract void ProcessNextOperator(); 226 227 private void operatorGraph_InitialOperatorChanged(object sender, EventArgs e) { 228 Initialize(); 229 } 230 231 public event EventHandler OperatorGraphChanged; 232 protected virtual void OnOperatorGraphChanged() { 233 if (OperatorGraphChanged != null) 234 OperatorGraphChanged(this, EventArgs.Empty); 235 } 236 /// <summary> 237 /// Occurs when the execution time changed. 238 /// </summary> 239 public event EventHandler ExecutionTimeChanged; 240 /// <summary> 241 /// Fires a new <c>ExecutionTimeChanged</c> event. 242 /// </summary> 243 protected virtual void OnExecutionTimeChanged() { 244 if (ExecutionTimeChanged != null) 245 ExecutionTimeChanged(this, new EventArgs()); 246 } 247 /// <summary> 248 /// Occurs when the execution is initialized. 228 249 /// </summary> 229 250 public event EventHandler Initialized; … … 236 257 } 237 258 /// <summary> 238 /// Occurs when an operation is executed. 239 /// </summary> 240 public event EventHandler<EventArgs<IOperation>> OperationExecuted; 241 /// <summary> 242 /// Fires a new <c>OperationExecuted</c> event. 243 /// </summary> 244 /// <param name="operation">The operation that has been executed.</param> 245 protected virtual void OnOperationExecuted(IOperation operation) { 246 if (OperationExecuted != null) 247 OperationExecuted(this, new EventArgs<IOperation>(operation)); 259 /// Occurs when the execution is executed. 260 /// </summary> 261 public event EventHandler Started; 262 /// <summary> 263 /// Fires a new <c>Started</c> event. 264 /// </summary> 265 protected virtual void OnStarted() { 266 if (Started != null) 267 Started(this, new EventArgs()); 268 } 269 /// <summary> 270 /// Occurs when the execution is finished. 271 /// </summary> 272 public event EventHandler Stopped; 273 /// <summary> 274 /// Fires a new <c>Stopped</c> event. 275 /// </summary> 276 protected virtual void OnStopped() { 277 if (Stopped != null) 278 Stopped(this, new EventArgs()); 248 279 } 249 280 /// <summary> … … 256 287 /// <param name="exception">The exception that was thrown.</param> 257 288 protected virtual void OnExceptionOccurred(Exception exception) { 258 Abort();259 289 if (ExceptionOccurred != null) 260 290 ExceptionOccurred(this, new EventArgs<Exception>(exception)); 261 291 } 262 /// <summary>263 /// Occurs when the execution time changed.264 /// </summary>265 public event EventHandler ExecutionTimeChanged;266 /// <summary>267 /// Fires a new <c>ExecutionTimeChanged</c> event.268 /// </summary>269 protected virtual void OnExecutionTimeChanged() {270 if (ExecutionTimeChanged != null)271 ExecutionTimeChanged(this, new EventArgs());272 }273 /// <summary>274 /// Occurs when the execution is finished.275 /// </summary>276 public event EventHandler Finished;277 /// <summary>278 /// Fires a new <c>Finished</c> event.279 /// </summary>280 protected virtual void OnFinished() {281 if (Finished != null)282 Finished(this, new EventArgs());283 }284 292 } 285 293 }
Note: See TracChangeset
for help on using the changeset viewer.