1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Globalization;
|
---|
4 | using System.Linq;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.DataPreprocessing {
|
---|
8 | public class DataGridLogic : IDataGridLogic {
|
---|
9 |
|
---|
10 | private IPreprocessingData preprocessingData;
|
---|
11 |
|
---|
12 | public DataGridLogic(IPreprocessingData preprocessingData) {
|
---|
13 | this.preprocessingData = preprocessingData;
|
---|
14 | }
|
---|
15 |
|
---|
16 | public int Rows {
|
---|
17 | get {
|
---|
18 | return preprocessingData.Rows;
|
---|
19 | }
|
---|
20 | }
|
---|
21 |
|
---|
22 | public int Columns {
|
---|
23 | get {
|
---|
24 | return preprocessingData.Columns;
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 | public IEnumerable<string> ColumnNames {
|
---|
29 | get {
|
---|
30 | return preprocessingData.VariableNames;
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | public string GetColumnTypeAsString(int columnIndex) {
|
---|
35 | if (preprocessingData.IsType<double>(columnIndex)) {
|
---|
36 | return "double";
|
---|
37 | } else if (preprocessingData.IsType<string>(columnIndex)) {
|
---|
38 | return "string";
|
---|
39 | } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
|
---|
40 | return "DateTime";
|
---|
41 | }
|
---|
42 | return "Unknown Type";
|
---|
43 | }
|
---|
44 |
|
---|
45 | public IEnumerable<string> RowNames {
|
---|
46 | get {
|
---|
47 | return Enumerable.Range(1, Rows).Select(n => n.ToString());
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public bool Validate(string value, out string errorMessage, int columnIndex) {
|
---|
52 | if (columnIndex < 0 || columnIndex > preprocessingData.VariableNames.Count()) {
|
---|
53 | throw new ArgumentOutOfRangeException("column index is out of range");
|
---|
54 | }
|
---|
55 | bool valid = false;
|
---|
56 | if (preprocessingData.IsType<double>(columnIndex)) {
|
---|
57 | double val;
|
---|
58 | valid = double.TryParse(value, out val);
|
---|
59 | errorMessage = string.Empty;
|
---|
60 | if (!valid) {
|
---|
61 | errorMessage = "Invalid Value (Valid Value Format: \"" + FormatPatterns.GetDoubleFormatPattern() + "\")";
|
---|
62 | }
|
---|
63 | } else if (preprocessingData.IsType<string>(columnIndex)) {
|
---|
64 | valid = value != null;
|
---|
65 | errorMessage = string.Empty;
|
---|
66 | if (!valid) {
|
---|
67 | errorMessage = "Invalid Value (string must not be null)";
|
---|
68 | }
|
---|
69 | } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
|
---|
70 | DateTime date;
|
---|
71 | valid = DateTime.TryParse(value, out date);
|
---|
72 | errorMessage = string.Empty;
|
---|
73 | if (!valid) {
|
---|
74 | errorMessage = "Invalid Value (Valid Value Format: \"" + CultureInfo.CurrentCulture.DateTimeFormat + "\"";
|
---|
75 | }
|
---|
76 | } else {
|
---|
77 | throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
|
---|
78 | }
|
---|
79 |
|
---|
80 | return valid;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public string GetValue(int rowIndex, int columnIndex) {
|
---|
84 | return preprocessingData.GetCellAsString(columnIndex, rowIndex);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public bool SetValue(string value, int rowIndex, int columnIndex) {
|
---|
88 | bool valid = false;
|
---|
89 | if (preprocessingData.IsType<double>(columnIndex)) {
|
---|
90 | double val;
|
---|
91 | valid = double.TryParse(value, out val);
|
---|
92 | if (valid) {
|
---|
93 | preprocessingData.SetCell<double>(columnIndex, rowIndex, val);
|
---|
94 | }
|
---|
95 | } else if (preprocessingData.IsType<string>(columnIndex)) {
|
---|
96 | valid = value != null;
|
---|
97 | if (valid) {
|
---|
98 | preprocessingData.SetCell<string>(columnIndex, rowIndex, value);
|
---|
99 | }
|
---|
100 | } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
|
---|
101 | DateTime date;
|
---|
102 | valid = DateTime.TryParse(value, out date);
|
---|
103 | if (valid) {
|
---|
104 | preprocessingData.SetCell<DateTime>(columnIndex, rowIndex, date);
|
---|
105 | }
|
---|
106 | } else {
|
---|
107 | throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
|
---|
108 | }
|
---|
109 |
|
---|
110 | return valid;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|