[14727] | 1 | using System.Windows.Forms;
|
---|
| 2 |
|
---|
| 3 | namespace HeuristicLab.BenchmarkSuite.Views {
|
---|
[14897] | 4 | using System.Collections.Generic;
|
---|
[14777] | 5 |
|
---|
[14727] | 6 | using HeuristicLab.Core.Views;
|
---|
| 7 | using HeuristicLab.MainForm;
|
---|
| 8 |
|
---|
| 9 | [View("Push Expression Selection Editor")]
|
---|
[14875] | 10 | [Content(typeof(ProblemData), true)]
|
---|
[14727] | 11 | public partial class DataEditorView : NamedItemView {
|
---|
[14777] | 12 | private const string ValueSeparator = ", ";
|
---|
| 13 |
|
---|
[14727] | 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 |
|
---|
[14777] | 26 | var inputArgs = new string[Content.InputArgumentTypes.Length];
|
---|
| 27 | for (var i = 0; i < Content.InputArgumentTypes.Length; i++) {
|
---|
[14727] | 28 | inputArgs[i] = row.Cells[i].Value.ToString();
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[14777] | 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();
|
---|
[14727] | 34 | }
|
---|
| 35 |
|
---|
| 36 | var example = Content.Examples[e.RowIndex];
|
---|
| 37 |
|
---|
| 38 | try {
|
---|
[14834] | 39 | Content.Examples[e.RowIndex] = ParseExample(inputArgs, Content.InputArgumentTypes, outputArgs, Content.OutputArgumentTypes);
|
---|
[14727] | 40 | }
|
---|
| 41 | catch {
|
---|
| 42 | MessageBox.Show("Unable to parse example!");
|
---|
| 43 |
|
---|
| 44 | // reset entry
|
---|
[14777] | 45 | row.Cells[e.ColumnIndex].Value = e.ColumnIndex < Content.InputArgumentTypes.Length
|
---|
[14727] | 46 | ? example.InputArgs[e.ColumnIndex]
|
---|
[14777] | 47 | : example.OutputArgs[e.ColumnIndex - Content.InputArgumentTypes.Length];
|
---|
[14727] | 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[14777] | 51 | private Example ParseExample(string[] inputArgs, ExampleArgumentType[] inputArgTypes, string[] outputArgs, ExampleArgumentType[] outputArgTypes) {
|
---|
| 52 | var example = new Example();
|
---|
[14897] | 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>();
|
---|
[14777] | 58 |
|
---|
[14897] | 59 | example.InputArgs = inputArgs;
|
---|
[14777] | 60 | for (var i = 0; i < inputArgs.Length; i++) {
|
---|
| 61 | var arg = inputArgs[i];
|
---|
| 62 | var type = inputArgTypes[i];
|
---|
| 63 |
|
---|
| 64 | switch (type) {
|
---|
[14875] | 65 | case ExampleArgumentType.Boolean:
|
---|
[14897] | 66 | booleanValues.Add(ExampleArgumentConverter.ConvertBoolean(arg));
|
---|
[14777] | 67 | break;
|
---|
| 68 | case ExampleArgumentType.Integer:
|
---|
[14897] | 69 | integerValues.Add(ExampleArgumentConverter.ConvertInteger(arg));
|
---|
[14777] | 70 | break;
|
---|
| 71 | case ExampleArgumentType.Float:
|
---|
[14897] | 72 | floatValues.Add(ExampleArgumentConverter.ConvertDouble(arg));
|
---|
[14777] | 73 | break;
|
---|
[14897] | 74 | case ExampleArgumentType.Char:
|
---|
| 75 | charValues.Add(arg.Length == 1 ? arg[0] : default(char));
|
---|
[14777] | 76 | break;
|
---|
[14897] | 77 | case ExampleArgumentType.String:
|
---|
| 78 | stringValues.Add(arg);
|
---|
| 79 | break;
|
---|
[14777] | 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[14897] | 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;
|
---|
[14777] | 96 | for (var i = 0; i < outputArgs.Length; i++) {
|
---|
| 97 | var arg = outputArgs[i];
|
---|
| 98 | var type = outputArgTypes[i];
|
---|
| 99 |
|
---|
| 100 | switch (type) {
|
---|
[14875] | 101 | case ExampleArgumentType.Boolean:
|
---|
[14897] | 102 | booleanValues.Add(ExampleArgumentConverter.ConvertBoolean(arg));
|
---|
[14777] | 103 | break;
|
---|
| 104 | case ExampleArgumentType.Integer:
|
---|
[14897] | 105 | integerValues.Add(ExampleArgumentConverter.ConvertInteger(arg));
|
---|
[14777] | 106 | break;
|
---|
| 107 | case ExampleArgumentType.Float:
|
---|
[14897] | 108 | floatValues.Add(ExampleArgumentConverter.ConvertDouble(arg));
|
---|
[14777] | 109 | break;
|
---|
[14897] | 110 | case ExampleArgumentType.Char:
|
---|
| 111 | charValues.Add(arg.Length == 1 ? arg[0] : default(char));
|
---|
[14777] | 112 | break;
|
---|
[14897] | 113 | case ExampleArgumentType.String:
|
---|
| 114 | stringValues.Add(arg);
|
---|
| 115 | break;
|
---|
[14777] | 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[14897] | 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 |
|
---|
[14777] | 125 | return example;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[14875] | 128 | public new ProblemData Content
|
---|
[14727] | 129 | {
|
---|
[14875] | 130 | get { return (ProblemData)base.Content; }
|
---|
[14727] | 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 |
|
---|
[14908] | 151 | nameTextBox.Text = Content.Name;
|
---|
[14777] | 152 |
|
---|
[14952] | 153 | var cellTemplate = new DataGridViewTextBoxCell { Style = { WrapMode = DataGridViewTriState.True } };
|
---|
[14727] | 154 |
|
---|
[14777] | 155 | for (var i = 0; i < Content.InputArgumentTypes.Length; i++) {
|
---|
| 156 | var headerTypeName = ViewHelper.GetHeaderTypeName(Content.InputArgumentTypes[i]);
|
---|
[14727] | 157 | var column = new DataGridViewColumn {
|
---|
[14777] | 158 | HeaderText = string.Format("Input {0} : {1}", i + 1, headerTypeName),
|
---|
[14727] | 159 | AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
---|
| 160 | CellTemplate = cellTemplate
|
---|
| 161 | };
|
---|
| 162 |
|
---|
| 163 | dataGridView.Columns.Add(column);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[14777] | 166 | for (var i = 0; i < Content.OutputArgumentTypes.Length; i++) {
|
---|
| 167 | var headerTypeName = ViewHelper.GetHeaderTypeName(Content.OutputArgumentTypes[i]);
|
---|
[14727] | 168 | var column = new DataGridViewColumn {
|
---|
[14777] | 169 | HeaderText = string.Format("Output {0} : {1}", i + 1, headerTypeName),
|
---|
[14727] | 170 | AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
|
---|
| 171 | CellTemplate = cellTemplate
|
---|
| 172 | };
|
---|
| 173 |
|
---|
| 174 | dataGridView.Columns.Add(column);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[14952] | 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 | };
|
---|
[14727] | 184 | row.CreateCells(dataGridView);
|
---|
| 185 |
|
---|
[14952] | 186 | var inputArgumentCountDict = ViewHelper.CreateArgumentCountDict();
|
---|
[14777] | 187 | for (var i = 0; i < Content.InputArgumentTypes.Length; i++) {
|
---|
[14952] | 188 | var type = Content.InputArgumentTypes[i];
|
---|
| 189 | var offset = inputArgumentCountDict[type];
|
---|
[15032] | 190 | row.Cells[i].Value = ViewHelper.StringifyInput(type, offset, "R", example, ValueSeparator);
|
---|
[14952] | 191 | inputArgumentCountDict[type]++;
|
---|
[14727] | 192 | }
|
---|
| 193 |
|
---|
[14952] | 194 | var outputArgumentCountDict = ViewHelper.CreateArgumentCountDict();
|
---|
[14777] | 195 | for (var i = 0; i < Content.OutputArgumentTypes.Length; i++) {
|
---|
[14952] | 196 | var type = Content.OutputArgumentTypes[i];
|
---|
| 197 | var offset = outputArgumentCountDict[type];
|
---|
[15032] | 198 | row.Cells[Content.InputArgumentTypes.Length + i].Value = ViewHelper.StringifyOutput(type, offset, "R", example, ValueSeparator);
|
---|
[14952] | 199 | outputArgumentCountDict[type]++;
|
---|
[14727] | 200 | }
|
---|
| 201 |
|
---|
| 202 | dataGridView.Rows.Add(row);
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
| 206 | }
|
---|