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