Free cookie consent management tool by TermsFeed Policy Generator

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

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

Show expected parameter values and include type information in tool tip (#47)

File size: 8.6 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 System.Drawing;
25using System.Windows.Forms;
26using HeuristicLab.Core;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29using HeuristicLab.Persistence.Auxiliary;
30namespace HeuristicLab.DebugEngine {
31  /// <summary>
32  /// Base class for editors of engines.
33  /// </summary>
34  [View("DebugEngine View")]
35  [Content(typeof(DebugEngine), true)]
36  public partial class DebugEngineView : ItemView {
37    /// <summary>
38    /// Gets or sets the current engine.
39    /// </summary>
40    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="EditorBase"/>.</remarks>
41    public new DebugEngine Content {
42      get { return (DebugEngine)base.Content; }
43      set { base.Content = value; }
44    }
45
46    /// <summary>
47    /// Initializes a new instance of <see cref="EngineBaseEditor"/>.
48    /// </summary>
49    public DebugEngineView() {
50      InitializeComponent();
51    }
52
53    /// <summary>
54    /// Removes the event handlers from the underlying <see cref="IEngine"/>.
55    /// </summary>
56    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
57    protected override void DeregisterContentEvents() {
58      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
59      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
60      base.DeregisterContentEvents();
61    }
62
63    /// <summary>
64    /// Adds event handlers to the underlying <see cref="IEngine"/>.
65    /// </summary>
66    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
67    protected override void RegisterContentEvents() {
68      base.RegisterContentEvents();
69      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
70      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
71    }
72
73    /// <summary>
74    /// Updates all controls with the latest data of the model.
75    /// </summary>
76    /// <remarks>Calls <see cref="EditorBase.UpdateControls"/> of base class <see cref="EditorBase"/>.</remarks>
77    protected override void OnContentChanged() {
78      base.OnContentChanged();
79      if (Content == null) {
80        logView.Content = null;
81        executionTimeTextBox.Text = "-";
82      } else {
83        logView.Content = Content.Log;
84        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
85      }
86    }
87
88    protected override void SetEnabledStateOfControls() {
89      base.SetEnabledStateOfControls();
90      if (Content == null) {
91        logView.Enabled = false;
92        executionTimeTextBox.Enabled = false;
93      } else {
94        logView.Enabled = true;
95        executionTimeTextBox.Enabled = true;
96      }
97    }
98
99    protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
100      if (InvokeRequired)
101        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
102      else
103        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
104    }
105
106    void Content_ExecutionStateChanged(object sender, EventArgs e) {
107      switch (Content.ExecutionState) {
108        case ExecutionState.Paused:
109        case ExecutionState.Stopped:
110        case ExecutionState.Prepared:
111          UpdateView(); break;
112      }
113    }
114
115    protected virtual void UpdateView() {
116      if (InvokeRequired) {
117        Invoke(new Action(UpdateView));
118      } else {
119        executionStackTreeView.BeginUpdate();
120        executionStackTreeView.Nodes.Clear();
121        AddOperations(executionStackTreeView.Nodes, Content.ExecutionStack.ToArray());
122        executionStackTreeView.ExpandAll();
123        if (executionStackTreeView.Nodes.Count > 0)
124          executionStackTreeView.TopNode = executionStackTreeView.Nodes[0];
125        executionStackTreeView.EndUpdate();
126
127
128        scopeTreeView.BeginUpdate();
129        scopeTreeView.Nodes.Clear();
130        if (Content.CurrentOperation != null) {
131          AddScope(scopeTreeView.Nodes, Content.CurrentOperation.Scope);
132        }
133        scopeTreeView.ExpandAll();
134        if (scopeTreeView.Nodes.Count > 0)
135          scopeTreeView.TopNode = scopeTreeView.Nodes[0];
136        scopeTreeView.EndUpdate();
137      }
138    }
139
140    private string TypeName(object obj) {
141      if (obj == null)
142        return "null";
143      return TypeNameParser.Parse(obj.GetType().ToString()).GetTypeNameInCode(true);
144    }
145
146    private void AddOperations(TreeNodeCollection nodes, IEnumerable<IOperation> operations) {
147      foreach (IOperation op in operations) {
148        if (op is IAtomicOperation) {
149          IAtomicOperation atom = op as IAtomicOperation;
150          TreeNode node = nodes.Add(atom.Operator.Name);
151          node.Tag = atom;
152          node.ToolTipText = TypeName(atom);
153          if (atom.Operator.Breakpoint)
154            node.ForeColor = Color.Red;
155          foreach (var param in atom.Operator.Parameters) {
156            string typeName = "null";
157            TreeNode paramNode = node.Nodes.Add(string.Format("Param {0} = {1}", param.Name, GetApproximateValue(param, ref typeName)));
158            paramNode.Tag = param;
159            paramNode.ToolTipText = string.Format("{0} = {1}", TypeName(param), typeName);
160          }
161        } else if (op is OperationCollection) {
162          OperationCollection ops = op as OperationCollection;
163          TreeNode node = executionStackTreeView.Nodes.Add(string.Format("{0} Operations", ops.Count));
164          node.Tag = op;
165          node.ToolTipText = TypeName(ops);
166          AddOperations(node.Nodes, ops);
167        }
168      }
169    }
170
171    private string GetApproximateValue(IParameter param, ref string typeName) {
172      string value = "<none>";
173      try {
174        IExecutionContext context = Content.CurrentOperation as IExecutionContext;
175        IExecutionContext oldContext = param.ExecutionContext;
176        if (context != null) {
177          param.ExecutionContext = context;
178        }
179        value = param.ActualValue.ToString();
180        typeName = TypeName(param.ActualValue);
181        if (context != oldContext) {
182          param.ExecutionContext = oldContext;
183          value = string.Format("~ {0}", value);
184        }
185      } catch (Exception) { }
186      return value;
187    }
188
189    private void AddScope(TreeNodeCollection nodes, IScope scope) {
190      TreeNode node = nodes.Add(scope.Name);
191      foreach (var var in scope.Variables) {
192        TreeNode varNode = node.Nodes.Add(string.Format("{0}={1}", var.Name, var.Value.ToString()));
193        varNode.Tag = var.Value;
194        varNode.ToolTipText = TypeName(var.Value);
195      }
196      foreach (var subScope in scope.SubScopes) {
197        AddScope(node.Nodes, subScope);
198      }
199    }
200
201    private void stepButton_Click(object sender, EventArgs e) {
202      Content.Step();
203      UpdateView();
204    }
205
206    private void executionStackTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
207      if (e.Node != null) {
208        IAtomicOperation op = e.Node.Tag as IAtomicOperation;
209        if (op != null) {
210          op.Operator.Breakpoint = !op.Operator.Breakpoint;
211          if (op.Operator.Breakpoint) {
212            e.Node.ForeColor = Color.Red;
213          } else {
214            e.Node.ForeColor = Color.Black;
215          }
216          executionStackTreeView.SelectedNode = null;
217        }
218        IParameter param = e.Node.Tag as IParameter;
219        if (param != null)
220          MainFormManager.MainForm.ShowContent(param);
221      }
222    }
223
224    private void updateButton_Click(object sender, EventArgs e) {
225      UpdateView();
226    }
227
228    private void scopeTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
229      if (e.Node.Tag != null)
230        MainFormManager.MainForm.ShowContent((IItem)e.Node.Tag);
231    }
232  }
233}
Note: See TracBrowser for help on using the repository browser.