1 | using System.Windows.Forms;
|
---|
2 |
|
---|
3 | using HeuristicLab.Core.Views;
|
---|
4 | using HeuristicLab.MainForm;
|
---|
5 |
|
---|
6 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Views {
|
---|
7 | using System;
|
---|
8 | using System.Linq;
|
---|
9 |
|
---|
10 | using HeuristicLab.BenchmarkSuite.Problems;
|
---|
11 | using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
|
---|
12 | using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
|
---|
13 | using HeuristicLab.Problems.ProgramSynthesis.Push.Problem;
|
---|
14 |
|
---|
15 | [View("Push Program Debugger")]
|
---|
16 | [Content(typeof(PushSolution), true)]
|
---|
17 | public partial class PushProgramDebuggerView : NamedItemView {
|
---|
18 | private const string exampleSplitter = " => ";
|
---|
19 | private PushGpInterpreter interpreter;
|
---|
20 |
|
---|
21 | public PushProgramDebuggerView() {
|
---|
22 | InitializeComponent();
|
---|
23 |
|
---|
24 | Name = "Push Program Debugger";
|
---|
25 |
|
---|
26 | exampleComboBox.SelectedIndexChanged += SelectedExampleIndexChanged;
|
---|
27 | runButton.Click += RunButtonClick;
|
---|
28 | resetButton.Click += ResetButtonClick;
|
---|
29 | stepButton.Click += StepButtonClick;
|
---|
30 | }
|
---|
31 |
|
---|
32 | protected override void OnReadOnlyChanged() {
|
---|
33 | base.OnReadOnlyChanged();
|
---|
34 |
|
---|
35 | exampleComboBox.Enabled = !ReadOnly;
|
---|
36 | execList.Enabled = !ReadOnly;
|
---|
37 | codeList.Enabled = !ReadOnly;
|
---|
38 | nameList.Enabled = !ReadOnly;
|
---|
39 | integerList.Enabled = !ReadOnly;
|
---|
40 | floatList.Enabled = !ReadOnly;
|
---|
41 | booleanList.Enabled = !ReadOnly;
|
---|
42 | }
|
---|
43 |
|
---|
44 | private void RunButtonClick(object sender, EventArgs e) {
|
---|
45 | if (interpreter == null)
|
---|
46 | return;
|
---|
47 |
|
---|
48 | interpreter.Resume();
|
---|
49 |
|
---|
50 | UpdateExecList();
|
---|
51 | UpdateValueLists();
|
---|
52 | CheckIfButtonsCanBeEnabled();
|
---|
53 | }
|
---|
54 |
|
---|
55 | private void SyncExecList() {
|
---|
56 | execList.Items.RemoveAt(0);
|
---|
57 |
|
---|
58 | if (!interpreter.ExecStack.SequenceEqual(execList.Items.Cast<Expression>().Reverse())) {
|
---|
59 | UpdateExecList();
|
---|
60 | }
|
---|
61 |
|
---|
62 | this.execGroupBox.Text = string.Format("Exec [{0}]", this.execList.Items.Count);
|
---|
63 | }
|
---|
64 |
|
---|
65 | private void StepButtonClick(object sender, EventArgs e) {
|
---|
66 | if (interpreter == null || stepWidthBox.Value <= 0)
|
---|
67 | return;
|
---|
68 |
|
---|
69 | var count = Math.Min(stepWidthBox.Value, interpreter.ExecStack.Count);
|
---|
70 |
|
---|
71 | for (var i = 0; i < count; i++) {
|
---|
72 | interpreter.Step();
|
---|
73 | }
|
---|
74 |
|
---|
75 | stepWidthBox.Maximum = interpreter.ExecStack.Count;
|
---|
76 |
|
---|
77 | SyncExecList();
|
---|
78 | UpdateValueLists();
|
---|
79 | CheckIfButtonsCanBeEnabled();
|
---|
80 | }
|
---|
81 |
|
---|
82 | private void CheckIfButtonsCanBeEnabled() {
|
---|
83 | if (interpreter == null)
|
---|
84 | return;
|
---|
85 |
|
---|
86 | runButton.Enabled = interpreter.CanStep;
|
---|
87 | stepButton.Enabled = interpreter.CanStep;
|
---|
88 | stepWidthBox.Enabled = interpreter.CanStep;
|
---|
89 | }
|
---|
90 |
|
---|
91 | private void ResetButtonClick(object sender, System.EventArgs e) {
|
---|
92 | Reset();
|
---|
93 | }
|
---|
94 |
|
---|
95 | private void SelectedExampleIndexChanged(object sender, System.EventArgs e) {
|
---|
96 | Reset();
|
---|
97 | }
|
---|
98 |
|
---|
99 | public new PushSolution Content
|
---|
100 | {
|
---|
101 | get { return (PushSolution)base.Content; }
|
---|
102 | set
|
---|
103 | {
|
---|
104 | base.Content = value;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | private void Reset() {
|
---|
109 | if (Content == null ||
|
---|
110 | interpreter == null ||
|
---|
111 | this.exampleComboBox.SelectedIndex < 0)
|
---|
112 | return;
|
---|
113 |
|
---|
114 | interpreter.Clear();
|
---|
115 |
|
---|
116 | var example = Content.Data.Examples[this.exampleComboBox.SelectedIndex];
|
---|
117 |
|
---|
118 | interpreter.BooleanStack.Push(example.InputBoolean);
|
---|
119 | interpreter.IntegerStack.Push(example.InputInt);
|
---|
120 | interpreter.FloatStack.Push(example.InputFloat);
|
---|
121 |
|
---|
122 | interpreter.Run(Content.Program, true);
|
---|
123 | stepWidthBox.Maximum = interpreter.ExecStack.Count;
|
---|
124 |
|
---|
125 | UpdateValueLists();
|
---|
126 | UpdateExecList();
|
---|
127 | CheckIfButtonsCanBeEnabled();
|
---|
128 | }
|
---|
129 |
|
---|
130 | protected override void OnContentChanged() {
|
---|
131 | if (Content == null) return;
|
---|
132 |
|
---|
133 | interpreter = Content.Pool.GetInstance(Content.Random);
|
---|
134 |
|
---|
135 | UpdateExamples(Content.Data);
|
---|
136 |
|
---|
137 | if (this.exampleComboBox.SelectedIndex < 0) {
|
---|
138 | this.exampleComboBox.SelectedIndex = 0; // Triggers Reset via event
|
---|
139 | } else {
|
---|
140 | Reset();
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | private void ClearLists() {
|
---|
145 | codeList.Items.Clear();
|
---|
146 | nameList.Items.Clear();
|
---|
147 | booleanList.Items.Clear();
|
---|
148 | integerList.Items.Clear();
|
---|
149 | floatList.Items.Clear();
|
---|
150 | }
|
---|
151 |
|
---|
152 | private void UpdateExecList() {
|
---|
153 | execList.Items.Clear();
|
---|
154 |
|
---|
155 | foreach (var item in interpreter.ExecStack.Reverse())
|
---|
156 | this.execList.Items.Add(item);
|
---|
157 |
|
---|
158 | this.execGroupBox.Text = string.Format("Exec [{0}]", this.execList.Items.Count);
|
---|
159 | }
|
---|
160 |
|
---|
161 | private void UpdateValueLists() {
|
---|
162 | ClearLists();
|
---|
163 |
|
---|
164 | if (Content == null || interpreter == null)
|
---|
165 | return;
|
---|
166 |
|
---|
167 | if (interpreter.Configuration.IsCodeStackEnabled) {
|
---|
168 | this.codeList.Enabled = true;
|
---|
169 |
|
---|
170 | foreach (var item in interpreter.CodeStack.Reverse())
|
---|
171 | this.codeList.Items.Add(item);
|
---|
172 |
|
---|
173 | this.codeGroupBox.Text = string.Format("Code [{0}]", this.codeList.Items.Count);
|
---|
174 | } else {
|
---|
175 | this.codeList.Enabled = false;
|
---|
176 | this.codeGroupBox.Text = "Code - Disabled";
|
---|
177 | }
|
---|
178 |
|
---|
179 | if (interpreter.Configuration.IsIntegerStackEnabled) {
|
---|
180 | this.integerList.Enabled = true;
|
---|
181 |
|
---|
182 | foreach (var item in interpreter.IntegerStack.Reverse())
|
---|
183 | this.integerList.Items.Add(item);
|
---|
184 |
|
---|
185 | this.integerGroupBox.Text = string.Format("Integer [{0}]", this.integerList.Items.Count);
|
---|
186 | } else {
|
---|
187 | this.integerList.Enabled = false;
|
---|
188 | this.integerGroupBox.Text = "Integer - Disabled";
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (interpreter.Configuration.IsFloatStackEnabled) {
|
---|
192 | this.floatList.Enabled = true;
|
---|
193 |
|
---|
194 | foreach (var item in interpreter.FloatStack.Reverse())
|
---|
195 | this.floatList.Items.Add(item);
|
---|
196 |
|
---|
197 | this.floatGroupBox.Text = string.Format("Float [{0}]", this.floatList.Items.Count);
|
---|
198 | } else {
|
---|
199 | this.floatList.Enabled = false;
|
---|
200 | this.floatGroupBox.Text = "Float - Disabled";
|
---|
201 | }
|
---|
202 |
|
---|
203 | if (interpreter.Configuration.IsBooleanStackEnabled) {
|
---|
204 | this.booleanList.Enabled = true;
|
---|
205 |
|
---|
206 | foreach (var item in interpreter.BooleanStack.Reverse())
|
---|
207 | this.booleanList.Items.Add(item);
|
---|
208 |
|
---|
209 | this.booleanGroupBox.Text = string.Format("Boolean [{0}]", this.booleanList.Items.Count);
|
---|
210 | } else {
|
---|
211 | this.booleanList.Enabled = false;
|
---|
212 | this.booleanGroupBox.Text = "Boolean - Disabled";
|
---|
213 | }
|
---|
214 |
|
---|
215 | if (interpreter.Configuration.IsNameStackEnabled) {
|
---|
216 | this.nameList.Enabled = true;
|
---|
217 |
|
---|
218 | foreach (var item in interpreter.NameStack.Reverse())
|
---|
219 | this.nameList.Items.Add(item);
|
---|
220 |
|
---|
221 | this.nameGroupBox.Text = string.Format("Name [{0}]", this.nameList.Items.Count);
|
---|
222 | } else {
|
---|
223 | this.nameList.Enabled = false;
|
---|
224 | this.nameGroupBox.Text = "Name - Disabled";
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | private void UpdateExamples(Data data) {
|
---|
229 | const string seperator = ", ";
|
---|
230 | var stringRepresentations = data.Examples.Select(e =>
|
---|
231 | string.Join(seperator, e.InputArgs) +
|
---|
232 | exampleSplitter +
|
---|
233 | string.Join(seperator, e.OutputArgs));
|
---|
234 |
|
---|
235 | exampleComboBox.Items.Clear();
|
---|
236 | foreach (var str in stringRepresentations) {
|
---|
237 | exampleComboBox.Items.Add(str);
|
---|
238 | }
|
---|
239 | }
|
---|
240 | }
|
---|
241 | }
|
---|