Free cookie consent management tool by TermsFeed Policy Generator

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

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

Merged cloning refactoring branch back into trunk (#922)

File size: 3.6 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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;
[4722]23using HeuristicLab.Common;
[2]24using HeuristicLab.Core;
[1853]25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]26
27namespace HeuristicLab.SequentialEngine {
[851]28  /// <summary>
29  /// Represents an engine that executes its steps sequentially, also if they could be executed
30  /// in parallel.
31  /// </summary>
[3017]32  [StorableClass]
[2546]33  [Item("Sequential Engine", "Engine for sequential execution of algorithms.")]
[2664]34  public class SequentialEngine : Engine {
[2]35    private IOperator currentOperator;
36
[4722]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
[851]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>
[3288]53    protected override void ProcessNextOperation() {
[2656]54      currentOperator = null;
[2834]55      IOperation next = ExecutionStack.Pop();
56      OperationCollection coll = next as OperationCollection;
[2757]57      while (coll != null) {
58        for (int i = coll.Count - 1; i >= 0; i--)
59          ExecutionStack.Push(coll[i]);
[2818]60        next = ExecutionStack.Count > 0 ? ExecutionStack.Pop() : null;
[2834]61        coll = next as OperationCollection;
[2757]62      }
[2834]63      IAtomicOperation operation = next as IAtomicOperation;
64      if (operation != null) {
[2818]65        try {
[2834]66          currentOperator = operation.Operator;
67          ExecutionStack.Push(operation.Operator.Execute((IExecutionContext)operation));
[2818]68        }
69        catch (Exception ex) {
[2834]70          ExecutionStack.Push(operation);
[3758]71          OnExceptionOccurred(new OperatorExecutionException(operation.Operator, ex));
[3262]72          Pause();
[2818]73        }
[3289]74        if (operation.Operator.Breakpoint) {
[3290]75          Log.LogMessage(string.Format("Breakpoint: {0}", operation.Operator.Name != string.Empty ? operation.Operator.Name : operation.Operator.ItemName));
[3262]76          Pause();
[3289]77        }
[2]78      }
79    }
[2851]80
[3262]81    public override void Pause() {
82      base.Pause();
83      if (currentOperator != null) currentOperator.Abort();
[2851]84    }
[3262]85    public override void Stop() {
86      base.Stop();
87      if (currentOperator != null) currentOperator.Abort();
88    }
[2]89  }
90}
Note: See TracBrowser for help on using the repository browser.