[10239] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
[10246] | 3 | using System.Globalization;
|
---|
[10239] | 4 | using System.Linq;
|
---|
[10246] | 5 | using HeuristicLab.Data;
|
---|
[10239] | 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 |
|
---|
[10246] | 16 | public int Rows {
|
---|
| 17 | get {
|
---|
| 18 | return preprocessingData.Rows;
|
---|
| 19 | }
|
---|
[10239] | 20 | }
|
---|
| 21 |
|
---|
[10246] | 22 | public int Columns {
|
---|
| 23 | get {
|
---|
| 24 | return preprocessingData.Columns;
|
---|
| 25 | }
|
---|
| 26 | }
|
---|
[10239] | 27 |
|
---|
[10246] | 28 | public IEnumerable<string> ColumnNames {
|
---|
| 29 | get {
|
---|
| 30 | return preprocessingData.VariableNames;
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
[10239] | 33 |
|
---|
[10246] | 34 | public IEnumerable<string> RowNames {
|
---|
| 35 | get {
|
---|
| 36 | return Enumerable.Range(1, Rows).Select(n => n.ToString());
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
[10239] | 39 |
|
---|
[10246] | 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");
|
---|
[10239] | 43 | }
|
---|
[10246] | 44 | bool valid = false;
|
---|
| 45 | string variableName = preprocessingData.GetVariableName(columnIndex);
|
---|
| 46 | if (preprocessingData.IsType<double>(variableName)) {
|
---|
| 47 | double val;
|
---|
| 48 | valid = double.TryParse(value, out val);
|
---|
| 49 | errorMessage = string.Empty;
|
---|
| 50 | if (!valid) {
|
---|
| 51 | errorMessage = "Invalid Value (Valid Value Format: \"" + FormatPatterns.GetDoubleFormatPattern() + "\")";
|
---|
| 52 | }
|
---|
| 53 | } else if (preprocessingData.IsType<string>(variableName)) {
|
---|
| 54 | valid = value != null;
|
---|
| 55 | errorMessage = string.Empty;
|
---|
| 56 | if (!valid) {
|
---|
| 57 | errorMessage = "Invalid Value (string must not be null)";
|
---|
| 58 | }
|
---|
| 59 | } else if (preprocessingData.IsType<DateTime>(variableName)) {
|
---|
| 60 | DateTime date;
|
---|
| 61 | valid = DateTime.TryParse(value, out date);
|
---|
| 62 | errorMessage = string.Empty;
|
---|
| 63 | if (!valid) {
|
---|
| 64 | errorMessage = "Invalid Value (Valid Value Format: \"" + CultureInfo.CurrentCulture.DateTimeFormat + "\"";
|
---|
| 65 | }
|
---|
| 66 | } else {
|
---|
| 67 | throw new ArgumentException("column with variableName: " + variableName + " contains a non supported type.");
|
---|
| 68 | }
|
---|
[10239] | 69 |
|
---|
[10246] | 70 | return valid;
|
---|
| 71 | }
|
---|
[10239] | 72 |
|
---|
[10246] | 73 | public string GetValue(int rowIndex, int columnIndex) {
|
---|
| 74 | return preprocessingData.GetCellAsString(preprocessingData.GetVariableName(columnIndex), rowIndex);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | public bool SetValue(string value, int rowIndex, int columnIndex) {
|
---|
| 78 | string variableName = preprocessingData.GetVariableName(columnIndex);
|
---|
| 79 | bool valid = false;
|
---|
| 80 | if (preprocessingData.IsType<double>(variableName)) {
|
---|
| 81 | double val;
|
---|
| 82 | valid = double.TryParse(value, out val);
|
---|
| 83 | if (valid) {
|
---|
| 84 | preprocessingData.SetCell<double>(variableName, rowIndex, val);
|
---|
[10239] | 85 | }
|
---|
[10246] | 86 | } else if (preprocessingData.IsType<string>(variableName)) {
|
---|
| 87 | valid = value != null;
|
---|
| 88 | if (valid) {
|
---|
| 89 | preprocessingData.SetCell<string>(variableName, rowIndex, value);
|
---|
| 90 | }
|
---|
| 91 | } else if (preprocessingData.IsType<DateTime>(variableName)) {
|
---|
| 92 | DateTime date;
|
---|
| 93 | valid = DateTime.TryParse(value, out date);
|
---|
| 94 | if (valid) {
|
---|
| 95 | preprocessingData.SetCell<DateTime>(variableName, rowIndex, date);
|
---|
| 96 | }
|
---|
| 97 | } else {
|
---|
| 98 | throw new ArgumentException("column with variableName: " + variableName + " contains a non supported type.");
|
---|
[10239] | 99 | }
|
---|
[10246] | 100 |
|
---|
| 101 | return valid;
|
---|
[10239] | 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|