Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactoring and modularization of DebugEngine (#47)

File size: 9.1 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.Common.Resources;
27using HeuristicLab.Core;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30using HeuristicLab.Persistence.Auxiliary;
31namespace HeuristicLab.DebugEngine {
32
33  /// <summary>
34  /// Engine espcially for debugging
35  /// </summary>
36  [View("DebugEngine View")]
37  [Content(typeof(DebugEngine), true)]
38  public partial class DebugEngineView : ItemView {
39
40    #region Basics
41
42    /// <summary>
43    /// Gets or sets the current engine.
44    /// </summary>
45    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="EditorBase"/>.</remarks>
46    public new DebugEngine Content {
47      get { return (DebugEngine)base.Content; }
48      set { base.Content = value; }
49    }
50
51    /// <summary>
52    /// Initializes a new instance of <see cref="DebugEngineView"/>.
53    /// </summary>
54    public DebugEngineView() {
55      InitializeComponent();
56      updateButton.Image = VS2008ImageLibrary.Refresh;
57      stepButton.Image = VS2008ImageLibrary.MoveNext;
58      parentButton.Image = VS2008ImageLibrary.ArrowUp;
59    }
60
61    /// <summary>
62    /// Removes the event handlers from the underlying <see cref="IEngine"/>.
63    /// </summary>
64    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
65    protected override void DeregisterContentEvents() {
66      Content.CurrentOperationChanged -= new EventHandler<OperationChangedEventArgs>(Content_CurrentOperationChanged);
67      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
68      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
69      base.DeregisterContentEvents();
70    }
71
72    /// <summary>
73    /// Adds event handlers to the underlying <see cref="IEngine"/>.
74    /// </summary>
75    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
76    protected override void RegisterContentEvents() {
77      base.RegisterContentEvents();
78      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
79      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
80      Content.CurrentOperationChanged += new EventHandler<OperationChangedEventArgs>(Content_CurrentOperationChanged);
81    }
82
83    /// <summary>
84    /// Updates all controls with the latest data of the model.
85    /// </summary>
86    /// <remarks>Calls <see cref="EditorBase.UpdateControls"/> of base class <see cref="EditorBase"/>.</remarks>
87    protected override void OnContentChanged() {
88      base.OnContentChanged();
89      if (Content == null) {
90        logView.Content = null;
91        executionTimeTextBox.Text = "-";
92        executionStackView.Content = null;
93      } else {
94        logView.Content = Content.Log;
95        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
96        executionStackView.Content = Content.ExecutionStack;
97      }
98    }
99
100    protected override void SetEnabledStateOfControls() {
101      base.SetEnabledStateOfControls();
102      if (Content == null) {
103        logView.Enabled = false;
104        executionTimeTextBox.Enabled = false;
105      } else {
106        logView.Enabled = true;
107        executionTimeTextBox.Enabled = true;
108      }
109    }
110
111    #endregion
112
113    protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
114      if (InvokeRequired)
115        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
116      else
117        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
118    }
119
120    void Content_ExecutionStateChanged(object sender, EventArgs e) {
121      if (InvokeRequired)
122        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
123      else {
124        switch (Content.ExecutionState) {
125          case ExecutionState.Started: executionStackView.SuspendUpdate(); break;
126          default: executionStackView.ResumeUpdate(); break;
127        }
128      }
129    }
130
131    #region Current Operation
132
133    void Content_CurrentOperationChanged(object sender, OperationChangedEventArgs e) {
134      if (InvokeRequired)
135        Invoke(new EventHandler<OperationChangedEventArgs>(Content_CurrentOperationChanged), sender, e);
136      else
137        SetOperation(Content.CurrentOperation);
138    }
139
140    private void SetOperation(IOperation operation) {
141      IAtomicOperation atomicOperation = operation as IAtomicOperation;
142      operationTextBox.Text = "<none>";
143      parameterCollectionView.Content = null;
144      toolTip.SetToolTip(operationTextBox, null);
145      if (atomicOperation != null && atomicOperation.Operator != null) {
146        operationTextBox.Text = string.Format("Atomic {0}", atomicOperation.Operator.Name);
147        toolTip.SetToolTip(operationTextBox, Utils.TypeName(atomicOperation.Operator));
148      }
149      OperationCollection operationCollection = operation as OperationCollection;
150      if (operationCollection != null)
151        operationTextBox.Text = string.Format("Collection {0}", operationCollection.Count);
152      IExecutionContext context = operation as IExecutionContext;
153      IScope scope = null;
154      if (context != null) {
155        parameterCollectionView.Content = context.Parameters;
156        scope = context.Scope;
157        while (scope != null && scope.Parent != null)
158          scope = scope.Parent;
159      }
160      UpdateScope(scope);
161      if (context != null)
162        parentButton.Enabled = context.Parent != null;
163      scopeTreeView.Tag = context;
164    }
165
166    private void UpdateScope(IScope scope) {
167      scopeTreeView.BeginUpdate();
168      scopeTreeView.Nodes.Clear();
169      if (scope != null) {
170        AddScope(scopeTreeView.Nodes, scope);
171      }
172      scopeTreeView.ExpandAll();
173      if (scopeTreeView.Nodes.Count > 0)
174        scopeTreeView.TopNode = scopeTreeView.Nodes[0];
175      scopeTreeView.EndUpdate();
176    }
177
178   
179
180    /*
181    private string GetApproximateValue(IParameter param, ref string typeName) {
182      string valueString = "<none>";
183      IExecutionContext context = Content.CurrentOperation as IExecutionContext;
184      IExecutionContext originalContext = param.ExecutionContext;
185      object value = null;
186      try {
187        try {
188          param.ExecutionContext = context;
189          value = param.ActualValue;
190        } finally {
191          param.ExecutionContext = originalContext;
192        }
193      } catch (Exception) { }
194      if (value != null) {
195        valueString = value.ToString();
196        if (context != originalContext)
197          valueString = " ~ " + valueString;
198        typeName = TypeName(value);
199      }
200      return valueString;
201    }
202     */
203
204    private void AddScope(TreeNodeCollection nodes, IScope scope) {
205      TreeNode node = nodes.Add(scope.Name);
206      if (Content.CurrentAtomicOperation != null && Content.CurrentAtomicOperation.Scope == scope) {
207        node.ForeColor = Color.Red;
208        node.BackColor = Color.LightGray;
209      }
210      foreach (var var in scope.Variables) {
211        TreeNode varNode = node.Nodes.Add(string.Format("{0}={1}", var.Name, var.Value.ToString()));
212        varNode.Tag = var.Value;
213        varNode.ToolTipText = Utils.TypeName(var.Value);
214      }
215      foreach (var subScope in scope.SubScopes) {
216        AddScope(node.Nodes, subScope);
217      }
218    }
219
220    #endregion
221
222    protected virtual void UpdateView() {
223      if (InvokeRequired) {
224        Invoke(new Action(UpdateView));
225      } else {
226        SetOperation(Content.CurrentOperation);
227      }
228    }
229
230    private void stepButton_Click(object sender, EventArgs e) {
231      Content.Step();
232      UpdateView();
233    }
234
235    private void updateButton_Click(object sender, EventArgs e) {
236      UpdateView();
237    }
238
239    private void scopeTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
240      if (e.Node.Tag != null)
241        MainFormManager.MainForm.ShowContent((IItem)e.Node.Tag);
242    }
243
244    private void parentButton_Click(object sender, EventArgs e) {
245      IExecutionContext context = scopeTreeView.Tag as IExecutionContext;
246      if (context != null) {
247        SetOperation(context.Parent as IOperation);
248      }
249    }
250  }
251}
Note: See TracBrowser for help on using the repository browser.