[1693] | 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 HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Operators;
|
---|
[1789] | 26 | using HeuristicLab.Data;
|
---|
| 27 | using System.Threading;
|
---|
| 28 | using System.Diagnostics;
|
---|
[1693] | 29 |
|
---|
| 30 | namespace HeuristicLab.FixedOperators {
|
---|
[1789] | 31 | class FixedOperatorBase : CombinedOperator {
|
---|
| 32 | /// <summary>
|
---|
| 33 | /// Execution pointer shows which command actually is executed
|
---|
| 34 | /// </summary>
|
---|
| 35 | protected int executionPointer;
|
---|
[1693] | 36 |
|
---|
[1789] | 37 | /// <summary>
|
---|
| 38 | /// Execution pointer if execution was aborted previously
|
---|
| 39 | /// </summary>
|
---|
| 40 | protected IntData persistedExecutionPointer;
|
---|
| 41 |
|
---|
| 42 | /// <summary>
|
---|
| 43 | /// Current operator in execution.
|
---|
| 44 | /// </summary>
|
---|
| 45 | protected IOperator currentOperator;
|
---|
| 46 |
|
---|
| 47 | public FixedOperatorBase() : base() {
|
---|
| 48 | //AddVariableInfo(new VariableInfo("ExecutionPointer", "Execution pointer for algorithm abortion", typeof(IntData), VariableKind.New));
|
---|
| 49 | } // FixedOperatorBase
|
---|
| 50 |
|
---|
| 51 | private bool IsExecuted() {
|
---|
| 52 | return persistedExecutionPointer.Data > executionPointer;
|
---|
| 53 | } // AlreadyExecuted
|
---|
| 54 |
|
---|
[1875] | 55 | protected void ExecuteExitable(IOperator op, IScope scope) {
|
---|
| 56 |
|
---|
| 57 | } // ExecuteExitable
|
---|
| 58 |
|
---|
| 59 | protected void SetRegion(string region) {
|
---|
| 60 |
|
---|
| 61 | } // SetRegion
|
---|
| 62 |
|
---|
| 63 | protected virtual void Execute(IOperator op, IScope scope) {
|
---|
[1789] | 64 | if (!IsExecuted()) {
|
---|
[1875] | 65 | ExecuteOperation(op, scope);
|
---|
[1789] | 66 | persistedExecutionPointer.Data++;
|
---|
| 67 | } // if not executed
|
---|
| 68 | executionPointer++;
|
---|
| 69 |
|
---|
| 70 | if (Canceled)
|
---|
| 71 | throw new CancelException();
|
---|
[1693] | 72 | } // Execute
|
---|
| 73 |
|
---|
[1789] | 74 | protected void ExecuteOperation(IOperator op, IScope scope) {
|
---|
| 75 | IOperation operation;
|
---|
| 76 | currentOperator = op;
|
---|
| 77 | operation = op.Execute(scope);
|
---|
| 78 | if (operation != null) {
|
---|
| 79 | //IOperator currentOperator;
|
---|
| 80 | Stack<IOperation> executionStack = new Stack<IOperation>();
|
---|
| 81 | executionStack.Push(op.Execute(scope));
|
---|
| 82 |
|
---|
| 83 | while (executionStack.Count > 0) {
|
---|
| 84 | operation = executionStack.Pop();
|
---|
| 85 | if (operation is AtomicOperation) {
|
---|
| 86 | AtomicOperation atomicOperation = (AtomicOperation)operation;
|
---|
| 87 | IOperation next = null;
|
---|
| 88 | try {
|
---|
| 89 | currentOperator = atomicOperation.Operator;
|
---|
| 90 | next = currentOperator.Execute(atomicOperation.Scope);
|
---|
| 91 | }
|
---|
| 92 | catch (Exception) {
|
---|
| 93 | throw new InvalidOperationException("Invalid Operation occured in FixedBase.Execute");
|
---|
| 94 | }
|
---|
| 95 | if (next != null)
|
---|
| 96 | executionStack.Push(next);
|
---|
| 97 | } else if (operation is CompositeOperation) {
|
---|
| 98 | CompositeOperation compositeOperation = (CompositeOperation)operation;
|
---|
| 99 | for (int i = compositeOperation.Operations.Count - 1; i >= 0; i--)
|
---|
| 100 | executionStack.Push(compositeOperation.Operations[i]);
|
---|
| 101 | } // else if
|
---|
| 102 | } // while
|
---|
| 103 | } // if (operation != null)
|
---|
| 104 | } // ExecuteOperation
|
---|
| 105 |
|
---|
| 106 | public override IOperation Apply(IScope scope) {
|
---|
| 107 | try {
|
---|
| 108 | persistedExecutionPointer = scope.GetVariableValue<IntData>("ExecutionPointer", false);
|
---|
| 109 | }
|
---|
| 110 | catch (Exception) {
|
---|
| 111 | persistedExecutionPointer = new IntData(0);
|
---|
| 112 | scope.AddVariable(new Variable("ExecutionPointer", persistedExecutionPointer));
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | executionPointer = 0;
|
---|
| 116 | return null;
|
---|
| 117 | } // Apply
|
---|
| 118 |
|
---|
| 119 | public override void Abort() {
|
---|
| 120 | base.Abort();
|
---|
| 121 | currentOperator.Abort();
|
---|
| 122 | //engineThread.Abort();
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[1693] | 125 | } // class FixedBase
|
---|
[1789] | 126 |
|
---|
| 127 | class CancelException : Exception {
|
---|
| 128 |
|
---|
| 129 | } // class CancelException
|
---|
[1693] | 130 | } // namespace HeuristicLab.FixedOperators
|
---|