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 | using System.Text;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.FixedOperators {
|
---|
32 | class FixedOperatorBase : CombinedOperator {
|
---|
33 | protected ItemList<IOperation> persistedOperations;
|
---|
34 | protected Stack<IOperation> executionStack;
|
---|
35 |
|
---|
36 | /// <summary>
|
---|
37 | /// Execution pointer shows which command actually is executed
|
---|
38 | /// </summary>
|
---|
39 | protected int executionPointer;
|
---|
40 |
|
---|
41 | /// <summary>
|
---|
42 | /// Execution pointer if execution was aborted previously
|
---|
43 | /// </summary>
|
---|
44 | protected IntData persistedExecutionPointer;
|
---|
45 |
|
---|
46 | protected int tempExePointer;
|
---|
47 | protected int tempPersExePointer;
|
---|
48 |
|
---|
49 | /// <summary>
|
---|
50 | /// Current operator in execution.
|
---|
51 | /// </summary>
|
---|
52 | protected IOperator currentOperator;
|
---|
53 |
|
---|
54 | public FixedOperatorBase()
|
---|
55 | : base() {
|
---|
56 | executionStack = new Stack<IOperation>();
|
---|
57 | } // FixedOperatorBase
|
---|
58 |
|
---|
59 | private bool IsExecuted() {
|
---|
60 | return persistedExecutionPointer.Data > executionPointer;
|
---|
61 | } // AlreadyExecuted
|
---|
62 |
|
---|
63 | protected void ExecuteExitable(IOperator op, IScope scope) {
|
---|
64 |
|
---|
65 | } // ExecuteExitable
|
---|
66 |
|
---|
67 | protected void SetRegion(string region) {
|
---|
68 |
|
---|
69 | } // SetRegion
|
---|
70 |
|
---|
71 | protected virtual void Execute(IOperator op, IScope scope) {
|
---|
72 | if (!IsExecuted()) {
|
---|
73 | ExecuteOperation(op, scope);
|
---|
74 | persistedExecutionPointer.Data++;
|
---|
75 | } // if not executed
|
---|
76 | executionPointer++;
|
---|
77 |
|
---|
78 | if (Canceled)
|
---|
79 | throw new CancelException();
|
---|
80 | } // Execute
|
---|
81 |
|
---|
82 | protected void ExecuteOperation(IOperator op, IScope scope) {
|
---|
83 | IOperation operation;
|
---|
84 | if (persistedOperations.Count == 0) {
|
---|
85 | currentOperator = op;
|
---|
86 | operation = op.Execute(scope);
|
---|
87 | if (operation != null) {
|
---|
88 | executionStack.Push(operation);
|
---|
89 | }
|
---|
90 | } else {
|
---|
91 | executionStack = new Stack<IOperation>(persistedOperations);
|
---|
92 | }
|
---|
93 |
|
---|
94 | while (executionStack.Count > 0) {
|
---|
95 | operation = executionStack.Pop();
|
---|
96 | if (operation is AtomicOperation) {
|
---|
97 | AtomicOperation atomicOperation = (AtomicOperation)operation;
|
---|
98 | IOperation next = null;
|
---|
99 | try {
|
---|
100 | currentOperator = atomicOperation.Operator;
|
---|
101 | next = currentOperator.Execute(atomicOperation.Scope);
|
---|
102 | }
|
---|
103 | catch (Exception) {
|
---|
104 | throw new InvalidOperationException("Invalid Operation occured in FixedBase.Execute");
|
---|
105 | }
|
---|
106 | if (next != null)
|
---|
107 | executionStack.Push(next);
|
---|
108 | } else if (operation is CompositeOperation) {
|
---|
109 | CompositeOperation compositeOperation = (CompositeOperation)operation;
|
---|
110 | for (int i = compositeOperation.Operations.Count - 1; i >= 0; i--)
|
---|
111 | executionStack.Push(compositeOperation.Operations[i]);
|
---|
112 | } // else if
|
---|
113 |
|
---|
114 | if (Canceled && executionStack.Count > 0) {
|
---|
115 | SaveExecutionStack(executionStack);
|
---|
116 | throw new CancelException();
|
---|
117 | }
|
---|
118 | } // while
|
---|
119 | } // ExecuteOperation
|
---|
120 |
|
---|
121 | private void SaveExecutionStack(Stack<IOperation> stack) {
|
---|
122 | persistedOperations = new ItemList<IOperation>();
|
---|
123 | persistedOperations.AddRange(stack.ToArray());
|
---|
124 | } // SaveExecutionStack
|
---|
125 |
|
---|
126 | public override IOperation Apply(IScope scope) {
|
---|
127 | base.Apply(scope);
|
---|
128 | try {
|
---|
129 | persistedExecutionPointer = scope.GetVariableValue<IntData>("ExecutionPointer", false);
|
---|
130 | }
|
---|
131 | catch (Exception) {
|
---|
132 | persistedExecutionPointer = new IntData(0);
|
---|
133 | scope.AddVariable(new Variable("ExecutionPointer", persistedExecutionPointer));
|
---|
134 | }
|
---|
135 |
|
---|
136 | try {
|
---|
137 | persistedOperations = scope.GetVariableValue<ItemList<IOperation>>("ExecutionStack", false);
|
---|
138 | }
|
---|
139 | catch (Exception) {
|
---|
140 | persistedOperations = new ItemList<IOperation>();
|
---|
141 | scope.AddVariable(new Variable("ExecutionStack", persistedOperations));
|
---|
142 | }
|
---|
143 |
|
---|
144 | executionPointer = 0;
|
---|
145 |
|
---|
146 | for (int i = 0; i < SubOperators.Count; i++) {
|
---|
147 | if (scope.GetVariable(SubOperators[i].Name) != null)
|
---|
148 | scope.RemoveVariable(SubOperators[i].Name);
|
---|
149 | scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i]));
|
---|
150 | }
|
---|
151 |
|
---|
152 | return null;
|
---|
153 | } // Apply
|
---|
154 |
|
---|
155 | public override void Abort() {
|
---|
156 | base.Abort();
|
---|
157 | currentOperator.Abort();
|
---|
158 | } // Abort
|
---|
159 |
|
---|
160 | /// <summary>
|
---|
161 | /// Saves the value of the execution pointers into temp variables
|
---|
162 | /// </summary>
|
---|
163 | protected void SaveExecutionPointer() {
|
---|
164 | tempExePointer = executionPointer;
|
---|
165 | tempPersExePointer = persistedExecutionPointer.Data;
|
---|
166 | } // SaveExecutionPointer
|
---|
167 |
|
---|
168 | protected void SetExecutionPointerToLastSaved() {
|
---|
169 | if (executionPointer != persistedExecutionPointer.Data)
|
---|
170 | persistedExecutionPointer.Data = tempPersExePointer;
|
---|
171 | else
|
---|
172 | persistedExecutionPointer.Data = tempExePointer;
|
---|
173 | executionPointer = tempExePointer;
|
---|
174 | } // SetExecutionPointerToLastSaved
|
---|
175 |
|
---|
176 |
|
---|
177 | protected void ResetExecutionPointer() {
|
---|
178 | executionPointer = 0;
|
---|
179 | persistedExecutionPointer.Data = 0;
|
---|
180 | } // ResetExecutionPointer
|
---|
181 | } // class FixedOperatorBase
|
---|
182 |
|
---|
183 | class CancelException : Exception {
|
---|
184 |
|
---|
185 | } // class CancelException
|
---|
186 | } // namespace HeuristicLab.FixedOperators
|
---|