Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/20/10 05:00:28 (14 years ago)
Author:
swagner
Message:

Committing first results of the refactoring of HeuristicLab.Core and related plugins (#95)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Core/3.3/EngineBase.cs

    r2526 r2653  
    2525using System.Xml;
    2626using System.Threading;
     27using System.Drawing;
    2728using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2829using HeuristicLab.Common;
     
    3435  /// one execution and can handle parallel executions.
    3536  /// </summary>
     37  [Item("EngineBase", "A base class for engines.")]
    3638  public abstract class EngineBase : ItemBase, IEngine {
     39    public override Image ItemImage {
     40      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event; }
     41    }
    3742
    3843    /// <summary>
    3944    /// Field of the current instance that represent the operator graph.
    4045    /// </summary>
    41     [Storable]
    42     protected IOperatorGraph myOperatorGraph;
     46    private OperatorGraph operatorGraph;
    4347    /// <summary>
    4448    /// Gets the current operator graph.
    4549    /// </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
    4965    /// <summary>
    5066    /// Field of the current instance that represent the global scope.
    5167    /// </summary>
    5268    [Storable]
    53     protected IScope myGlobalScope;
     69    private Scope globalScope;
    5470    /// <summary>
    5571    /// Gets the current global scope.
    5672    /// </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;
    6379    /// <summary>
    6480    /// Gets or sets the execution time.
     
    6682    /// <remarks>Calls <see cref="OnExecutionTimeChanged"/> in the setter.</remarks>
    6783    public TimeSpan ExecutionTime {
    68       get { return myExecutionTime; }
     84      get { return executionTime; }
    6985      protected set {
    70         myExecutionTime = value;
     86        executionTime = value;
    7187        OnExecutionTimeChanged();
    7288      }
     
    7793    /// </summary>
    7894    [Storable]
    79     protected Stack<IOperation> myExecutionStack;
     95    private Stack<ExecutionContext> executionStack;
    8096    /// <summary>
    8197    /// Gets the current execution stack.
    8298    /// </summary>
    83     public Stack<IOperation> ExecutionStack {
    84       get { return myExecutionStack; }
     99    protected Stack<ExecutionContext> ExecutionStack {
     100      get { return executionStack; }
    85101    }
    86102
     
    88104    /// Flag of the current instance whether it is currently running.
    89105    /// </summary>
    90     protected bool myRunning;
     106    private bool running;
    91107    /// <summary>
    92108    /// Gets information whether the instance is currently running.
    93109    /// </summary>
    94110    public bool Running {
    95       get { return myRunning; }
     111      get { return running; }
    96112    }
    97113
     
    99115    /// Flag of the current instance whether it is canceled.
    100116    /// </summary>
    101     protected bool myCanceled;
     117    private bool canceled;
    102118    /// <summary>
    103119    /// Gets information whether the instance is currently canceled.
    104120    /// </summary>
    105     public bool Canceled {
    106       get { return myCanceled; }
     121    protected bool Canceled {
     122      get { return canceled; }
    107123    }
    108124    /// <summary>
    109125    /// Gets information whether the instance has already terminated.
    110126    /// </summary>
    111     public virtual bool Terminated {
    112       get { return ExecutionStack.Count == 0; }
     127    public bool Finished {
     128      get { return executionStack.Count == 0; }
    113129    }
    114130
     
    118134    /// <remarks>Calls <see cref="Reset"/>.</remarks>
    119135    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();
    124139    }
    125140
     
    131146    /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>
    132147    /// <returns>The cloned object as <see cref="EngineBase"/>.</returns>
    133     public override IItem Clone(ICloner cloner) {
     148    public override IDeepCloneable Clone(Cloner cloner) {
    134149      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;
    144158      return clone;
    145159    }
    146160
    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     }
    174161    /// <inheritdoc/>
    175162    /// <remarks>Sets <c>myCanceled</c> and <c>myRunning</c> to <c>false</c>. The global scope is cleared,
     
    177164    /// with the initial operator is added. <br/>
    178165    /// Calls <see cref="OnInitialized"/>.</remarks>
    179     public virtual void Reset() {
    180       myCanceled = false;
    181       myRunning = false;
     166    public void Initialize() {
     167      canceled = false;
     168      running = false;
    182169      GlobalScope.Clear();
    183170      ExecutionTime = new TimeSpan();
    184       myExecutionStack.Clear();
     171      executionStack.Clear();
    185172      if (OperatorGraph.InitialOperator != null)
    186         myExecutionStack.Push(new AtomicOperation(OperatorGraph.InitialOperator, GlobalScope));
     173        executionStack.Push(new ExecutionContext(null, OperatorGraph.InitialOperator, GlobalScope));
    187174      OnInitialized();
    188175    }
     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    }
    189197
    190198    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();
    197200      DateTime start = DateTime.Now;
    198201      DateTime end;
    199       while ((!Canceled) && (!Terminated)) {
    200         ProcessNextOperation();
     202      while ((!Canceled) && (!Finished)) {
     203        ProcessNextOperator();
    201204        end = DateTime.Now;
    202205        ExecutionTime += end - start;
     
    204207      }
    205208      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();
    208214      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();
    218217      ExecutionTime += DateTime.Now - start;
     218      running = false;
     219      OnStopped();
    219220    }
    220221
     
    222223    /// Performs the next operation.
    223224    /// </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.
    228249    /// </summary>
    229250    public event EventHandler Initialized;
     
    236257    }
    237258    /// <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());
    248279    }
    249280    /// <summary>
     
    256287    /// <param name="exception">The exception that was thrown.</param>
    257288    protected virtual void OnExceptionOccurred(Exception exception) {
    258       Abort();
    259289      if (ExceptionOccurred != null)
    260290        ExceptionOccurred(this, new EventArgs<Exception>(exception));
    261291    }
    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     }
    284292  }
    285293}
Note: See TracChangeset for help on using the changeset viewer.