1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Globalization;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.DataPreprocessing {
|
---|
29 | public class DataGridLogic : IDataGridLogic {
|
---|
30 |
|
---|
31 | private ITransactionalPreprocessingData preprocessingData;
|
---|
32 |
|
---|
33 | public DataGridLogic(ITransactionalPreprocessingData preprocessingData) {
|
---|
34 | this.preprocessingData = preprocessingData;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public int Rows {
|
---|
38 | get {
|
---|
39 | return preprocessingData.Rows;
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public int Columns {
|
---|
44 | get {
|
---|
45 | return preprocessingData.Columns;
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public IEnumerable<string> ColumnNames {
|
---|
50 | get {
|
---|
51 | return preprocessingData.VariableNames;
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public IEnumerable<string> RowNames {
|
---|
56 | get {
|
---|
57 | return Enumerable.Range(1, Rows).Select(n => n.ToString());
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public bool Validate(string value, out string errorMessage, int columnIndex) {
|
---|
62 | if (columnIndex < 0 || columnIndex > preprocessingData.VariableNames.Count()) {
|
---|
63 | throw new ArgumentOutOfRangeException("column index is out of range");
|
---|
64 | }
|
---|
65 | bool valid = false;
|
---|
66 | if (preprocessingData.IsType<double>(columnIndex)) {
|
---|
67 | double val;
|
---|
68 | valid = double.TryParse(value, out val);
|
---|
69 | errorMessage = string.Empty;
|
---|
70 | if (!valid) {
|
---|
71 | errorMessage = "Invalid Value (Valid Value Format: \"" + FormatPatterns.GetDoubleFormatPattern() + "\")";
|
---|
72 | }
|
---|
73 | } else if (preprocessingData.IsType<string>(columnIndex)) {
|
---|
74 | valid = value != null;
|
---|
75 | errorMessage = string.Empty;
|
---|
76 | if (!valid) {
|
---|
77 | errorMessage = "Invalid Value (string must not be null)";
|
---|
78 | }
|
---|
79 | } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
|
---|
80 | DateTime date;
|
---|
81 | valid = DateTime.TryParse(value, out date);
|
---|
82 | errorMessage = string.Empty;
|
---|
83 | if (!valid) {
|
---|
84 | errorMessage = "Invalid Value (Valid Value Format: \"" + CultureInfo.CurrentCulture.DateTimeFormat + "\"";
|
---|
85 | }
|
---|
86 | } else {
|
---|
87 | throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
|
---|
88 | }
|
---|
89 |
|
---|
90 | return valid;
|
---|
91 | }
|
---|
92 |
|
---|
93 | public string GetValue(int columnIndex, int rowIndex) {
|
---|
94 | return preprocessingData.GetCellAsString(columnIndex, rowIndex);
|
---|
95 | }
|
---|
96 |
|
---|
97 | public bool SetValue(string value, int columnIndex, int rowIndex) {
|
---|
98 | bool valid = false;
|
---|
99 | if (preprocessingData.IsType<double>(columnIndex)) {
|
---|
100 | double val;
|
---|
101 | valid = double.TryParse(value, out val);
|
---|
102 | SetValueIfValid(columnIndex, rowIndex, valid, value);
|
---|
103 | } else if (preprocessingData.IsType<string>(columnIndex)) {
|
---|
104 | valid = value != null;
|
---|
105 | SetValueIfValid(columnIndex, rowIndex, valid, value);
|
---|
106 | } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
|
---|
107 | DateTime date;
|
---|
108 | valid = DateTime.TryParse(value, out date);
|
---|
109 | SetValueIfValid(columnIndex, rowIndex, valid, value);
|
---|
110 | } else {
|
---|
111 | throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
|
---|
112 | }
|
---|
113 | return valid;
|
---|
114 | }
|
---|
115 |
|
---|
116 | private void SetValueIfValid<T>(int columnIndex, int rowIndex, bool valid, T value) {
|
---|
117 | if (valid) {
|
---|
118 | preprocessingData.SetCell<T>(columnIndex, rowIndex, value);
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | public event DataPreprocessingChangedEventHandler Changed {
|
---|
123 | add { preprocessingData.Changed += value; }
|
---|
124 | remove { preprocessingData.Changed -= value; }
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|