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