Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/03/11 00:46:55 (13 years ago)
Author:
swagner
Message:

Merged ParallelEngine branch back into trunk (#1333)

Location:
trunk/sources
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources

  • trunk/sources/HeuristicLab.SequentialEngine/3.3/SequentialEngine.cs

    r4722 r5193  
    2121
    2222using System;
     23using System.Threading;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
     
    2728namespace HeuristicLab.SequentialEngine {
    2829  /// <summary>
    29   /// Represents an engine that executes its steps sequentially, also if they could be executed
    30   /// in parallel.
     30  /// Engine for sequential execution of algorithms.
    3131  /// </summary>
    3232  [StorableClass]
    3333  [Item("Sequential Engine", "Engine for sequential execution of algorithms.")]
    3434  public class SequentialEngine : Engine {
    35     private IOperator currentOperator;
    36 
    3735    [StorableConstructor]
    3836    protected SequentialEngine(bool deserializing) : base(deserializing) { }
     
    4442    }
    4543
    46     /// <summary>
    47     /// Deals with the next operation, if it is an <see cref="AtomicOperation"/> it is executed,
    48     /// if it is a <see cref="CompositeOperation"/> its single operations are pushed on the execution stack.
    49     /// </summary>
    50     /// <remarks>If an error occurs during the execution the operation is aborted and the operation
    51     /// is pushed on the stack again.<br/>
    52     /// If the execution was successful <see cref="EngineBase.OnOperationExecuted"/> is called.</remarks>
    53     protected override void ProcessNextOperation() {
    54       currentOperator = null;
    55       IOperation next = ExecutionStack.Pop();
    56       OperationCollection coll = next as OperationCollection;
    57       while (coll != null) {
    58         for (int i = coll.Count - 1; i >= 0; i--)
    59           ExecutionStack.Push(coll[i]);
    60         next = ExecutionStack.Count > 0 ? ExecutionStack.Pop() : null;
    61         coll = next as OperationCollection;
    62       }
    63       IAtomicOperation operation = next as IAtomicOperation;
    64       if (operation != null) {
    65         try {
    66           currentOperator = operation.Operator;
    67           ExecutionStack.Push(operation.Operator.Execute((IExecutionContext)operation));
    68         }
    69         catch (Exception ex) {
    70           ExecutionStack.Push(operation);
    71           OnExceptionOccurred(new OperatorExecutionException(operation.Operator, ex));
    72           Pause();
    73         }
    74         if (operation.Operator.Breakpoint) {
    75           Log.LogMessage(string.Format("Breakpoint: {0}", operation.Operator.Name != string.Empty ? operation.Operator.Name : operation.Operator.ItemName));
    76           Pause();
     44    protected override void Run(CancellationToken cancellationToken) {
     45      IOperation next;
     46      OperationCollection coll;
     47      IAtomicOperation operation;
     48
     49      while (ExecutionStack.Count > 0) {
     50        cancellationToken.ThrowIfCancellationRequested();
     51
     52        next = ExecutionStack.Pop();
     53        if (next is OperationCollection) {
     54          coll = (OperationCollection)next;
     55          for (int i = coll.Count - 1; i >= 0; i--)
     56            if (coll[i] != null) ExecutionStack.Push(coll[i]);
     57        } else if (next is IAtomicOperation) {
     58          operation = (IAtomicOperation)next;
     59          try {
     60            next = operation.Operator.Execute((IExecutionContext)operation, cancellationToken);
     61          }
     62          catch (Exception ex) {
     63            ExecutionStack.Push(operation);
     64            if (ex is OperationCanceledException) throw ex;
     65            else throw new OperatorExecutionException(operation.Operator, ex);
     66          }
     67          if (next != null) ExecutionStack.Push(next);
     68
     69          if (operation.Operator.Breakpoint) {
     70            Log.LogMessage(string.Format("Breakpoint: {0}", operation.Operator.Name != string.Empty ? operation.Operator.Name : operation.Operator.ItemName));
     71            Pause();
     72          }
    7773        }
    7874      }
    7975    }
    80 
    81     public override void Pause() {
    82       base.Pause();
    83       if (currentOperator != null) currentOperator.Abort();
    84     }
    85     public override void Stop() {
    86       base.Stop();
    87       if (currentOperator != null) currentOperator.Abort();
    88     }
    8976  }
    9077}
Note: See TracChangeset for help on using the changeset viewer.