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