Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • implemented reviewers' comments
  • added additional plugins HeuristicLab.Evolutionary, HeuristicLab.Permutation, HeuristicLab.Selection, and HeuristicLab.Routing.TSP
File size: 3.1 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 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      IExecutionSequence next = ExecutionStack.Pop();
60      ExecutionContextCollection coll = next as ExecutionContextCollection;
61      while (coll != null) {
62        for (int i = coll.Count - 1; i >= 0; i--)
63          ExecutionStack.Push(coll[i]);
64        next = ExecutionStack.Pop();
65        coll = next as ExecutionContextCollection;
66      }
67      ExecutionContext context = next as ExecutionContext;
68      try {
69        currentOperator = context.Operator;
70        ExecutionStack.Push(context.Operator.Execute(context));
71        currentOperator = null;
72      }
73      catch (Exception ex) {
74        ExecutionStack.Push(context);
75        Stop();
76        OnExceptionOccurred(ex);
77      }
78      if (context.Operator.Breakpoint)
79        Stop();
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.