Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DebugEngine/DebugEngineView.cs @ 4743

Last change on this file since 4743 was 4743, checked in by epitzer, 13 years ago

Preliminary version of debug engine with stepping, execution stack view and current operator scope view (#47)

File size: 5.6 KB
Line 
1
2using System;
3using System.Collections.Generic;
4using System.Drawing;
5using System.Windows.Forms;
6using HeuristicLab.Core;
7using HeuristicLab.Core.Views;
8using HeuristicLab.MainForm;
9namespace HeuristicLab.DebugEngine {
10  /// <summary>
11  /// Base class for editors of engines.
12  /// </summary>
13  [View("DebugEngine View")]
14  [Content(typeof(DebugEngine), true)]
15  public partial class DebugEngineView : ItemView {
16    /// <summary>
17    /// Gets or sets the current engine.
18    /// </summary>
19    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="EditorBase"/>.</remarks>
20    public new DebugEngine Content {
21      get { return (DebugEngine)base.Content; }
22      set { base.Content = value; }
23    }
24
25    /// <summary>
26    /// Initializes a new instance of <see cref="EngineBaseEditor"/>.
27    /// </summary>
28    public DebugEngineView() {
29      InitializeComponent();
30    }
31
32    /// <summary>
33    /// Removes the event handlers from the underlying <see cref="IEngine"/>.
34    /// </summary>
35    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
36    protected override void DeregisterContentEvents() {
37      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
38      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
39      base.DeregisterContentEvents();
40    }
41
42    /// <summary>
43    /// Adds event handlers to the underlying <see cref="IEngine"/>.
44    /// </summary>
45    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
46    protected override void RegisterContentEvents() {
47      base.RegisterContentEvents();
48      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
49      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
50    }
51
52    /// <summary>
53    /// Updates all controls with the latest data of the model.
54    /// </summary>
55    /// <remarks>Calls <see cref="EditorBase.UpdateControls"/> of base class <see cref="EditorBase"/>.</remarks>
56    protected override void OnContentChanged() {
57      base.OnContentChanged();
58      if (Content == null) {
59        logView.Content = null;
60        executionTimeTextBox.Text = "-";
61      } else {
62        logView.Content = Content.Log;
63        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
64      }
65    }
66
67    protected override void SetEnabledStateOfControls() {
68      base.SetEnabledStateOfControls();
69      if (Content == null) {
70        logView.Enabled = false;
71        executionTimeTextBox.Enabled = false;
72      } else {
73        logView.Enabled = true;
74        executionTimeTextBox.Enabled = true;
75      }
76    }
77
78    protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
79      if (InvokeRequired)
80        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
81      else
82        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
83    }
84
85    void Content_ExecutionStateChanged(object sender, EventArgs e) {
86      switch (Content.ExecutionState) {
87        case ExecutionState.Paused:
88        case ExecutionState.Stopped:
89        case ExecutionState.Prepared:
90          UpdateView(); break;
91      }
92    }
93
94    protected virtual void UpdateView() {
95      if (InvokeRequired) {
96        Invoke(new Action(UpdateView));
97      } else {
98        executionStackTreeView.Nodes.Clear();
99        AddOperations(executionStackTreeView.Nodes, Content.ExecutionStack.ToArray());
100        executionStackTreeView.ExpandAll();
101
102        scopeTreeView.Nodes.Clear();
103        if (Content.CurrentOperation != null) {
104          AddScope(scopeTreeView.Nodes, Content.CurrentOperation.Scope);
105        }
106        scopeTreeView.ExpandAll();
107      }
108    }
109
110    private void AddOperations(TreeNodeCollection nodes, IEnumerable<IOperation> operations) {
111      foreach (IOperation op in operations) {
112        if (op is IAtomicOperation) {
113          IAtomicOperation atom = op as IAtomicOperation;
114          TreeNode node = nodes.Add(atom.Operator.Name);
115          node.Tag = atom;
116          if (atom.Operator.Breakpoint)
117            node.ForeColor = Color.Red;
118        } else if (op is OperationCollection) {
119          OperationCollection ops = op as OperationCollection;
120          TreeNode node = executionStackTreeView.Nodes.Add(string.Format("{0} Operations", ops.Count));
121          node.Tag = op;
122          AddOperations(node.Nodes, ops);
123        }
124      }
125    }
126
127    private void AddScope(TreeNodeCollection nodes, IScope scope) {
128      TreeNode node = nodes.Add(scope.Name);
129      foreach (var var in scope.Variables) {
130        node.Nodes.Add(string.Format("{0}={1}", var.Name, var.Value.ToString()));
131      }
132      foreach (var subScope in scope.SubScopes) {
133        AddScope(node.Nodes, subScope);
134      }
135    }
136
137    private void stepButton_Click(object sender, EventArgs e) {
138      Content.Step();
139      UpdateView();
140    }
141
142    private void executionStackTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
143      if (e.Node != null) {
144        IAtomicOperation op = e.Node.Tag as IAtomicOperation;
145        if (op != null) {
146          op.Operator.Breakpoint = !op.Operator.Breakpoint;
147          if (op.Operator.Breakpoint) {
148            e.Node.ForeColor = Color.Red;
149          } else {
150            e.Node.ForeColor = Color.Black;
151          }
152          executionStackTreeView.SelectedNode = null;
153        }
154      }
155    }
156
157    private void updateButton_Click(object sender, EventArgs e) {
158      UpdateView();
159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.