Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3262 was 1851, checked in by gkronber, 16 years ago

Reverted r1847 after discussion with swagner. Instead each operator should decide independently what happens after abort and adapt the returned next operation accordingly. #633 (Engines do not handle abortion of operators correctly)

File size: 3.8 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
36    /// <summary>
37    /// Creates a new instance of <see cref="SequentialEngineEditor"/>.
38    /// </summary>
39    /// <returns>The created view as <see cref="SequentialEngineEditor"/>.</returns>
40    public override IView CreateView() {
41      return new SequentialEngineEditor(this);
42    }
43
44    /// <summary>
45    /// Creates a new instance of <see cref="SequentialEngineEditor"/>.
46    /// </summary>
47    /// <returns>The created editor as <see cref="SequentialEngineEditor"/>.</returns>
48    public virtual IEditor CreateEditor() {
49      return new SequentialEngineEditor(this);
50    }
51
52    /// <summary>
53    /// Aborts the engine execution.
54    /// </summary>
55    /// <remarks>Calls <see cref="EngineBase.Abort"/> of base class <see cref="EngineBase"/> and
56    /// <see cref="IOperator.Abort"/> of the current <see cref="IOperator"/>.</remarks>
57    public override void Abort() {
58      base.Abort();
59      if (currentOperator != null)
60        currentOperator.Abort();
61    }
62
63    /// <summary>
64    /// Deals with the next operation, if it is an <see cref="AtomicOperation"/> it is executed,
65    /// if it is a <see cref="CompositeOperation"/> its single operations are pushed on the execution stack.
66    /// </summary>
67    /// <remarks>If an error occurs during the execution the operation is aborted and the operation
68    /// is pushed on the stack again.<br/>
69    /// If the execution was successful <see cref="EngineBase.OnOperationExecuted"/> is called.</remarks>
70    protected override void ProcessNextOperation() {
71      IOperation operation = myExecutionStack.Pop();
72      if (operation is AtomicOperation) {
73        AtomicOperation atomicOperation = (AtomicOperation)operation;
74        IOperation next = null;
75        try {
76          currentOperator = atomicOperation.Operator;
77          next = atomicOperation.Operator.Execute(atomicOperation.Scope);
78        }
79        catch (Exception ex) {
80          // push operation on stack again
81          myExecutionStack.Push(atomicOperation);
82          Abort();
83          ThreadPool.QueueUserWorkItem(delegate(object state) { OnExceptionOccurred(ex);});
84        }
85        if (next != null)
86          myExecutionStack.Push(next);
87        OnOperationExecuted(atomicOperation);
88        if (atomicOperation.Operator.Breakpoint) Abort();
89      } else if (operation is CompositeOperation) {
90        CompositeOperation compositeOperation = (CompositeOperation)operation;
91        for (int i = compositeOperation.Operations.Count - 1; i >= 0; i--)
92          myExecutionStack.Push(compositeOperation.Operations[i]);
93      }
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.