[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;
|
---|
[3376] | 23 | using HeuristicLab.Common;
|
---|
[2] | 24 | using HeuristicLab.Core;
|
---|
[1853] | 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 26 |
|
---|
| 27 | namespace 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 |
|
---|
[851] | 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>
|
---|
[3288] | 44 | protected override void ProcessNextOperation() {
|
---|
[2656] | 45 | currentOperator = null;
|
---|
[2834] | 46 | IOperation next = ExecutionStack.Pop();
|
---|
| 47 | OperationCollection coll = next as OperationCollection;
|
---|
[2757] | 48 | while (coll != null) {
|
---|
| 49 | for (int i = coll.Count - 1; i >= 0; i--)
|
---|
| 50 | ExecutionStack.Push(coll[i]);
|
---|
[2818] | 51 | next = ExecutionStack.Count > 0 ? ExecutionStack.Pop() : null;
|
---|
[2834] | 52 | coll = next as OperationCollection;
|
---|
[2757] | 53 | }
|
---|
[2834] | 54 | IAtomicOperation operation = next as IAtomicOperation;
|
---|
| 55 | if (operation != null) {
|
---|
[2818] | 56 | try {
|
---|
[2834] | 57 | currentOperator = operation.Operator;
|
---|
| 58 | ExecutionStack.Push(operation.Operator.Execute((IExecutionContext)operation));
|
---|
[2818] | 59 | }
|
---|
| 60 | catch (Exception ex) {
|
---|
[2834] | 61 | ExecutionStack.Push(operation);
|
---|
[3226] | 62 | OnExceptionOccurred(ex);
|
---|
[3262] | 63 | Pause();
|
---|
[2818] | 64 | }
|
---|
[3289] | 65 | if (operation.Operator.Breakpoint) {
|
---|
[3290] | 66 | Log.LogMessage(string.Format("Breakpoint: {0}", operation.Operator.Name != string.Empty ? operation.Operator.Name : operation.Operator.ItemName));
|
---|
[3262] | 67 | Pause();
|
---|
[3289] | 68 | }
|
---|
[2] | 69 | }
|
---|
| 70 | }
|
---|
[2851] | 71 |
|
---|
[3262] | 72 | public override void Pause() {
|
---|
| 73 | base.Pause();
|
---|
| 74 | if (currentOperator != null) currentOperator.Abort();
|
---|
[2851] | 75 | }
|
---|
[3262] | 76 | public override void Stop() {
|
---|
| 77 | base.Stop();
|
---|
| 78 | if (currentOperator != null) currentOperator.Abort();
|
---|
| 79 | }
|
---|
[2] | 80 | }
|
---|
| 81 | }
|
---|