Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/Engine.cs @ 3188

Last change on this file since 3188 was 3017, checked in by epitzer, 14 years ago

Merge StorableClassType.Empty into StorableClassType.MarkedOnly and make it the default if not specified (#548)

File size: 8.1 KB
RevLine 
[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
22using System;
23using System.Collections.Generic;
[2796]24using System.Drawing;
[2]25using System.Threading;
[2796]26using HeuristicLab.Common;
[1823]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]28
29namespace 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; }
[2]77    }
[776]78
79    /// <summary>
80    /// Flag of the current instance whether it is canceled.
81    /// </summary>
[2653]82    private bool canceled;
[776]83    /// <summary>
84    /// Gets information whether the instance is currently canceled.
85    /// </summary>
[2653]86    protected bool Canceled {
87      get { return canceled; }
[2851]88      private set {
89        if (canceled != value) {
90          canceled = value;
91          OnCanceledChanged();
92        }
93      }
[2]94    }
[776]95    /// <summary>
96    /// Gets information whether the instance has already terminated.
97    /// </summary>
[2653]98    public bool Finished {
99      get { return executionStack.Count == 0; }
[2]100    }
101
[776]102    /// <summary>
103    /// Initializes a new instance of <see cref="EngineBase"/> with a new global scope.
104    /// </summary>
[2664]105    protected Engine() {
[2834]106      executionStack = new Stack<IOperation>();
[2]107    }
108
[776]109    /// <summary>
110    /// Clones the current instance (deep clone).
111    /// </summary>
[2526]112    /// <remarks>Deep clone through <see cref="cloner.Clone"/> method of helper class
[776]113    /// <see cref="Auxiliary"/>.</remarks>
114    /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>
115    /// <returns>The cloned object as <see cref="EngineBase"/>.</returns>
[2653]116    public override IDeepCloneable Clone(Cloner cloner) {
[2664]117      Engine clone = (Engine)base.Clone(cloner);
[2653]118      clone.executionTime = executionTime;
[2834]119      IOperation[] contexts = executionStack.ToArray();
[2653]120      for (int i = contexts.Length - 1; i >= 0; i--)
[2834]121        clone.executionStack.Push((IOperation)cloner.Clone(contexts[i]));
[2653]122      clone.running = running;
123      clone.canceled = canceled;
[2]124      return clone;
125    }
126
[2834]127    public void Prepare(IOperation initialOperation) {
[2851]128      Canceled = false;
[2653]129      running = false;
130      ExecutionTime = new TimeSpan();
131      executionStack.Clear();
[2834]132      if (initialOperation != null)
133        executionStack.Push(initialOperation);
[2790]134      OnPrepared();
[2]135    }
[776]136    /// <inheritdoc/>
137    /// <remarks>Calls <see cref="ThreadPool.QueueUserWorkItem(System.Threading.WaitCallback, object)"/>
138    /// of class <see cref="ThreadPool"/>.</remarks>
[2653]139    public void Start() {
140      running = true;
[2851]141      Canceled = false;
[2653]142      ThreadPool.QueueUserWorkItem(new WaitCallback(Run), null);
[2]143    }
[776]144    /// <inheritdoc/>
145    /// <remarks>Calls <see cref="ThreadPool.QueueUserWorkItem(System.Threading.WaitCallback, object)"/>
146    /// of class <see cref="ThreadPool"/>.</remarks>
[2653]147    public void Step() {
148      running = true;
[2851]149      Canceled = false;
[2653]150      ThreadPool.QueueUserWorkItem(new WaitCallback(RunStep), null);
[2]151    }
[776]152    /// <inheritdoc/>
153    /// <remarks>Sets the protected flag <c>myCanceled</c> to <c>true</c>.</remarks>
[2851]154    public void Stop() {
155      Canceled = true;
[2]156    }
157
158    private void Run(object state) {
[2653]159      OnStarted();
[2]160      DateTime start = DateTime.Now;
161      DateTime end;
[2653]162      while ((!Canceled) && (!Finished)) {
163        ProcessNextOperator();
[2]164        end = DateTime.Now;
165        ExecutionTime += end - start;
166        start = end;
167      }
168      ExecutionTime += DateTime.Now - start;
[2653]169      running = false;
170      OnStopped();
[2]171    }
[2653]172    private void RunStep(object state) {
173      OnStarted();
[2]174      DateTime start = DateTime.Now;
[2653]175      if ((!Canceled) && (!Finished))
176        ProcessNextOperator();
[2]177      ExecutionTime += DateTime.Now - start;
[2653]178      running = false;
179      OnStopped();
[2]180    }
181
[776]182    /// <summary>
183    /// Performs the next operation.
184    /// </summary>
[2653]185    protected abstract void ProcessNextOperator();
[2]186
[2796]187    /// <summary>
[2653]188    /// Occurs when the execution time changed.
[776]189    /// </summary>
[2653]190    public event EventHandler ExecutionTimeChanged;
191    /// <summary>
192    /// Fires a new <c>ExecutionTimeChanged</c> event.
193    /// </summary>
194    protected virtual void OnExecutionTimeChanged() {
195      if (ExecutionTimeChanged != null)
[2793]196        ExecutionTimeChanged(this, EventArgs.Empty);
[2653]197    }
198    /// <summary>
[2790]199    /// Occurs when the execution is prepared for a new run.
[2653]200    /// </summary>
[2790]201    public event EventHandler Prepared;
[776]202    /// <summary>
[2790]203    /// Fires a new <c>Prepared</c> event.
[776]204    /// </summary>
[2790]205    protected virtual void OnPrepared() {
206      if (Prepared != null)
[2793]207        Prepared(this, EventArgs.Empty);
[2]208    }
[776]209    /// <summary>
[2653]210    /// Occurs when the execution is executed.
[776]211    /// </summary>
[2653]212    public event EventHandler Started;
[776]213    /// <summary>
[2653]214    /// Fires a new <c>Started</c> event.
[776]215    /// </summary>
[2653]216    protected virtual void OnStarted() {
217      if (Started != null)
[2793]218        Started(this, EventArgs.Empty);
[2]219    }
[776]220    /// <summary>
[2653]221    /// Occurs when the execution is finished.
222    /// </summary>
223    public event EventHandler Stopped;
224    /// <summary>
225    /// Fires a new <c>Stopped</c> event.
226    /// </summary>
227    protected virtual void OnStopped() {
228      if (Stopped != null)
[2793]229        Stopped(this, EventArgs.Empty);
[2653]230    }
[2851]231    protected virtual void OnCanceledChanged() { }
[2653]232    /// <summary>
[776]233    /// Occurs when an exception occured during the execution.
234    /// </summary>
[2474]235    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
[776]236    /// <summary>
237    /// Aborts the execution and fires a new <c>ExceptionOccurred</c> event.
238    /// </summary>
239    /// <param name="exception">The exception that was thrown.</param>
[2]240    protected virtual void OnExceptionOccurred(Exception exception) {
241      if (ExceptionOccurred != null)
[2474]242        ExceptionOccurred(this, new EventArgs<Exception>(exception));
[2]243    }
244  }
245}
Note: See TracBrowser for help on using the repository browser.