Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.SequentialEngine/3.3/SequentialEngine.cs @ 5264

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

Merged cloning refactoring branch back into trunk (#922)

File size: 3.6 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    [StorableConstructor]
38    protected SequentialEngine(bool deserializing) : base(deserializing) { }
39    protected SequentialEngine(SequentialEngine original, Cloner cloner) : base(original, cloner) { }
40    public SequentialEngine() : base() { }
41
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new SequentialEngine(this, cloner);
44    }
45
46    /// <summary>
47    /// Deals with the next operation, if it is an <see cref="AtomicOperation"/> it is executed,
48    /// if it is a <see cref="CompositeOperation"/> its single operations are pushed on the execution stack.
49    /// </summary>
50    /// <remarks>If an error occurs during the execution the operation is aborted and the operation
51    /// is pushed on the stack again.<br/>
52    /// If the execution was successful <see cref="EngineBase.OnOperationExecuted"/> is called.</remarks>
53    protected override void ProcessNextOperation() {
54      currentOperator = null;
55      IOperation next = ExecutionStack.Pop();
56      OperationCollection coll = next as OperationCollection;
57      while (coll != null) {
58        for (int i = coll.Count - 1; i >= 0; i--)
59          ExecutionStack.Push(coll[i]);
60        next = ExecutionStack.Count > 0 ? ExecutionStack.Pop() : null;
61        coll = next as OperationCollection;
62      }
63      IAtomicOperation operation = next as IAtomicOperation;
64      if (operation != null) {
65        try {
66          currentOperator = operation.Operator;
67          ExecutionStack.Push(operation.Operator.Execute((IExecutionContext)operation));
68        }
69        catch (Exception ex) {
70          ExecutionStack.Push(operation);
71          OnExceptionOccurred(new OperatorExecutionException(operation.Operator, ex));
72          Pause();
73        }
74        if (operation.Operator.Breakpoint) {
75          Log.LogMessage(string.Format("Breakpoint: {0}", operation.Operator.Name != string.Empty ? operation.Operator.Name : operation.Operator.ItemName));
76          Pause();
77        }
78      }
79    }
80
81    public override void Pause() {
82      base.Pause();
83      if (currentOperator != null) currentOperator.Abort();
84    }
85    public override void Stop() {
86      base.Stop();
87      if (currentOperator != null) currentOperator.Abort();
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.