Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SequentialEngine/3.3/SequentialEngine.cs @ 2716

Last change on this file since 2716 was 2664, checked in by swagner, 15 years ago

Abandoned policy that the names of all abstract base classes have to end in "Base" (#95)

File size: 2.9 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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  [EmptyStorableClass]
34  [Creatable("Engines")]
35  [Item("Sequential Engine", "Engine for sequential execution of algorithms.")]
36  public class SequentialEngine : Engine {
37    private IOperator currentOperator;
38
39    /// <summary>
40    /// Aborts the engine execution.
41    /// </summary>
42    /// <remarks>Calls <see cref="EngineBase.Abort"/> of base class <see cref="EngineBase"/> and
43    /// <see cref="IOperator.Abort"/> of the current <see cref="IOperator"/>.</remarks>
44    public override void Stop() {
45      base.Stop();
46      if (currentOperator != null)
47        currentOperator.Abort();
48    }
49
50    /// <summary>
51    /// Deals with the next operation, if it is an <see cref="AtomicOperation"/> it is executed,
52    /// if it is a <see cref="CompositeOperation"/> its single operations are pushed on the execution stack.
53    /// </summary>
54    /// <remarks>If an error occurs during the execution the operation is aborted and the operation
55    /// is pushed on the stack again.<br/>
56    /// If the execution was successful <see cref="EngineBase.OnOperationExecuted"/> is called.</remarks>
57    protected override void ProcessNextOperator() {
58      currentOperator = null;
59      ExecutionContext context = ExecutionStack.Pop();
60      ExecutionContextCollection next = null;
61      try {
62        currentOperator = context.Operator;
63        next = context.Operator.Execute(context);
64        currentOperator = null;
65      }
66      catch (Exception ex) {
67        ExecutionStack.Push(context);
68        Stop();
69        OnExceptionOccurred(ex);
70      }
71      if (next != null) {
72        for (int i = next.Count - 1; i >= 0; i--)
73          ExecutionStack.Push(next[i]);
74      }
75      if (context.Operator.Breakpoint)
76        Stop();
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.