[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 |
|
---|
[10367] | 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 |
|
---|
[10246] | 45 | public IEnumerable<string> RowNames {
|
---|
| 46 | get {
|
---|
| 47 | return Enumerable.Range(1, Rows).Select(n => n.ToString());
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
[10239] | 50 |
|
---|
[10246] | 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");
|
---|
[10239] | 54 | }
|
---|
[10246] | 55 | bool valid = false;
|
---|
[10367] | 56 | if (preprocessingData.IsType<double>(columnIndex)) {
|
---|
[10246] | 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 | }
|
---|
[10367] | 63 | } else if (preprocessingData.IsType<string>(columnIndex)) {
|
---|
[10246] | 64 | valid = value != null;
|
---|
| 65 | errorMessage = string.Empty;
|
---|
| 66 | if (!valid) {
|
---|
| 67 | errorMessage = "Invalid Value (string must not be null)";
|
---|
| 68 | }
|
---|
[10367] | 69 | } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
|
---|
[10246] | 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 {
|
---|
[10367] | 77 | throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
|
---|
[10246] | 78 | }
|
---|
[10239] | 79 |
|
---|
[10246] | 80 | return valid;
|
---|
| 81 | }
|
---|
[10239] | 82 |
|
---|
[10246] | 83 | public string GetValue(int rowIndex, int columnIndex) {
|
---|
[10367] | 84 | return preprocessingData.GetCellAsString(columnIndex, rowIndex);
|
---|
[10246] | 85 | }
|
---|
| 86 |
|
---|
| 87 | public bool SetValue(string value, int rowIndex, int columnIndex) {
|
---|
| 88 | bool valid = false;
|
---|
[10367] | 89 | if (preprocessingData.IsType<double>(columnIndex)) {
|
---|
[10246] | 90 | double val;
|
---|
| 91 | valid = double.TryParse(value, out val);
|
---|
| 92 | if (valid) {
|
---|
[10367] | 93 | preprocessingData.SetCell<double>(columnIndex, rowIndex, val);
|
---|
[10239] | 94 | }
|
---|
[10367] | 95 | } else if (preprocessingData.IsType<string>(columnIndex)) {
|
---|
[10246] | 96 | valid = value != null;
|
---|
| 97 | if (valid) {
|
---|
[10367] | 98 | preprocessingData.SetCell<string>(columnIndex, rowIndex, value);
|
---|
[10246] | 99 | }
|
---|
[10367] | 100 | } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
|
---|
[10246] | 101 | DateTime date;
|
---|
| 102 | valid = DateTime.TryParse(value, out date);
|
---|
| 103 | if (valid) {
|
---|
[10367] | 104 | preprocessingData.SetCell<DateTime>(columnIndex, rowIndex, date);
|
---|
[10246] | 105 | }
|
---|
| 106 | } else {
|
---|
[10367] | 107 | throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
|
---|
[10239] | 108 | }
|
---|
[10246] | 109 |
|
---|
| 110 | return valid;
|
---|
[10239] | 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|