[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using System.Threading;
|
---|
[1853] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 28 |
|
---|
| 29 | namespace HeuristicLab.SequentialEngine {
|
---|
[851] | 30 | /// <summary>
|
---|
| 31 | /// Represents an engine that executes its steps sequentially, also if they could be executed
|
---|
| 32 | /// in parallel.
|
---|
| 33 | /// </summary>
|
---|
[1853] | 34 | [EmptyStorableClass]
|
---|
[2] | 35 | public class SequentialEngine : EngineBase, IEditable {
|
---|
| 36 | private IOperator currentOperator;
|
---|
| 37 |
|
---|
[851] | 38 | /// <summary>
|
---|
| 39 | /// Creates a new instance of <see cref="SequentialEngineEditor"/>.
|
---|
| 40 | /// </summary>
|
---|
| 41 | /// <returns>The created view as <see cref="SequentialEngineEditor"/>.</returns>
|
---|
[2] | 42 | public override IView CreateView() {
|
---|
| 43 | return new SequentialEngineEditor(this);
|
---|
| 44 | }
|
---|
[851] | 45 |
|
---|
| 46 | /// <summary>
|
---|
| 47 | /// Creates a new instance of <see cref="SequentialEngineEditor"/>.
|
---|
| 48 | /// </summary>
|
---|
| 49 | /// <returns>The created editor as <see cref="SequentialEngineEditor"/>.</returns>
|
---|
[2] | 50 | public virtual IEditor CreateEditor() {
|
---|
| 51 | return new SequentialEngineEditor(this);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[851] | 54 | /// <summary>
|
---|
[1441] | 55 | /// Aborts the engine execution.
|
---|
[851] | 56 | /// </summary>
|
---|
| 57 | /// <remarks>Calls <see cref="EngineBase.Abort"/> of base class <see cref="EngineBase"/> and
|
---|
| 58 | /// <see cref="IOperator.Abort"/> of the current <see cref="IOperator"/>.</remarks>
|
---|
[2] | 59 | public override void Abort() {
|
---|
| 60 | base.Abort();
|
---|
| 61 | if (currentOperator != null)
|
---|
| 62 | currentOperator.Abort();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[851] | 65 | /// <summary>
|
---|
| 66 | /// Deals with the next operation, if it is an <see cref="AtomicOperation"/> it is executed,
|
---|
| 67 | /// if it is a <see cref="CompositeOperation"/> its single operations are pushed on the execution stack.
|
---|
| 68 | /// </summary>
|
---|
| 69 | /// <remarks>If an error occurs during the execution the operation is aborted and the operation
|
---|
| 70 | /// is pushed on the stack again.<br/>
|
---|
| 71 | /// If the execution was successful <see cref="EngineBase.OnOperationExecuted"/> is called.</remarks>
|
---|
[2] | 72 | protected override void ProcessNextOperation() {
|
---|
| 73 | IOperation operation = myExecutionStack.Pop();
|
---|
| 74 | if (operation is AtomicOperation) {
|
---|
| 75 | AtomicOperation atomicOperation = (AtomicOperation)operation;
|
---|
| 76 | IOperation next = null;
|
---|
| 77 | try {
|
---|
| 78 | currentOperator = atomicOperation.Operator;
|
---|
| 79 | next = atomicOperation.Operator.Execute(atomicOperation.Scope);
|
---|
| 80 | }
|
---|
| 81 | catch (Exception ex) {
|
---|
| 82 | // push operation on stack again
|
---|
| 83 | myExecutionStack.Push(atomicOperation);
|
---|
| 84 | Abort();
|
---|
| 85 | ThreadPool.QueueUserWorkItem(delegate(object state) { OnExceptionOccurred(ex);});
|
---|
| 86 | }
|
---|
| 87 | if (next != null)
|
---|
| 88 | myExecutionStack.Push(next);
|
---|
| 89 | OnOperationExecuted(atomicOperation);
|
---|
| 90 | if (atomicOperation.Operator.Breakpoint) Abort();
|
---|
| 91 | } else if (operation is CompositeOperation) {
|
---|
| 92 | CompositeOperation compositeOperation = (CompositeOperation)operation;
|
---|
| 93 | for (int i = compositeOperation.Operations.Count - 1; i >= 0; i--)
|
---|
| 94 | myExecutionStack.Push(compositeOperation.Operations[i]);
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | }
|
---|