Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SequentialEngine/3.2/SequentialEngine.cs @ 1847

Last change on this file since 1847 was 1847, checked in by gkronber, 15 years ago

Fixed #633 for the SequentialEngine by:

  • introducing a boolean property into IOperator that indicates if an operator supports abortion.
  • pushing the current operation back onto the execution stack of the SequentialEngine when the current operator can be aborted.
File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
24using System.Text;
25using HeuristicLab.Core;
26using System.Threading;
27
28namespace HeuristicLab.SequentialEngine {
29  /// <summary>
30  /// Represents an engine that executes its steps sequentially, also if they could be executed
31  /// in parallel.
32  /// </summary>
33  public class SequentialEngine : EngineBase, IEditable {
34    private IOperator currentOperator;
35    private IOperation currentOperation;
36
37    /// <summary>
38    /// Creates a new instance of <see cref="SequentialEngineEditor"/>.
39    /// </summary>
40    /// <returns>The created view as <see cref="SequentialEngineEditor"/>.</returns>
41    public override IView CreateView() {
42      return new SequentialEngineEditor(this);
43    }
44
45    /// <summary>
46    /// Creates a new instance of <see cref="SequentialEngineEditor"/>.
47    /// </summary>
48    /// <returns>The created editor as <see cref="SequentialEngineEditor"/>.</returns>
49    public virtual IEditor CreateEditor() {
50      return new SequentialEngineEditor(this);
51    }
52
53    /// <summary>
54    /// Aborts the engine execution.
55    /// </summary>
56    /// <remarks>Calls <see cref="EngineBase.Abort"/> of base class <see cref="EngineBase"/> and
57    /// <see cref="IOperator.Abort"/> of the current <see cref="IOperator"/>.</remarks>
58    public override void Abort() {
59      base.Abort();
60      if (currentOperator != null && currentOperator.SupportsAbort) {
61        currentOperator.Abort();
62        myExecutionStack.Push(currentOperation);
63      }
64    }
65
66    /// <summary>
67    /// Deals with the next operation, if it is an <see cref="AtomicOperation"/> it is executed,
68    /// if it is a <see cref="CompositeOperation"/> its single operations are pushed on the execution stack.
69    /// </summary>
70    /// <remarks>If an error occurs during the execution the operation is aborted and the operation
71    /// is pushed on the stack again.<br/>
72    /// If the execution was successful <see cref="EngineBase.OnOperationExecuted"/> is called.</remarks>
73    protected override void ProcessNextOperation() {
74      currentOperation = myExecutionStack.Pop();
75      if (currentOperation is AtomicOperation) {
76        AtomicOperation atomicOperation = (AtomicOperation)currentOperation;
77        IOperation next = null;
78        try {
79          currentOperator = atomicOperation.Operator;
80          next = atomicOperation.Operator.Execute(atomicOperation.Scope);
81        }
82        catch (Exception ex) {
83          // push operation on stack again
84          myExecutionStack.Push(atomicOperation);
85          Abort();
86          ThreadPool.QueueUserWorkItem(delegate(object state) { OnExceptionOccurred(ex);});
87        }
88        if (next != null)
89          myExecutionStack.Push(next);
90        OnOperationExecuted(atomicOperation);
91        if (atomicOperation.Operator.Breakpoint) Abort();
92      } else if (currentOperation is CompositeOperation) {
93        CompositeOperation compositeOperation = (CompositeOperation)currentOperation;
94        for (int i = compositeOperation.Operations.Count - 1; i >= 0; i--)
95          myExecutionStack.Push(compositeOperation.Operations[i]);
96      }
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.