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;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using System.Threading;
|
---|
28 | using System.Diagnostics;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.FixedOperators {
|
---|
31 | class FixedOperatorBase : CombinedOperator {
|
---|
32 | /// <summary>
|
---|
33 | /// Execution pointer shows which command actually is executed
|
---|
34 | /// </summary>
|
---|
35 | protected int executionPointer;
|
---|
36 |
|
---|
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 |
|
---|
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) {
|
---|
64 | if (!IsExecuted()) {
|
---|
65 | ExecuteOperation(op, scope);
|
---|
66 | persistedExecutionPointer.Data++;
|
---|
67 | //Console.WriteLine("Execute: {0}", executionPointer);
|
---|
68 | } // if not executed
|
---|
69 | //else
|
---|
70 | //Console.WriteLine("Skip Execute: {0}", executionPointer);
|
---|
71 | executionPointer++;
|
---|
72 |
|
---|
73 | if (Canceled)
|
---|
74 | throw new CancelException();
|
---|
75 | } // Execute
|
---|
76 |
|
---|
77 | protected void ExecuteOperation(IOperator op, IScope scope) {
|
---|
78 | IOperation operation;
|
---|
79 | currentOperator = op;
|
---|
80 | operation = op.Execute(scope);
|
---|
81 | if (operation != null) {
|
---|
82 | //IOperator currentOperator;
|
---|
83 | Stack<IOperation> executionStack = new Stack<IOperation>();
|
---|
84 | executionStack.Push(op.Execute(scope));
|
---|
85 |
|
---|
86 | while (executionStack.Count > 0) {
|
---|
87 | operation = executionStack.Pop();
|
---|
88 | if (operation is AtomicOperation) {
|
---|
89 | AtomicOperation atomicOperation = (AtomicOperation)operation;
|
---|
90 | IOperation next = null;
|
---|
91 | try {
|
---|
92 | currentOperator = atomicOperation.Operator;
|
---|
93 | next = currentOperator.Execute(atomicOperation.Scope);
|
---|
94 | }
|
---|
95 | catch (Exception) {
|
---|
96 | throw new InvalidOperationException("Invalid Operation occured in FixedBase.Execute");
|
---|
97 | }
|
---|
98 | if (next != null)
|
---|
99 | executionStack.Push(next);
|
---|
100 | } else if (operation is CompositeOperation) {
|
---|
101 | CompositeOperation compositeOperation = (CompositeOperation)operation;
|
---|
102 | for (int i = compositeOperation.Operations.Count - 1; i >= 0; i--)
|
---|
103 | executionStack.Push(compositeOperation.Operations[i]);
|
---|
104 | } // else if
|
---|
105 | } // while
|
---|
106 | } // if (operation != null)
|
---|
107 | } // ExecuteOperation
|
---|
108 |
|
---|
109 | public override IOperation Apply(IScope scope) {
|
---|
110 | try {
|
---|
111 | persistedExecutionPointer = scope.GetVariableValue<IntData>("ExecutionPointer", false);
|
---|
112 | }
|
---|
113 | catch (Exception) {
|
---|
114 | persistedExecutionPointer = new IntData(0);
|
---|
115 | scope.AddVariable(new Variable("ExecutionPointer", persistedExecutionPointer));
|
---|
116 | }
|
---|
117 |
|
---|
118 | executionPointer = 0;
|
---|
119 | return null;
|
---|
120 | } // Apply
|
---|
121 |
|
---|
122 | public override void Abort() {
|
---|
123 | base.Abort();
|
---|
124 | currentOperator.Abort();
|
---|
125 | //engineThread.Abort();
|
---|
126 | }
|
---|
127 |
|
---|
128 | } // class FixedBase
|
---|
129 |
|
---|
130 | class CancelException : Exception {
|
---|
131 |
|
---|
132 | } // class CancelException
|
---|
133 | } // namespace HeuristicLab.FixedOperators
|
---|