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 IEnumerable<string> RowNames {
|
---|
35 | get {
|
---|
36 | return Enumerable.Range(1, Rows).Select(n => n.ToString());
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public bool Validate(string value, out string errorMessage, int columnIndex) {
|
---|
41 | if (columnIndex < 0 || columnIndex > preprocessingData.VariableNames.Count()) {
|
---|
42 | throw new ArgumentOutOfRangeException("column index is out of range");
|
---|
43 | }
|
---|
44 | bool valid = false;
|
---|
45 | if (preprocessingData.IsType<double>(columnIndex)) {
|
---|
46 | double val;
|
---|
47 | valid = double.TryParse(value, out val);
|
---|
48 | errorMessage = string.Empty;
|
---|
49 | if (!valid) {
|
---|
50 | errorMessage = "Invalid Value (Valid Value Format: \"" + FormatPatterns.GetDoubleFormatPattern() + "\")";
|
---|
51 | }
|
---|
52 | } else if (preprocessingData.IsType<string>(columnIndex)) {
|
---|
53 | valid = value != null;
|
---|
54 | errorMessage = string.Empty;
|
---|
55 | if (!valid) {
|
---|
56 | errorMessage = "Invalid Value (string must not be null)";
|
---|
57 | }
|
---|
58 | } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
|
---|
59 | DateTime date;
|
---|
60 | valid = DateTime.TryParse(value, out date);
|
---|
61 | errorMessage = string.Empty;
|
---|
62 | if (!valid) {
|
---|
63 | errorMessage = "Invalid Value (Valid Value Format: \"" + CultureInfo.CurrentCulture.DateTimeFormat + "\"";
|
---|
64 | }
|
---|
65 | } else {
|
---|
66 | throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
|
---|
67 | }
|
---|
68 |
|
---|
69 | return valid;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public string GetValue(int rowIndex, int columnIndex) {
|
---|
73 | return preprocessingData.GetCellAsString(columnIndex, rowIndex);
|
---|
74 | }
|
---|
75 |
|
---|
76 | public bool SetValue(string value, int rowIndex, int columnIndex) {
|
---|
77 | bool valid = false;
|
---|
78 | if (preprocessingData.IsType<double>(columnIndex)) {
|
---|
79 | double val;
|
---|
80 | valid = double.TryParse(value, out val);
|
---|
81 | if (valid) {
|
---|
82 | preprocessingData.SetCell<double>(columnIndex, rowIndex, val);
|
---|
83 | }
|
---|
84 | } else if (preprocessingData.IsType<string>(columnIndex)) {
|
---|
85 | valid = value != null;
|
---|
86 | if (valid) {
|
---|
87 | preprocessingData.SetCell<string>(columnIndex, rowIndex, value);
|
---|
88 | }
|
---|
89 | } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
|
---|
90 | DateTime date;
|
---|
91 | valid = DateTime.TryParse(value, out date);
|
---|
92 | if (valid) {
|
---|
93 | preprocessingData.SetCell<DateTime>(columnIndex, rowIndex, date);
|
---|
94 | }
|
---|
95 | } else {
|
---|
96 | throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
|
---|
97 | }
|
---|
98 |
|
---|
99 | return valid;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|