Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DebugEngine/DebugEngine.cs @ 4857

Last change on this file since 4857 was 4827, checked in by epitzer, 14 years ago

Prevent stepping execution on empty execution stack (#47)

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.DebugEngine {
29
30  [StorableClass]
31  [Item("Debug Engine", "Engine for debugging algorithms.")]
32  public class DebugEngine : Engine {
33    private IOperator currentOperator;
34
35    [StorableConstructor]
36    protected DebugEngine(bool deserializing) : base(deserializing) { }
37    protected DebugEngine(DebugEngine original, Cloner cloner) : base(original, cloner) { }
38    public DebugEngine()
39      : base() {
40    }
41
42    public new Stack<IOperation> ExecutionStack {
43      get { return base.ExecutionStack; }
44    }
45
46    public IAtomicOperation CurrentOperation { get; private set; }
47
48    public override IDeepCloneable Clone(Cloner cloner) {
49      return new DebugEngine(this, cloner);
50    }
51
52    /// <summary>
53    /// Deals with the next operation, if it is an <see cref="AtomicOperation"/> it is executed,
54    /// if it is a <see cref="CompositeOperation"/> its single operations are pushed on the execution stack.
55    /// </summary>
56    /// <remarks>If an error occurs during the execution the operation is aborted and the operation
57    /// is pushed on the stack again.<br/>
58    /// If the execution was successful <see cref="EngineBase.OnOperationExecuted"/> is called.</remarks>
59    protected override void ProcessNextOperation() {
60      currentOperator = null;
61      IOperation next = ExecutionStack.Pop();
62      OperationCollection coll = next as OperationCollection;
63      while (coll != null) {
64        Log.LogMessage("Expanding OperationCollection");
65        for (int i = coll.Count - 1; i >= 0; i--) {
66          ExecutionStack.Push(coll[i]);
67        }
68        next = ExecutionStack.Count > 0 ? ExecutionStack.Pop() : null;
69        coll = next as OperationCollection;
70      }
71      IAtomicOperation operation = next as IAtomicOperation;
72      if (operation != null) {
73        Log.LogMessage("Preparing IAtomicOperation");
74        try {
75          currentOperator = operation.Operator;
76          CurrentOperation = operation;
77          if (operation.Operator.Breakpoint) {
78            Log.LogMessage(string.Format("Breakpoint: Before {0}", operation.Operator.Name != string.Empty ? operation.Operator.Name : operation.Operator.ItemName));
79            Pause();
80          }
81          Log.LogMessage("Executing IAtomicOperation");
82          ExecutionStack.Push(operation.Operator.Execute((IExecutionContext)operation));
83        } catch (Exception ex) {
84          OnExceptionOccurred(new OperatorExecutionException(operation.Operator, ex));
85          Pause();
86        }
87      } else {
88        Log.LogMessage("Nothing to do");
89        CurrentOperation = null;
90      }
91    }
92
93    public override void Pause() {
94      base.Pause();
95      if (currentOperator != null) currentOperator.Abort();
96    }
97    public override void Stop() {
98      base.Stop();
99      if (currentOperator != null) currentOperator.Abort();
100    }
101
102    public virtual void Step() {
103      OnStarted();
104      var lastUpdateTime = DateTime.Now;
105      if (ExecutionStack.Count > 0) {
106        while (ExecutionStack.Count > 0 && ExecutionStack.Peek() == null)
107          ExecutionStack.Pop();
108        if (ExecutionStack.Count > 0)
109          ProcessNextOperation();
110      }
111      ExecutionTime += DateTime.Now - lastUpdateTime;
112      OnPaused();
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.