1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.DebugEngine {
|
---|
8 |
|
---|
9 | [StorableClass]
|
---|
10 | [Item("Debug Engine", "Engine for debugging algorithms.")]
|
---|
11 | public class DebugEngine : Engine {
|
---|
12 | private IOperator currentOperator;
|
---|
13 |
|
---|
14 | [StorableConstructor]
|
---|
15 | protected DebugEngine(bool deserializing) : base(deserializing) { }
|
---|
16 | protected DebugEngine(DebugEngine original, Cloner cloner) : base(original, cloner) { }
|
---|
17 | public DebugEngine()
|
---|
18 | : base() {
|
---|
19 | }
|
---|
20 |
|
---|
21 | public new Stack<IOperation> ExecutionStack {
|
---|
22 | get { return base.ExecutionStack; }
|
---|
23 | }
|
---|
24 |
|
---|
25 | public IAtomicOperation CurrentOperation { get; private set; }
|
---|
26 |
|
---|
27 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
28 | return new DebugEngine(this, cloner);
|
---|
29 | }
|
---|
30 |
|
---|
31 | /// <summary>
|
---|
32 | /// Deals with the next operation, if it is an <see cref="AtomicOperation"/> it is executed,
|
---|
33 | /// if it is a <see cref="CompositeOperation"/> its single operations are pushed on the execution stack.
|
---|
34 | /// </summary>
|
---|
35 | /// <remarks>If an error occurs during the execution the operation is aborted and the operation
|
---|
36 | /// is pushed on the stack again.<br/>
|
---|
37 | /// If the execution was successful <see cref="EngineBase.OnOperationExecuted"/> is called.</remarks>
|
---|
38 | protected override void ProcessNextOperation() {
|
---|
39 | currentOperator = null;
|
---|
40 | IOperation next = ExecutionStack.Pop();
|
---|
41 | OperationCollection coll = next as OperationCollection;
|
---|
42 | while (coll != null) {
|
---|
43 | for (int i = coll.Count - 1; i >= 0; i--) {
|
---|
44 | ExecutionStack.Push(coll[i]);
|
---|
45 | }
|
---|
46 | next = ExecutionStack.Count > 0 ? ExecutionStack.Pop() : null;
|
---|
47 | coll = next as OperationCollection;
|
---|
48 | }
|
---|
49 | IAtomicOperation operation = next as IAtomicOperation;
|
---|
50 | if (operation != null) {
|
---|
51 | try {
|
---|
52 | currentOperator = operation.Operator;
|
---|
53 | CurrentOperation = operation;
|
---|
54 | if (operation.Operator.Breakpoint) {
|
---|
55 | Log.LogMessage(string.Format("Breakpoint: Before {0}", operation.Operator.Name != string.Empty ? operation.Operator.Name : operation.Operator.ItemName));
|
---|
56 | Pause();
|
---|
57 | }
|
---|
58 | ExecutionStack.Push(operation.Operator.Execute((IExecutionContext)operation));
|
---|
59 | } catch (Exception ex) {
|
---|
60 | OnExceptionOccurred(new OperatorExecutionException(operation.Operator, ex));
|
---|
61 | Pause();
|
---|
62 | }
|
---|
63 | } else {
|
---|
64 | CurrentOperation = null;
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | public override void Pause() {
|
---|
69 | base.Pause();
|
---|
70 | if (currentOperator != null) currentOperator.Abort();
|
---|
71 | }
|
---|
72 | public override void Stop() {
|
---|
73 | base.Stop();
|
---|
74 | if (currentOperator != null) currentOperator.Abort();
|
---|
75 | }
|
---|
76 |
|
---|
77 | public virtual void Step() {
|
---|
78 | OnStarted();
|
---|
79 | var lastUpdateTime = DateTime.Now;
|
---|
80 | if (ExecutionStack.Count > 0)
|
---|
81 | ProcessNextOperation();
|
---|
82 | ExecutionTime += DateTime.Now - lastUpdateTime;
|
---|
83 | OnPaused();
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|