Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3712 was 3376, checked in by swagner, 15 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

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