[14897] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Drawing;
|
---|
| 4 | using System.Linq;
|
---|
| 5 | using System.Windows.Forms;
|
---|
[15771] | 6 | using HeuristicLab.Core.Views;
|
---|
| 7 | using HeuristicLab.MainForm;
|
---|
[14897] | 8 |
|
---|
[15771] | 9 | namespace HeuristicLab.Problems.ProgramSynthesis.Views {
|
---|
[15289] | 10 |
|
---|
[14897] | 11 | public partial class PushDebuggerView : ItemView {
|
---|
[15017] | 12 | private readonly IDictionary<StackTypes, ListBox> debugListDict = new Dictionary<StackTypes, ListBox>();
|
---|
| 13 | private readonly IDictionary<StackTypes, PushProgramTreeView> debugPushProgramDict = new Dictionary<StackTypes, PushProgramTreeView>();
|
---|
[14897] | 14 |
|
---|
| 15 | private PooledPushInterpreter interpreter;
|
---|
| 16 | private PushInterpreterPool pool;
|
---|
| 17 |
|
---|
[14952] | 18 | private const string GROUP_BOX_TEXT_STRING_FORMAT = "{0}[{1}]";
|
---|
| 19 | private const string EXEC_COUNT_LABEL_FORMAT = "{0}/{1} max. expressions evaluated";
|
---|
[14897] | 20 |
|
---|
| 21 | public event EventHandler<IPushInterpreter> OnReset;
|
---|
| 22 |
|
---|
| 23 | public PushDebuggerView() {
|
---|
| 24 | InitializeComponent();
|
---|
| 25 | InitEvents();
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | ~PushDebuggerView() {
|
---|
| 29 | interpreter.Dispose();
|
---|
| 30 | }
|
---|
| 31 |
|
---|
[15771] | 32 | public new PushSolution Content {
|
---|
[14897] | 33 | get { return (PushSolution)base.Content; }
|
---|
[15771] | 34 | set {
|
---|
[14897] | 35 | base.Content = value;
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | protected override void OnContentChanged() {
|
---|
| 40 | if (Content == null) return;
|
---|
| 41 |
|
---|
| 42 | Name = "Push Debugger";
|
---|
| 43 | pool = new PushInterpreterPool(Content.Config);
|
---|
| 44 |
|
---|
| 45 | if (interpreter != null) {
|
---|
| 46 | interpreter.Dispose();
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[15273] | 49 | var random = Content.GetRandom();
|
---|
| 50 | interpreter = pool.Create(random);
|
---|
[14897] | 51 |
|
---|
| 52 | InitDebugLists(Content.Config);
|
---|
| 53 | ClearDebugLists();
|
---|
| 54 | UpdateExecList();
|
---|
| 55 | UpdateDebugLists();
|
---|
| 56 | ResetDebugging();
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[14952] | 59 | private void UpdateExecCountLabel() {
|
---|
| 60 | execCountLabel.Text = string.Format(
|
---|
| 61 | EXEC_COUNT_LABEL_FORMAT,
|
---|
| 62 | interpreter.ExecCounter,
|
---|
| 63 | Content.Config.EvalPushLimit);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[14897] | 66 | private void InitEvents() {
|
---|
| 67 | runButton.Click += RunButtonClick;
|
---|
| 68 | resetButton.Click += ResetButtonClick;
|
---|
| 69 | stepButton.Click += StepButtonClick;
|
---|
| 70 | simplifyButton.Click += SimplifyButtonClick;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | private void RunButtonClick(object sender, EventArgs e) {
|
---|
| 74 | if (interpreter == null)
|
---|
| 75 | return;
|
---|
| 76 |
|
---|
| 77 | interpreter.Resume();
|
---|
[14952] | 78 | UpdateStep();
|
---|
[14897] | 79 | }
|
---|
| 80 |
|
---|
| 81 | private void StepButtonClick(object sender, EventArgs e) {
|
---|
| 82 | if (interpreter == null || stepWidthBox.Value <= 0)
|
---|
| 83 | return;
|
---|
| 84 |
|
---|
| 85 | var count = Math.Min(stepWidthBox.Value, interpreter.ExecStack.Count);
|
---|
| 86 |
|
---|
[14952] | 87 | for (var i = 0; i < count; i++) {
|
---|
| 88 | SkipNoops();
|
---|
[14897] | 89 |
|
---|
[14952] | 90 | // run next none noop expression
|
---|
| 91 | interpreter.Step();
|
---|
[14897] | 92 | }
|
---|
| 93 |
|
---|
[14952] | 94 | // skip trailing noops so next expression is a none noop in debugger
|
---|
| 95 | SkipNoops();
|
---|
[14897] | 96 |
|
---|
| 97 | stepWidthBox.Maximum = Math.Max(1, interpreter.ExecStack.Count);
|
---|
[14952] | 98 | UpdateStep();
|
---|
| 99 | }
|
---|
[14897] | 100 |
|
---|
[14952] | 101 | private void UpdateStep() {
|
---|
[14897] | 102 | UpdateExecList();
|
---|
| 103 | UpdateDebugLists();
|
---|
[14952] | 104 | UpdateExecCountLabel();
|
---|
[14897] | 105 | CheckIfButtonsCanBeEnabled();
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[14952] | 108 | private void SkipNoops() {
|
---|
| 109 | // skip noops
|
---|
| 110 | if (skipNoopsCheckBox.Checked) {
|
---|
| 111 | while (interpreter.CanStep && interpreter.ExecStack.Top.IsNoop(interpreter)) {
|
---|
| 112 | interpreter.Step();
|
---|
| 113 | }
|
---|
[14897] | 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | private void CheckIfButtonsCanBeEnabled() {
|
---|
| 118 | runButton.Enabled = interpreter != null && interpreter.CanStep;
|
---|
| 119 | stepButton.Enabled = interpreter != null && interpreter.CanStep;
|
---|
| 120 | stepWidthBox.Enabled = interpreter != null && interpreter.CanStep;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | private void ResetButtonClick(object sender, EventArgs e) {
|
---|
| 124 | ResetDebugging();
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | private void SimplifyButtonClick(object sender, EventArgs e) {
|
---|
| 128 | var newContent = Content.Simplify();
|
---|
| 129 |
|
---|
| 130 | MainFormManager.MainForm.ShowContent(newContent, GetType());
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | public void ResetDebugging() {
|
---|
| 134 | if (Content == null ||
|
---|
| 135 | pool == null ||
|
---|
| 136 | Content.Program == null)
|
---|
| 137 | return;
|
---|
| 138 |
|
---|
| 139 | if (interpreter != null) {
|
---|
| 140 | interpreter.Reset();
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | if (OnReset != null) {
|
---|
| 144 | OnReset(this, interpreter);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | interpreter.Run(Content.Program, true);
|
---|
| 148 |
|
---|
| 149 | stepWidthBox.Maximum = interpreter.ExecStack.Count;
|
---|
[14952] | 150 | UpdateStep();
|
---|
[14897] | 151 | }
|
---|
| 152 |
|
---|
| 153 | private void ClearDebugLists() {
|
---|
[15017] | 154 | foreach (var list in debugListDict.Values) {
|
---|
[14897] | 155 | list.Items.Clear();
|
---|
| 156 | }
|
---|
[15017] | 157 |
|
---|
| 158 | foreach (var treeView in debugPushProgramDict.Values) {
|
---|
| 159 | treeView.Nodes.Clear();
|
---|
| 160 | }
|
---|
[14897] | 161 | }
|
---|
| 162 |
|
---|
| 163 | private void UpdateExecList() {
|
---|
[15017] | 164 | execTreeView.LoadExpressions(interpreter.ExecStack.Reverse());
|
---|
| 165 | exec2GroupBox.Text = string.Format(GROUP_BOX_TEXT_STRING_FORMAT, Enum.GetName(typeof(StackTypes), StackTypes.Exec), interpreter.ExecStack.Count);
|
---|
[14897] | 166 | }
|
---|
| 167 |
|
---|
| 168 | private void InitDebugLists(IReadOnlyPushConfiguration config) {
|
---|
[15017] | 169 | debugListDict.Clear();
|
---|
| 170 | debugPushProgramDict.Clear();
|
---|
[14897] | 171 |
|
---|
| 172 | // 2 = ExecList + EmptyColumn which is required to fill empty space
|
---|
| 173 | while (debugTableLayout.ColumnCount > 2) {
|
---|
| 174 | debugTableLayout.Controls.RemoveAt(1);
|
---|
| 175 | debugTableLayout.ColumnCount--;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | foreach (StackTypes stackType in Enum.GetValues(typeof(StackTypes))) {
|
---|
[15032] | 179 | if (stackType == StackTypes.Exec || !config.IsStackEnabled(stackType))
|
---|
| 180 | continue;
|
---|
[15017] | 181 |
|
---|
[15032] | 182 | switch (stackType) {
|
---|
| 183 | case StackTypes.Code:
|
---|
| 184 | var treeView = CreatePushProgramTreeView(stackType);
|
---|
| 185 | debugPushProgramDict.Add(stackType, treeView);
|
---|
| 186 | break;
|
---|
| 187 | default:
|
---|
| 188 | var list = CreateDebugList(stackType);
|
---|
| 189 | debugListDict.Add(stackType, list);
|
---|
| 190 | break;
|
---|
[14897] | 191 | }
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[15017] | 195 | private PushProgramTreeView CreatePushProgramTreeView(StackTypes type) {
|
---|
| 196 |
|
---|
| 197 | var treeView = new PushProgramTreeView {
|
---|
| 198 | Dock = DockStyle.Fill
|
---|
[14897] | 199 | };
|
---|
| 200 |
|
---|
[15017] | 201 | AddStackControlToTableLayout(type, treeView, 400);
|
---|
| 202 |
|
---|
| 203 | return treeView;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | private void AddStackControlToTableLayout(StackTypes type, Control control, int width) {
|
---|
| 207 | var groupBox = CreateStackGroupBox(type);
|
---|
| 208 | groupBox.Controls.Add(control);
|
---|
| 209 |
|
---|
| 210 | debugTableLayout.ColumnCount++;
|
---|
| 211 | debugTableLayout.ColumnStyles.Insert(1, new ColumnStyle(SizeType.Absolute, width));
|
---|
| 212 | debugTableLayout.Controls.Add(groupBox);
|
---|
| 213 | debugTableLayout.Controls.SetChildIndex(groupBox, 1);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | private ListBox CreateDebugList(StackTypes type) {
|
---|
[14897] | 217 | var list = new ListBox {
|
---|
[14952] | 218 | Dock = DockStyle.Fill
|
---|
[14897] | 219 | };
|
---|
| 220 |
|
---|
[14914] | 221 | // align numbers right
|
---|
[15289] | 222 | if (type == StackTypes.Integer ||
|
---|
| 223 | type == StackTypes.Float) {
|
---|
[14952] | 224 | list.DrawMode = DrawMode.OwnerDrawFixed;
|
---|
[14897] | 225 | list.DrawItem += (sender, e) => {
|
---|
| 226 | if (e.Index <= -1) return;
|
---|
| 227 | var item = list.Items[e.Index];
|
---|
| 228 |
|
---|
| 229 | e.DrawBackground();
|
---|
| 230 | e.DrawFocusRectangle();
|
---|
| 231 |
|
---|
| 232 | var brush = new SolidBrush(e.ForeColor);
|
---|
| 233 | var size = e.Graphics.MeasureString(item.ToString(), e.Font);
|
---|
| 234 |
|
---|
| 235 | e.Graphics.DrawString(
|
---|
| 236 | item.ToString(),
|
---|
| 237 | e.Font,
|
---|
| 238 | brush,
|
---|
| 239 | e.Bounds.Right - size.Width,
|
---|
| 240 | e.Bounds.Top + (e.Bounds.Height / 2 - size.Height / 2));
|
---|
| 241 | };
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[15017] | 244 | AddStackControlToTableLayout(type, list, 150);
|
---|
[14897] | 245 |
|
---|
| 246 | return list;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[15017] | 249 | private static GroupBox CreateStackGroupBox(StackTypes type) {
|
---|
| 250 | return new GroupBox {
|
---|
| 251 | Anchor = AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top,
|
---|
| 252 | AutoSize = true,
|
---|
| 253 | AutoSizeMode = AutoSizeMode.GrowAndShrink,
|
---|
| 254 | Text = Enum.GetName(typeof(StackTypes), type)
|
---|
| 255 | };
|
---|
| 256 | }
|
---|
[14897] | 257 |
|
---|
| 258 | private void UpdateDebugLists() {
|
---|
| 259 | ClearDebugLists();
|
---|
| 260 |
|
---|
| 261 | if (Content == null || interpreter == null)
|
---|
| 262 | return;
|
---|
| 263 |
|
---|
[15017] | 264 | foreach (var pair in debugPushProgramDict) {
|
---|
[14897] | 265 | var name = Enum.GetName(typeof(StackTypes), pair.Key);
|
---|
[15017] | 266 | var stack = (IPushStack<Expression>)interpreter.Stacks[pair.Key];
|
---|
[14897] | 267 |
|
---|
[15017] | 268 | pair.Value.AddExpressions(stack);
|
---|
| 269 | ((GroupBox)pair.Value.Parent).Text = string.Format(GROUP_BOX_TEXT_STRING_FORMAT, name, pair.Value.Nodes.Count);
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | foreach (var pair in debugListDict) {
|
---|
| 273 | var name = Enum.GetName(typeof(StackTypes), pair.Key);
|
---|
| 274 | var items = interpreter.StringifyStack(pair.Key).ToArray();
|
---|
| 275 |
|
---|
[14952] | 276 | pair.Value.Items.AddRange(items);
|
---|
| 277 | ((GroupBox)pair.Value.Parent).Text = string.Format(GROUP_BOX_TEXT_STRING_FORMAT, name, pair.Value.Items.Count);
|
---|
[14897] | 278 | }
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | }
|
---|
| 282 | }
|
---|