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;
|
---|
11 | using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
|
---|
12 | using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
|
---|
13 | using HeuristicLab.Problems.ProgramSynthesis.Push.Problem;
|
---|
14 | using HeuristicLab.Random;
|
---|
15 |
|
---|
16 | [View("Push Program Debugger")]
|
---|
17 | [Content(typeof(PushSolution), true)]
|
---|
18 | public partial class PushProgramDebuggerView : NamedItemView {
|
---|
19 | private const string Separator = ", ";
|
---|
20 | private PushProgram program;
|
---|
21 |
|
---|
22 | private ListViewItem[] execListCache; //array to cache items for the virtual list
|
---|
23 | private int firstExecListItemIndex; //stores the index of the first item in the cache
|
---|
24 |
|
---|
25 | private const string exampleSplitter = " => ";
|
---|
26 | private PushInterpreter interpreter;
|
---|
27 |
|
---|
28 | public PushProgramDebuggerView() {
|
---|
29 | InitializeComponent();
|
---|
30 |
|
---|
31 | Name = "Push Program Debugger";
|
---|
32 |
|
---|
33 | exampleComboBox.SelectedIndexChanged += SelectedExampleIndexChanged;
|
---|
34 | runButton.Click += RunButtonClick;
|
---|
35 | resetButton.Click += ResetButtonClick;
|
---|
36 | stepButton.Click += StepButtonClick;
|
---|
37 |
|
---|
38 | execList.RetrieveVirtualItem += ExecListRetrieveVirtualItem;
|
---|
39 | execList.CacheVirtualItems += ExecListCacheVirtualItems;
|
---|
40 |
|
---|
41 | execList.View = View.Details;
|
---|
42 | execList.HeaderStyle = ColumnHeaderStyle.Nonclickable;
|
---|
43 | execList.AutoResizeColumns(ColumnHeaderAutoResizeStyle.None);
|
---|
44 | execList.Columns.Add(new ColumnHeader { Width = execList.Width - 40 });
|
---|
45 | execList.VirtualMode = true;
|
---|
46 | execList.VirtualListSize = 100;
|
---|
47 | }
|
---|
48 |
|
---|
49 | private void ExecListCacheVirtualItems(object sender, CacheVirtualItemsEventArgs e) {
|
---|
50 | //We've gotten a request to refresh the cache.
|
---|
51 | //First check if it's really neccesary.
|
---|
52 | if (execListCache != null &&
|
---|
53 | e.StartIndex >= firstExecListItemIndex &&
|
---|
54 | e.EndIndex <= firstExecListItemIndex + execListCache.Length) {
|
---|
55 | //If the newly requested cache is a subset of the old cache,
|
---|
56 | //no need to rebuild everything, so do nothing.
|
---|
57 | return;
|
---|
58 | }
|
---|
59 |
|
---|
60 | //Now we need to rebuild the cache.
|
---|
61 | UpdateListCache(e.StartIndex, e.EndIndex);
|
---|
62 | }
|
---|
63 |
|
---|
64 | private void ExecListRetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e) {
|
---|
65 | //check to see if the requested item is currently in the cache
|
---|
66 | if (execListCache == null ||
|
---|
67 | e.ItemIndex < firstExecListItemIndex ||
|
---|
68 | e.ItemIndex >= firstExecListItemIndex + execListCache.Length) {
|
---|
69 | UpdateListCache(e.ItemIndex, execList.VirtualListSize);
|
---|
70 | }
|
---|
71 |
|
---|
72 | e.Item = e.ItemIndex >= firstExecListItemIndex && e.ItemIndex < firstExecListItemIndex + execListCache.Length
|
---|
73 | ? execListCache[e.ItemIndex - firstExecListItemIndex]
|
---|
74 | : e.Item = new ListViewItem();
|
---|
75 | }
|
---|
76 |
|
---|
77 | private void UpdateListCache(int startIndex, int endIndex) {
|
---|
78 | if (interpreter == null) {
|
---|
79 | execListCache = new ListViewItem[0];
|
---|
80 | return;
|
---|
81 | }
|
---|
82 |
|
---|
83 | this.firstExecListItemIndex = startIndex;
|
---|
84 | var length = Math.Min(endIndex - startIndex + 1, interpreter.ExecStack.Count); //indexes are inclusive
|
---|
85 | this.execListCache = new ListViewItem[length];
|
---|
86 |
|
---|
87 | var expressions = this.interpreter.ExecStack.Peek(length);
|
---|
88 |
|
---|
89 | //Fill the cache with the appropriate ListViewItems.
|
---|
90 | for (var i = 0; i < length; i++)
|
---|
91 | this.execListCache[i] = new ListViewItem(expressions[length - 1 - i].ToString());
|
---|
92 |
|
---|
93 | execList.AutoResizeColumns(ColumnHeaderAutoResizeStyle.None);
|
---|
94 | execList.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
|
---|
95 | }
|
---|
96 |
|
---|
97 | //protected override void OnEnabledChanged(EventArgs e) {
|
---|
98 | // SetReadonlyFields();
|
---|
99 | //}
|
---|
100 |
|
---|
101 | //protected override void OnReadOnlyChanged() {
|
---|
102 | // base.OnReadOnlyChanged();
|
---|
103 |
|
---|
104 | // SetReadonlyFields();
|
---|
105 | //}
|
---|
106 |
|
---|
107 | //protected override void SetEnabledStateOfControls()
|
---|
108 | //{
|
---|
109 | // SetReadonlyFields();
|
---|
110 | //}
|
---|
111 |
|
---|
112 | private void SetReadonlyFields() {
|
---|
113 | exampleComboBox.Enabled = !ReadOnly;
|
---|
114 | execList.Enabled = !ReadOnly;
|
---|
115 | codeList.Enabled = !ReadOnly;
|
---|
116 | nameList.Enabled = !ReadOnly;
|
---|
117 | integerList.Enabled = !ReadOnly;
|
---|
118 | floatList.Enabled = !ReadOnly;
|
---|
119 | booleanList.Enabled = !ReadOnly;
|
---|
120 | stepButton.Enabled = !ReadOnly;
|
---|
121 | runButton.Enabled = !ReadOnly;
|
---|
122 | resetButton.Enabled = !ReadOnly;
|
---|
123 | skipNoopsCheckBox.Enabled = !ReadOnly;
|
---|
124 | stepWidthBox.Enabled = !ReadOnly;
|
---|
125 | }
|
---|
126 |
|
---|
127 | private void RunButtonClick(object sender, EventArgs e) {
|
---|
128 | if (interpreter == null)
|
---|
129 | return;
|
---|
130 |
|
---|
131 | interpreter.Resume();
|
---|
132 |
|
---|
133 | UpdateExecList();
|
---|
134 | UpdateValueLists();
|
---|
135 | CheckIfButtonsCanBeEnabled();
|
---|
136 | }
|
---|
137 |
|
---|
138 | private void SyncExecList() {
|
---|
139 | UpdateExecList();
|
---|
140 |
|
---|
141 | this.execGroupBox.Text = string.Format("Exec [{0}]", this.interpreter.ExecStack.Count);
|
---|
142 | }
|
---|
143 |
|
---|
144 | private void StepButtonClick(object sender, EventArgs e) {
|
---|
145 | if (interpreter == null || stepWidthBox.Value <= 0)
|
---|
146 | return;
|
---|
147 |
|
---|
148 | var count = Math.Min(stepWidthBox.Value, interpreter.ExecStack.Count);
|
---|
149 |
|
---|
150 | for (var i = 0; i < count; i++) {
|
---|
151 | if (skipNoopsCheckBox.Checked) {
|
---|
152 | bool noop;
|
---|
153 |
|
---|
154 | do {
|
---|
155 | noop = !interpreter.Step();
|
---|
156 | }
|
---|
157 | while (interpreter.CanStep && noop);
|
---|
158 | } else interpreter.Step();
|
---|
159 | }
|
---|
160 |
|
---|
161 | stepWidthBox.Maximum = interpreter.ExecStack.Count;
|
---|
162 |
|
---|
163 | SyncExecList();
|
---|
164 | UpdateValueLists();
|
---|
165 | CheckIfButtonsCanBeEnabled();
|
---|
166 | }
|
---|
167 |
|
---|
168 | private void CheckIfButtonsCanBeEnabled() {
|
---|
169 | if (interpreter == null)
|
---|
170 | return;
|
---|
171 |
|
---|
172 | runButton.Enabled = interpreter.CanStep;
|
---|
173 | stepButton.Enabled = interpreter.CanStep;
|
---|
174 | stepWidthBox.Enabled = interpreter.CanStep;
|
---|
175 | }
|
---|
176 |
|
---|
177 | private void ResetButtonClick(object sender, System.EventArgs e) {
|
---|
178 | Reset();
|
---|
179 | }
|
---|
180 |
|
---|
181 | private void SelectedExampleIndexChanged(object sender, System.EventArgs e) {
|
---|
182 | Reset();
|
---|
183 | }
|
---|
184 |
|
---|
185 | public new PushSolution Content
|
---|
186 | {
|
---|
187 | get { return (PushSolution)base.Content; }
|
---|
188 | set
|
---|
189 | {
|
---|
190 | base.Content = value;
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | private void Reset() {
|
---|
195 | if (Content == null ||
|
---|
196 | interpreter == null ||
|
---|
197 | this.exampleComboBox.SelectedIndex < 0)
|
---|
198 | return;
|
---|
199 |
|
---|
200 | interpreter.Clear();
|
---|
201 |
|
---|
202 | var example = Content.DataDescriptor.Examples[this.exampleComboBox.SelectedIndex];
|
---|
203 |
|
---|
204 | interpreter.BooleanStack.Push(example.InputBoolean);
|
---|
205 | interpreter.IntegerStack.Push(example.InputInt);
|
---|
206 | interpreter.FloatStack.Push(example.InputFloat);
|
---|
207 |
|
---|
208 | interpreter.Run(program, true);
|
---|
209 | stepWidthBox.Maximum = interpreter.ExecStack.Count;
|
---|
210 |
|
---|
211 | UpdateValueLists();
|
---|
212 | UpdateExecList();
|
---|
213 | CheckIfButtonsCanBeEnabled();
|
---|
214 | }
|
---|
215 |
|
---|
216 | protected override void OnContentChanged() {
|
---|
217 | if (Content == null) return;
|
---|
218 |
|
---|
219 | var random = Content.Random ?? new FastRandom();
|
---|
220 | interpreter = new PushInterpreter(Content.Config, random);
|
---|
221 | program = Content.IntegerVector.MapToPushProgram(Content.Config.EnabledExpressions);
|
---|
222 |
|
---|
223 | UpdateExamples(Content.DataDescriptor);
|
---|
224 |
|
---|
225 | if (this.exampleComboBox.SelectedIndex < 0) {
|
---|
226 | this.exampleComboBox.SelectedIndex = 0; // Triggers Reset via event
|
---|
227 | } else {
|
---|
228 | Reset();
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | private void ClearLists() {
|
---|
233 | codeList.Items.Clear();
|
---|
234 | nameList.Items.Clear();
|
---|
235 | booleanList.Items.Clear();
|
---|
236 | integerList.Items.Clear();
|
---|
237 | floatList.Items.Clear();
|
---|
238 | }
|
---|
239 |
|
---|
240 | private void UpdateExecList() {
|
---|
241 | UpdateListCache(0, execList.VirtualListSize);
|
---|
242 | execList.Update();
|
---|
243 |
|
---|
244 | this.execGroupBox.Text = string.Format("Exec [{0}]", this.execList.Items.Count);
|
---|
245 | }
|
---|
246 |
|
---|
247 | private void UpdateValueLists() {
|
---|
248 | ClearLists();
|
---|
249 |
|
---|
250 | if (Content == null || interpreter == null)
|
---|
251 | return;
|
---|
252 |
|
---|
253 | if (interpreter.Configuration.IsCodeStackEnabled) {
|
---|
254 | this.codeList.Enabled = true;
|
---|
255 |
|
---|
256 | foreach (var item in interpreter.CodeStack.Reverse())
|
---|
257 | this.codeList.Items.Add(item);
|
---|
258 |
|
---|
259 | this.codeGroupBox.Text = string.Format("Code [{0}]", this.codeList.Items.Count);
|
---|
260 | } else {
|
---|
261 | this.codeList.Enabled = false;
|
---|
262 | this.codeGroupBox.Text = "Code - Disabled";
|
---|
263 | }
|
---|
264 |
|
---|
265 | if (interpreter.Configuration.IsIntegerStackEnabled) {
|
---|
266 | this.integerList.Enabled = true;
|
---|
267 |
|
---|
268 | foreach (var item in interpreter.IntegerStack.Reverse())
|
---|
269 | this.integerList.Items.Add(item);
|
---|
270 |
|
---|
271 | this.integerGroupBox.Text = string.Format("Integer [{0}]", this.integerList.Items.Count);
|
---|
272 | } else {
|
---|
273 | this.integerList.Enabled = false;
|
---|
274 | this.integerGroupBox.Text = "Integer - Disabled";
|
---|
275 | }
|
---|
276 |
|
---|
277 | if (interpreter.Configuration.IsFloatStackEnabled) {
|
---|
278 | this.floatList.Enabled = true;
|
---|
279 |
|
---|
280 | foreach (var item in interpreter.FloatStack.Reverse())
|
---|
281 | this.floatList.Items.Add(item);
|
---|
282 |
|
---|
283 | this.floatGroupBox.Text = string.Format("Float [{0}]", this.floatList.Items.Count);
|
---|
284 | } else {
|
---|
285 | this.floatList.Enabled = false;
|
---|
286 | this.floatGroupBox.Text = "Float - Disabled";
|
---|
287 | }
|
---|
288 |
|
---|
289 | if (interpreter.Configuration.IsBooleanStackEnabled) {
|
---|
290 | this.booleanList.Enabled = true;
|
---|
291 |
|
---|
292 | foreach (var item in interpreter.BooleanStack.Reverse())
|
---|
293 | this.booleanList.Items.Add(item);
|
---|
294 |
|
---|
295 | this.booleanGroupBox.Text = string.Format("Boolean [{0}]", this.booleanList.Items.Count);
|
---|
296 | } else {
|
---|
297 | this.booleanList.Enabled = false;
|
---|
298 | this.booleanGroupBox.Text = "Boolean - Disabled";
|
---|
299 | }
|
---|
300 |
|
---|
301 | if (interpreter.Configuration.IsNameStackEnabled) {
|
---|
302 | this.nameList.Enabled = true;
|
---|
303 |
|
---|
304 | foreach (var item in interpreter.NameStack.Reverse())
|
---|
305 | this.nameList.Items.Add(item);
|
---|
306 |
|
---|
307 | this.nameGroupBox.Text = string.Format("Name [{0}]", this.nameList.Items.Count);
|
---|
308 | } else {
|
---|
309 | this.nameList.Enabled = false;
|
---|
310 | this.nameGroupBox.Text = "Name - Disabled";
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | private void UpdateExamples(IBenchmarkSuiteDataDescriptor data) {
|
---|
315 | if (data == null) return;
|
---|
316 |
|
---|
317 | var stringRepresentations = data.Examples.Select(e =>
|
---|
318 | string.Join(Separator, e.InputArgs) +
|
---|
319 | exampleSplitter +
|
---|
320 | string.Join(Separator, e.OutputArgs));
|
---|
321 |
|
---|
322 | exampleComboBox.Items.Clear();
|
---|
323 | foreach (var str in stringRepresentations) {
|
---|
324 | exampleComboBox.Items.Add(str);
|
---|
325 | }
|
---|
326 | }
|
---|
327 | }
|
---|
328 | }
|
---|