1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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.Linq;
|
---|
24 | using System.Threading;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HEAL.Attic;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.DebugEngine {
|
---|
30 |
|
---|
31 | [StorableType("BC1A9855-D280-4314-A272-CD2120679F12")]
|
---|
32 | [Item("Debug Engine", "Engine for debugging algorithms.")]
|
---|
33 | public class DebugEngine : Executable, IEngine {
|
---|
34 |
|
---|
35 | #region Construction and Cloning
|
---|
36 |
|
---|
37 | [StorableConstructor]
|
---|
38 | protected DebugEngine(StorableConstructorFlag _) : base(_) {
|
---|
39 | InitializeTimer();
|
---|
40 | }
|
---|
41 |
|
---|
42 | protected DebugEngine(DebugEngine original, Cloner cloner)
|
---|
43 | : base(original, cloner) {
|
---|
44 | if (original.ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
|
---|
45 | Log = cloner.Clone(original.Log);
|
---|
46 | ExecutionStack = cloner.Clone(original.ExecutionStack);
|
---|
47 | OperatorTrace = cloner.Clone(original.OperatorTrace);
|
---|
48 | InitializeTimer();
|
---|
49 | currentOperation = cloner.Clone(original.currentOperation);
|
---|
50 | }
|
---|
51 | public DebugEngine()
|
---|
52 | : base() {
|
---|
53 | Log = new Log();
|
---|
54 | ExecutionStack = new ExecutionStack();
|
---|
55 | OperatorTrace = new OperatorTrace();
|
---|
56 | InitializeTimer();
|
---|
57 | }
|
---|
58 |
|
---|
59 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
60 | return new DebugEngine(this, cloner);
|
---|
61 | }
|
---|
62 |
|
---|
63 | private void InitializeTimer() {
|
---|
64 | timer = new System.Timers.Timer(250);
|
---|
65 | timer.AutoReset = true;
|
---|
66 | timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
|
---|
67 | }
|
---|
68 |
|
---|
69 | #endregion
|
---|
70 |
|
---|
71 | #region Fields and Properties
|
---|
72 |
|
---|
73 | [Storable]
|
---|
74 | public ILog Log { get; private set; }
|
---|
75 |
|
---|
76 | [Storable]
|
---|
77 | public ExecutionStack ExecutionStack { get; private set; }
|
---|
78 |
|
---|
79 | [Storable]
|
---|
80 | public OperatorTrace OperatorTrace { get; private set; }
|
---|
81 |
|
---|
82 | private CancellationTokenSource cancellationTokenSource;
|
---|
83 | private bool stopPending;
|
---|
84 | private DateTime lastUpdateTime;
|
---|
85 | private System.Timers.Timer timer;
|
---|
86 |
|
---|
87 | [Storable]
|
---|
88 | private IOperation currentOperation;
|
---|
89 | public IOperation CurrentOperation {
|
---|
90 | get { return currentOperation; }
|
---|
91 | private set {
|
---|
92 | if (value != currentOperation) {
|
---|
93 | currentOperation = value;
|
---|
94 | OnOperationChanged(value);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | public virtual IAtomicOperation CurrentAtomicOperation {
|
---|
100 | get { return CurrentOperation as IAtomicOperation; }
|
---|
101 | }
|
---|
102 |
|
---|
103 | public virtual IExecutionContext CurrentExecutionContext {
|
---|
104 | get { return CurrentOperation as IExecutionContext; }
|
---|
105 | }
|
---|
106 |
|
---|
107 | public virtual bool CanContinue {
|
---|
108 | get { return CurrentOperation != null || ExecutionStack.Count > 0; }
|
---|
109 | }
|
---|
110 |
|
---|
111 | public virtual bool IsAtBreakpoint {
|
---|
112 | get { return CurrentAtomicOperation != null && CurrentAtomicOperation.Operator != null && CurrentAtomicOperation.Operator.Breakpoint; }
|
---|
113 | }
|
---|
114 |
|
---|
115 | #endregion
|
---|
116 |
|
---|
117 | #region Events
|
---|
118 |
|
---|
119 | public event EventHandler<OperationChangedEventArgs> CurrentOperationChanged;
|
---|
120 | protected virtual void OnOperationChanged(IOperation newOperation) {
|
---|
121 | EventHandler<OperationChangedEventArgs> handler = CurrentOperationChanged;
|
---|
122 | if (handler != null) {
|
---|
123 | handler(this, new OperationChangedEventArgs(newOperation));
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | #endregion
|
---|
128 |
|
---|
129 | #region Std Methods
|
---|
130 | public sealed override void Prepare() {
|
---|
131 | base.Prepare();
|
---|
132 | ExecutionStack.Clear();
|
---|
133 | CurrentOperation = null;
|
---|
134 | OperatorTrace.Reset();
|
---|
135 | OnPrepared();
|
---|
136 | }
|
---|
137 | public void Prepare(IOperation initialOperation) {
|
---|
138 | base.Prepare();
|
---|
139 | ExecutionStack.Clear();
|
---|
140 | if (initialOperation != null)
|
---|
141 | ExecutionStack.Add(initialOperation);
|
---|
142 | CurrentOperation = null;
|
---|
143 | OperatorTrace.Reset();
|
---|
144 | OnPrepared();
|
---|
145 | }
|
---|
146 | protected override void OnPrepared() {
|
---|
147 | Log.LogMessage("Engine prepared");
|
---|
148 | base.OnPrepared();
|
---|
149 | }
|
---|
150 |
|
---|
151 | public virtual void Step(bool skipStackOperations) {
|
---|
152 | OnStarted();
|
---|
153 | cancellationTokenSource = new CancellationTokenSource();
|
---|
154 | stopPending = false;
|
---|
155 | lastUpdateTime = DateTime.UtcNow;
|
---|
156 | timer.Start();
|
---|
157 | try {
|
---|
158 | ProcessNextOperation(true, cancellationTokenSource.Token);
|
---|
159 | while (skipStackOperations && !(CurrentOperation is IAtomicOperation) && CanContinue)
|
---|
160 | ProcessNextOperation(true, cancellationTokenSource.Token);
|
---|
161 | } catch (Exception ex) {
|
---|
162 | OnExceptionOccurred(ex);
|
---|
163 | }
|
---|
164 | timer.Stop();
|
---|
165 | ExecutionTime += DateTime.UtcNow - lastUpdateTime;
|
---|
166 | cancellationTokenSource.Dispose();
|
---|
167 | cancellationTokenSource = null;
|
---|
168 | if (stopPending) ExecutionStack.Clear();
|
---|
169 | if (stopPending || !CanContinue) OnStopped();
|
---|
170 | else OnPaused();
|
---|
171 | }
|
---|
172 |
|
---|
173 | public override void Start(CancellationToken cancellationToken) {
|
---|
174 | base.Start(cancellationToken);
|
---|
175 | cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
---|
176 | stopPending = false;
|
---|
177 |
|
---|
178 | try {
|
---|
179 | Run(cancellationTokenSource.Token);
|
---|
180 | } catch (OperationCanceledException) {
|
---|
181 | } catch (AggregateException ae) {
|
---|
182 | ae.FlattenAndHandle(new[] { typeof(OperationCanceledException) }, e => OnExceptionOccurred(e));
|
---|
183 | } catch (Exception e) {
|
---|
184 | OnExceptionOccurred(e);
|
---|
185 | }
|
---|
186 |
|
---|
187 | cancellationTokenSource.Dispose();
|
---|
188 | cancellationTokenSource = null;
|
---|
189 | if (stopPending) ExecutionStack.Clear();
|
---|
190 | if (stopPending || !CanContinue) OnStopped();
|
---|
191 | else OnPaused();
|
---|
192 | }
|
---|
193 | protected override void OnStarted() {
|
---|
194 | Log.LogMessage("Engine started");
|
---|
195 | base.OnStarted();
|
---|
196 | }
|
---|
197 |
|
---|
198 | public override void Pause() {
|
---|
199 | base.Pause();
|
---|
200 | cancellationTokenSource.Cancel();
|
---|
201 | }
|
---|
202 |
|
---|
203 | protected override void OnPaused() {
|
---|
204 | Log.LogMessage("Engine paused");
|
---|
205 | base.OnPaused();
|
---|
206 | }
|
---|
207 |
|
---|
208 | public override void Stop() {
|
---|
209 | CurrentOperation = null;
|
---|
210 | base.Stop();
|
---|
211 | if (ExecutionState == ExecutionState.Paused) {
|
---|
212 | ExecutionStack.Clear();
|
---|
213 | OnStopped();
|
---|
214 | } else {
|
---|
215 | stopPending = true;
|
---|
216 | cancellationTokenSource.Cancel();
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | protected override void OnStopped() {
|
---|
221 | Log.LogMessage("Engine stopped");
|
---|
222 | base.OnStopped();
|
---|
223 | }
|
---|
224 |
|
---|
225 | protected override void OnExceptionOccurred(Exception exception) {
|
---|
226 | Log.LogException(exception);
|
---|
227 | base.OnExceptionOccurred(exception);
|
---|
228 | }
|
---|
229 |
|
---|
230 | private void Run(object state) {
|
---|
231 | CancellationToken cancellationToken = (CancellationToken)state;
|
---|
232 |
|
---|
233 | OnStarted();
|
---|
234 | lastUpdateTime = DateTime.UtcNow;
|
---|
235 | timer.Start();
|
---|
236 | try {
|
---|
237 | if (!cancellationToken.IsCancellationRequested && CanContinue)
|
---|
238 | ProcessNextOperation(false, cancellationToken);
|
---|
239 | while (!cancellationToken.IsCancellationRequested && CanContinue && !IsAtBreakpoint)
|
---|
240 | ProcessNextOperation(false, cancellationToken);
|
---|
241 | cancellationToken.ThrowIfCancellationRequested();
|
---|
242 | } finally {
|
---|
243 | timer.Stop();
|
---|
244 | ExecutionTime += DateTime.UtcNow - lastUpdateTime;
|
---|
245 |
|
---|
246 | if (IsAtBreakpoint)
|
---|
247 | Log.LogMessage(string.Format("Breaking before: {0}", CurrentAtomicOperation.Operator.Name));
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
|
---|
252 | System.Timers.Timer timer = (System.Timers.Timer)sender;
|
---|
253 | timer.Enabled = false;
|
---|
254 | DateTime now = DateTime.UtcNow;
|
---|
255 | ExecutionTime += now - lastUpdateTime;
|
---|
256 | lastUpdateTime = now;
|
---|
257 | timer.Enabled = true;
|
---|
258 | }
|
---|
259 | #endregion
|
---|
260 |
|
---|
261 | #region Methods
|
---|
262 |
|
---|
263 |
|
---|
264 |
|
---|
265 | /// <summary>
|
---|
266 | /// Deals with the next operation, if it is an <see cref="AtomicOperation"/> it is executed,
|
---|
267 | /// if it is a <see cref="CompositeOperation"/> its single operations are pushed on the execution stack.
|
---|
268 | /// </summary>
|
---|
269 | /// <remarks>If an error occurs during the execution the operation is aborted and the operation
|
---|
270 | /// is pushed on the stack again.<br/>
|
---|
271 | /// If the execution was successful <see cref="EngineBase.OnOperationExecuted"/> is called.</remarks>
|
---|
272 | protected virtual void ProcessNextOperation(bool logOperations, CancellationToken cancellationToken) {
|
---|
273 | IAtomicOperation atomicOperation = CurrentOperation as IAtomicOperation;
|
---|
274 | OperationCollection operations = CurrentOperation as OperationCollection;
|
---|
275 | if (atomicOperation != null && operations != null)
|
---|
276 | throw new InvalidOperationException("Current operation is both atomic and an operation collection");
|
---|
277 |
|
---|
278 | if (atomicOperation != null) {
|
---|
279 | if (logOperations)
|
---|
280 | Log.LogMessage(string.Format("Performing atomic operation {0}", Utils.Name(atomicOperation)));
|
---|
281 | PerformAtomicOperation(atomicOperation, cancellationToken);
|
---|
282 | } else if (operations != null) {
|
---|
283 | if (logOperations)
|
---|
284 | Log.LogMessage("Expanding operation collection");
|
---|
285 | ExecutionStack.AddRange(operations.Reverse());
|
---|
286 | CurrentOperation = null;
|
---|
287 | } else if (ExecutionStack.Count > 0) {
|
---|
288 | if (logOperations)
|
---|
289 | Log.LogMessage("Popping execution stack");
|
---|
290 | CurrentOperation = ExecutionStack.Last();
|
---|
291 | ExecutionStack.RemoveAt(ExecutionStack.Count - 1);
|
---|
292 | } else {
|
---|
293 | if (logOperations)
|
---|
294 | Log.LogMessage("Nothing to do");
|
---|
295 | }
|
---|
296 | OperatorTrace.Regenerate(CurrentAtomicOperation);
|
---|
297 | }
|
---|
298 |
|
---|
299 | protected virtual void PerformAtomicOperation(IAtomicOperation operation, CancellationToken cancellationToken) {
|
---|
300 | if (operation != null) {
|
---|
301 | try {
|
---|
302 | IOperation successor = operation.Operator.Execute((IExecutionContext)operation, cancellationToken);
|
---|
303 | if (successor != null) {
|
---|
304 | OperatorTrace.RegisterParenthood(operation, successor);
|
---|
305 | ExecutionStack.Add(successor);
|
---|
306 | }
|
---|
307 | CurrentOperation = null;
|
---|
308 | } catch (Exception ex) {
|
---|
309 | if (ex is OperationCanceledException) throw;
|
---|
310 | else throw new OperatorExecutionException(operation.Operator, ex);
|
---|
311 | }
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | #endregion
|
---|
316 | }
|
---|
317 | }
|
---|