Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2834 was 2834, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • worked on operators, engines, and optimization
File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Core;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25
26namespace HeuristicLab.SequentialEngine {
27  /// <summary>
28  /// Represents an engine that executes its steps sequentially, also if they could be executed
29  /// in parallel.
30  /// </summary>
31  [EmptyStorableClass]
32  [Creatable("Engines")]
33  [Item("Sequential Engine", "Engine for sequential execution of algorithms.")]
34  public class SequentialEngine : Engine {
35    private IOperator currentOperator;
36
37    /// <summary>
38    /// Aborts the engine execution.
39    /// </summary>
40    /// <remarks>Calls <see cref="EngineBase.Abort"/> of base class <see cref="EngineBase"/> and
41    /// <see cref="IOperator.Abort"/> of the current <see cref="IOperator"/>.</remarks>
42    public override void Stop() {
43      base.Stop();
44      if (currentOperator != null)
45        currentOperator.Abort();
46    }
47
48    /// <summary>
49    /// Deals with the next operation, if it is an <see cref="AtomicOperation"/> it is executed,
50    /// if it is a <see cref="CompositeOperation"/> its single operations are pushed on the execution stack.
51    /// </summary>
52    /// <remarks>If an error occurs during the execution the operation is aborted and the operation
53    /// is pushed on the stack again.<br/>
54    /// If the execution was successful <see cref="EngineBase.OnOperationExecuted"/> is called.</remarks>
55    protected override void ProcessNextOperator() {
56      currentOperator = null;
57      IOperation next = ExecutionStack.Pop();
58      OperationCollection coll = next as OperationCollection;
59      while (coll != null) {
60        for (int i = coll.Count - 1; i >= 0; i--)
61          ExecutionStack.Push(coll[i]);
62        next = ExecutionStack.Count > 0 ? ExecutionStack.Pop() : null;
63        coll = next as OperationCollection;
64      }
65      IAtomicOperation operation = next as IAtomicOperation;
66      if (operation != null) {
67        try {
68          currentOperator = operation.Operator;
69          ExecutionStack.Push(operation.Operator.Execute((IExecutionContext)operation));
70          currentOperator = null;
71        }
72        catch (Exception ex) {
73          ExecutionStack.Push(operation);
74          Stop();
75          OnExceptionOccurred(ex);
76        }
77        if (operation.Operator.Breakpoint)
78          Stop();
79      }
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.