1 | using System.Windows.Forms;
|
---|
2 |
|
---|
3 | namespace HeuristicLab.BenchmarkSuite.Views {
|
---|
4 |
|
---|
5 | using HeuristicLab.BenchmarkSuite.Problems;
|
---|
6 | using HeuristicLab.Core.Views;
|
---|
7 | using HeuristicLab.MainForm;
|
---|
8 |
|
---|
9 | [View("Push Expression Selection Editor")]
|
---|
10 | [Content(typeof(Data), 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 |
|
---|
54 | example.InputArgs = new string[inputArgs.Length];
|
---|
55 | for (var i = 0; i < inputArgs.Length; i++) {
|
---|
56 | var arg = inputArgs[i];
|
---|
57 | var type = inputArgTypes[i];
|
---|
58 |
|
---|
59 | switch (type) {
|
---|
60 | case ExampleArgumentType.Bool:
|
---|
61 | example.InputBoolean = new[] { ExampleArgumentConverter.ConvertBoolean(arg) };
|
---|
62 | example.InputArgs[i] = arg;
|
---|
63 | break;
|
---|
64 | case ExampleArgumentType.Integer:
|
---|
65 | example.InputInt = new[] { ExampleArgumentConverter.ConvertInteger(arg) };
|
---|
66 | example.InputArgs[i] = arg;
|
---|
67 | break;
|
---|
68 | case ExampleArgumentType.IntegerCollection:
|
---|
69 | example.InputInt = ExampleArgumentConverter.ConvertIntegers(arg);
|
---|
70 | example.InputArgs[i] = string.Join(ValueSeparator, example.InputInt);
|
---|
71 | break;
|
---|
72 | case ExampleArgumentType.Float:
|
---|
73 | example.InputFloat = new[] { ExampleArgumentConverter.ConvertDouble(arg) };
|
---|
74 | example.InputArgs[i] = arg;
|
---|
75 | break;
|
---|
76 | case ExampleArgumentType.FloatCollection:
|
---|
77 | example.InputFloat = ExampleArgumentConverter.ConvertDoubles(arg);
|
---|
78 | example.InputArgs[i] = string.Join(ValueSeparator, example.InputFloat);
|
---|
79 | break;
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | example.OutputArgs = new string[outputArgs.Length];
|
---|
84 | for (var i = 0; i < outputArgs.Length; i++) {
|
---|
85 | var arg = outputArgs[i];
|
---|
86 | var type = outputArgTypes[i];
|
---|
87 |
|
---|
88 | switch (type) {
|
---|
89 | case ExampleArgumentType.Bool:
|
---|
90 | example.OutputBoolean = new[] { ExampleArgumentConverter.ConvertBoolean(arg) };
|
---|
91 | example.OutputArgs[i] = arg;
|
---|
92 | break;
|
---|
93 | case ExampleArgumentType.Integer:
|
---|
94 | example.OutputInt = new[] { ExampleArgumentConverter.ConvertInteger(arg) };
|
---|
95 | example.OutputArgs[i] = arg;
|
---|
96 | break;
|
---|
97 | case ExampleArgumentType.IntegerCollection:
|
---|
98 | example.OutputInt = ExampleArgumentConverter.ConvertIntegers(arg);
|
---|
99 | example.OutputArgs[i] = string.Join(ValueSeparator, example.OutputInt);
|
---|
100 | break;
|
---|
101 | case ExampleArgumentType.Float:
|
---|
102 | example.OutputFloat = new[] { ExampleArgumentConverter.ConvertDouble(arg) };
|
---|
103 | example.OutputArgs[i] = arg;
|
---|
104 | break;
|
---|
105 | case ExampleArgumentType.FloatCollection:
|
---|
106 | example.OutputFloat = ExampleArgumentConverter.ConvertDoubles(arg);
|
---|
107 | example.OutputArgs[i] = string.Join(ValueSeparator, example.OutputFloat);
|
---|
108 | break;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | return example;
|
---|
113 | }
|
---|
114 |
|
---|
115 | public new Data Content
|
---|
116 | {
|
---|
117 | get { return (Data)base.Content; }
|
---|
118 | set
|
---|
119 | {
|
---|
120 | base.Content = value;
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | protected override void OnReadOnlyChanged() {
|
---|
125 | base.OnReadOnlyChanged();
|
---|
126 |
|
---|
127 | dataGridView.Enabled = !ReadOnly;
|
---|
128 | }
|
---|
129 |
|
---|
130 | protected override void OnContentChanged() {
|
---|
131 | dataGridView.Columns.Clear();
|
---|
132 | dataGridView.Rows.Clear();
|
---|
133 |
|
---|
134 | if (Content == null) {
|
---|
135 | return;
|
---|
136 | }
|
---|
137 |
|
---|
138 | this.nameTextBox.Text = Content.Name;
|
---|
139 |
|
---|
140 | var cellTemplate = new DataGridViewTextBoxCell();
|
---|
141 |
|
---|
142 | for (var i = 0; i < Content.InputArgumentTypes.Length; i++) {
|
---|
143 | var headerTypeName = ViewHelper.GetHeaderTypeName(Content.InputArgumentTypes[i]);
|
---|
144 | var column = new DataGridViewColumn {
|
---|
145 | HeaderText = string.Format("Input {0} : {1}", i + 1, headerTypeName),
|
---|
146 | AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
---|
147 | CellTemplate = cellTemplate
|
---|
148 | };
|
---|
149 |
|
---|
150 | dataGridView.Columns.Add(column);
|
---|
151 | }
|
---|
152 |
|
---|
153 | for (var i = 0; i < Content.OutputArgumentTypes.Length; i++) {
|
---|
154 | var headerTypeName = ViewHelper.GetHeaderTypeName(Content.OutputArgumentTypes[i]);
|
---|
155 | var column = new DataGridViewColumn {
|
---|
156 | HeaderText = string.Format("Output {0} : {1}", i + 1, headerTypeName),
|
---|
157 | AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
---|
158 | CellTemplate = cellTemplate
|
---|
159 | };
|
---|
160 |
|
---|
161 | dataGridView.Columns.Add(column);
|
---|
162 | }
|
---|
163 |
|
---|
164 | foreach (var example in Content.Examples) {
|
---|
165 | var row = new DataGridViewRow();
|
---|
166 | row.HeaderCell.Value = row.Index + 1;
|
---|
167 |
|
---|
168 | row.CreateCells(dataGridView);
|
---|
169 |
|
---|
170 | for (var i = 0; i < Content.InputArgumentTypes.Length; i++) {
|
---|
171 | row.Cells[i].Value = ViewHelper.StringifyInput(Content.InputArgumentTypes[i], example, ValueSeparator);
|
---|
172 | }
|
---|
173 |
|
---|
174 | for (var i = 0; i < Content.OutputArgumentTypes.Length; i++) {
|
---|
175 | row.Cells[Content.InputArgumentTypes.Length + i].Value = ViewHelper.StringifyOutput(Content.OutputArgumentTypes[i], example, ValueSeparator);
|
---|
176 | }
|
---|
177 |
|
---|
178 | dataGridView.Rows.Add(row);
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|