Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/DataGridLogic.cs @ 10660

Last change on this file since 10660 was 10660, checked in by rstoll, 10 years ago

Improved performance for RowUpdateHeaders()

File size: 4.9 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Globalization;
25using System.Linq;
26using HeuristicLab.Data;
27using HeuristicLab.DataPreprocessing.Implementations;
28
29namespace HeuristicLab.DataPreprocessing {
30  public class DataGridLogic : IDataGridLogic {
31
32    private ITransactionalPreprocessingData preprocessingData;
33    private IEnumerable<string> rowNames;
34
35    public DataGridLogic(ITransactionalPreprocessingData preprocessingData) {
36      this.preprocessingData = preprocessingData;
37      createRowNames();
38    }
39
40    private void createRowNames() {
41      rowNames = Enumerable.Range(1, Rows).Select(n => n.ToString());
42    }
43
44    public int Rows {
45      get {
46        return preprocessingData.Rows;
47      }
48    }
49
50    public int Columns {
51      get {
52        return preprocessingData.Columns;
53      }
54    }
55
56    public IEnumerable<string> ColumnNames {
57      get {
58        return preprocessingData.VariableNames;
59      }
60    }
61
62    public IEnumerable<string> RowNames {
63      get {
64        return rowNames;
65      }
66    }
67
68    public bool Validate(string value, out string errorMessage, int columnIndex) {
69      if (columnIndex < 0 || columnIndex > preprocessingData.VariableNames.Count()) {
70        throw new ArgumentOutOfRangeException("column index is out of range");
71      }
72      bool valid = false;
73      if (preprocessingData.IsType<double>(columnIndex)) {
74        double val;
75        valid = double.TryParse(value, out val);
76        errorMessage = string.Empty;
77        if (!valid) {
78          errorMessage = "Invalid Value (Valid Value Format: \"" + FormatPatterns.GetDoubleFormatPattern() + "\")";
79        }
80      } else if (preprocessingData.IsType<string>(columnIndex)) {
81        valid = value != null;
82        errorMessage = string.Empty;
83        if (!valid) {
84          errorMessage = "Invalid Value (string must not be null)";
85        }
86      } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
87        DateTime date;
88        valid = DateTime.TryParse(value, out date);
89        errorMessage = string.Empty;
90        if (!valid) {
91          errorMessage = "Invalid Value (Valid Value Format: \"" + CultureInfo.CurrentCulture.DateTimeFormat + "\"";
92        }
93      } else {
94        throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
95      }
96
97      return valid;
98    }
99
100    public string GetValue(int columnIndex, int rowIndex) {
101      return preprocessingData.GetCellAsString(columnIndex, rowIndex);
102    }
103
104    public bool SetValue(string value, int columnIndex, int rowIndex) {
105      bool valid = false;
106      if (preprocessingData.IsType<double>(columnIndex)) {
107        double val;
108        valid = double.TryParse(value, out val);
109        SetValueIfValid(columnIndex, rowIndex, valid, val);
110      } else if (preprocessingData.IsType<string>(columnIndex)) {
111        valid = value != null;
112        SetValueIfValid(columnIndex, rowIndex, valid, value);
113      } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
114        DateTime date;
115        valid = DateTime.TryParse(value, out date);
116        SetValueIfValid(columnIndex, rowIndex, valid, date);
117      } else {
118        throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
119      }
120      return valid;
121    }
122
123    private void SetValueIfValid<T>(int columnIndex, int rowIndex, bool valid, T value) {
124      if (valid) {
125        preprocessingData.SetCell<T>(columnIndex, rowIndex, value);
126      }
127    }
128
129    public void DeleteRow(List<int> rows) {
130      preprocessingData.InTransaction(() => {
131        foreach (int rowIndex in rows) {
132          preprocessingData.DeleteRow(rowIndex);
133        }
134      });
135      createRowNames();
136    }
137
138    public event DataPreprocessingChangedEventHandler Changed {
139      add { preprocessingData.Changed += value; }
140      remove { preprocessingData.Changed -= value; }
141    }
142
143    public bool AreAllStringColumns(IEnumerable<int> columnIndices) {
144      return columnIndices.All(x => preprocessingData.IsType<string>(x));
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.