1 | using System.Windows.Forms;
|
---|
2 |
|
---|
3 | namespace HeuristicLab.BenchmarkSuite.Views {
|
---|
4 | using System.Collections.Generic;
|
---|
5 |
|
---|
6 | using HeuristicLab.Core.Views;
|
---|
7 | using HeuristicLab.MainForm;
|
---|
8 |
|
---|
9 | [View("Push Expression Selection Editor")]
|
---|
10 | [Content(typeof(ProblemData), true)]
|
---|
11 | public partial class DataEditorView : NamedItemView {
|
---|
12 | private const string ValueSeparator = ", ";
|
---|
13 |
|
---|
14 | public DataEditorView() {
|
---|
15 | InitializeComponent();
|
---|
16 |
|
---|
17 | dataGridView.CellEndEdit += DataGridViewCellEndEdit;
|
---|
18 | }
|
---|
19 |
|
---|
20 | private void DataGridViewCellEndEdit(object sender, DataGridViewCellEventArgs e) {
|
---|
21 | if (Content == null)
|
---|
22 | return;
|
---|
23 |
|
---|
24 | var row = dataGridView.Rows[e.RowIndex];
|
---|
25 |
|
---|
26 | var inputArgs = new string[Content.InputArgumentTypes.Length];
|
---|
27 | for (var i = 0; i < Content.InputArgumentTypes.Length; i++) {
|
---|
28 | inputArgs[i] = row.Cells[i].Value.ToString();
|
---|
29 | }
|
---|
30 |
|
---|
31 | var outputArgs = new string[Content.OutputArgumentTypes.Length];
|
---|
32 | for (var i = 0; i < Content.OutputArgumentTypes.Length; i++) {
|
---|
33 | outputArgs[i] = row.Cells[Content.InputArgumentTypes.Length + i].Value.ToString();
|
---|
34 | }
|
---|
35 |
|
---|
36 | var example = Content.Examples[e.RowIndex];
|
---|
37 |
|
---|
38 | try {
|
---|
39 | Content.Examples[e.RowIndex] = ParseExample(inputArgs, Content.InputArgumentTypes, outputArgs, Content.OutputArgumentTypes);
|
---|
40 | }
|
---|
41 | catch {
|
---|
42 | MessageBox.Show("Unable to parse example!");
|
---|
43 |
|
---|
44 | // reset entry
|
---|
45 | row.Cells[e.ColumnIndex].Value = e.ColumnIndex < Content.InputArgumentTypes.Length
|
---|
46 | ? example.InputArgs[e.ColumnIndex]
|
---|
47 | : example.OutputArgs[e.ColumnIndex - Content.InputArgumentTypes.Length];
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | private Example ParseExample(string[] inputArgs, ExampleArgumentType[] inputArgTypes, string[] outputArgs, ExampleArgumentType[] outputArgTypes) {
|
---|
52 | var example = new Example();
|
---|
53 | var booleanValues = new List<bool>();
|
---|
54 | var integerValues = new List<long>();
|
---|
55 | var floatValues = new List<double>();
|
---|
56 | var charValues = new List<char>();
|
---|
57 | var stringValues = new List<string>();
|
---|
58 |
|
---|
59 | example.InputArgs = inputArgs;
|
---|
60 | for (var i = 0; i < inputArgs.Length; i++) {
|
---|
61 | var arg = inputArgs[i];
|
---|
62 | var type = inputArgTypes[i];
|
---|
63 |
|
---|
64 | switch (type) {
|
---|
65 | case ExampleArgumentType.Boolean:
|
---|
66 | booleanValues.Add(ExampleArgumentConverter.ConvertBoolean(arg));
|
---|
67 | break;
|
---|
68 | case ExampleArgumentType.Integer:
|
---|
69 | integerValues.Add(ExampleArgumentConverter.ConvertInteger(arg));
|
---|
70 | break;
|
---|
71 | case ExampleArgumentType.Float:
|
---|
72 | floatValues.Add(ExampleArgumentConverter.ConvertDouble(arg));
|
---|
73 | break;
|
---|
74 | case ExampleArgumentType.Char:
|
---|
75 | charValues.Add(arg.Length == 1 ? arg[0] : default(char));
|
---|
76 | break;
|
---|
77 | case ExampleArgumentType.String:
|
---|
78 | stringValues.Add(arg);
|
---|
79 | break;
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | example.InputBoolean = booleanValues.ToArray();
|
---|
84 | example.InputInteger = integerValues.ToArray();
|
---|
85 | example.InputFloat = floatValues.ToArray();
|
---|
86 | example.InputChar = charValues.ToArray();
|
---|
87 | example.InputString = stringValues.ToArray();
|
---|
88 |
|
---|
89 | booleanValues.Clear();
|
---|
90 | integerValues.Clear();
|
---|
91 | floatValues.Clear();
|
---|
92 | charValues.Clear();
|
---|
93 | stringValues.Clear();
|
---|
94 |
|
---|
95 | example.OutputArgs = outputArgs;
|
---|
96 | for (var i = 0; i < outputArgs.Length; i++) {
|
---|
97 | var arg = outputArgs[i];
|
---|
98 | var type = outputArgTypes[i];
|
---|
99 |
|
---|
100 | switch (type) {
|
---|
101 | case ExampleArgumentType.Boolean:
|
---|
102 | booleanValues.Add(ExampleArgumentConverter.ConvertBoolean(arg));
|
---|
103 | break;
|
---|
104 | case ExampleArgumentType.Integer:
|
---|
105 | integerValues.Add(ExampleArgumentConverter.ConvertInteger(arg));
|
---|
106 | break;
|
---|
107 | case ExampleArgumentType.Float:
|
---|
108 | floatValues.Add(ExampleArgumentConverter.ConvertDouble(arg));
|
---|
109 | break;
|
---|
110 | case ExampleArgumentType.Char:
|
---|
111 | charValues.Add(arg.Length == 1 ? arg[0] : default(char));
|
---|
112 | break;
|
---|
113 | case ExampleArgumentType.String:
|
---|
114 | stringValues.Add(arg);
|
---|
115 | break;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | example.OutputBoolean = booleanValues.ToArray();
|
---|
120 | example.OutputInteger = integerValues.ToArray();
|
---|
121 | example.OutputFloat = floatValues.ToArray();
|
---|
122 | example.OutputChar = charValues.ToArray();
|
---|
123 | example.OutputString = stringValues.ToArray();
|
---|
124 |
|
---|
125 | return example;
|
---|
126 | }
|
---|
127 |
|
---|
128 | public new ProblemData Content
|
---|
129 | {
|
---|
130 | get { return (ProblemData)base.Content; }
|
---|
131 | set
|
---|
132 | {
|
---|
133 | base.Content = value;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | protected override void OnReadOnlyChanged() {
|
---|
138 | base.OnReadOnlyChanged();
|
---|
139 |
|
---|
140 | dataGridView.Enabled = !ReadOnly;
|
---|
141 | }
|
---|
142 |
|
---|
143 | protected override void OnContentChanged() {
|
---|
144 | dataGridView.Columns.Clear();
|
---|
145 | dataGridView.Rows.Clear();
|
---|
146 |
|
---|
147 | if (Content == null) {
|
---|
148 | return;
|
---|
149 | }
|
---|
150 |
|
---|
151 | nameTextBox.Text = Content.Name;
|
---|
152 |
|
---|
153 | var cellTemplate = new DataGridViewTextBoxCell { Style = { WrapMode = DataGridViewTriState.True } };
|
---|
154 |
|
---|
155 | for (var i = 0; i < Content.InputArgumentTypes.Length; i++) {
|
---|
156 | var headerTypeName = ViewHelper.GetHeaderTypeName(Content.InputArgumentTypes[i]);
|
---|
157 | var column = new DataGridViewColumn {
|
---|
158 | HeaderText = string.Format("Input {0} : {1}", i + 1, headerTypeName),
|
---|
159 | AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
---|
160 | CellTemplate = cellTemplate
|
---|
161 | };
|
---|
162 |
|
---|
163 | dataGridView.Columns.Add(column);
|
---|
164 | }
|
---|
165 |
|
---|
166 | for (var i = 0; i < Content.OutputArgumentTypes.Length; i++) {
|
---|
167 | var headerTypeName = ViewHelper.GetHeaderTypeName(Content.OutputArgumentTypes[i]);
|
---|
168 | var column = new DataGridViewColumn {
|
---|
169 | HeaderText = string.Format("Output {0} : {1}", i + 1, headerTypeName),
|
---|
170 | AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
---|
171 | CellTemplate = cellTemplate
|
---|
172 | };
|
---|
173 |
|
---|
174 | dataGridView.Columns.Add(column);
|
---|
175 | }
|
---|
176 |
|
---|
177 | for (var rowIdx = 0; rowIdx < Content.Examples.Length; rowIdx++) {
|
---|
178 | var example = Content.Examples[rowIdx];
|
---|
179 | var row = new DataGridViewRow {
|
---|
180 | HeaderCell = {
|
---|
181 | Value = (rowIdx + 1).ToString(),
|
---|
182 | }
|
---|
183 | };
|
---|
184 | row.CreateCells(dataGridView);
|
---|
185 |
|
---|
186 | var inputArgumentCountDict = ViewHelper.CreateArgumentCountDict();
|
---|
187 | for (var i = 0; i < Content.InputArgumentTypes.Length; i++) {
|
---|
188 | var type = Content.InputArgumentTypes[i];
|
---|
189 | var offset = inputArgumentCountDict[type];
|
---|
190 | row.Cells[i].Value = ViewHelper.StringifyInput(type, offset, "R", example, ValueSeparator);
|
---|
191 | inputArgumentCountDict[type]++;
|
---|
192 | }
|
---|
193 |
|
---|
194 | var outputArgumentCountDict = ViewHelper.CreateArgumentCountDict();
|
---|
195 | for (var i = 0; i < Content.OutputArgumentTypes.Length; i++) {
|
---|
196 | var type = Content.OutputArgumentTypes[i];
|
---|
197 | var offset = outputArgumentCountDict[type];
|
---|
198 | row.Cells[Content.InputArgumentTypes.Length + i].Value = ViewHelper.StringifyOutput(type, offset, "R", example, ValueSeparator);
|
---|
199 | outputArgumentCountDict[type]++;
|
---|
200 | }
|
---|
201 |
|
---|
202 | dataGridView.Rows.Add(row);
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|