Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/Implementations/DataGridLogic.cs @ 10999

Last change on this file since 10999 was 10999, checked in by rstoll, 8 years ago
  • Reordered Code
File size: 5.3 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;
27
28namespace HeuristicLab.DataPreprocessing {
29  public class DataGridLogic : IDataGridLogic {
30
31    private ITransactionalPreprocessingData PreprocessingData { get; private set; }
32    private IEnumerable<string> rowNames;
33
34    public int Rows {
35      get {
36        return PreprocessingData.Rows;
37      }
38    }
39
40    public int Columns {
41      get {
42        return PreprocessingData.Columns;
43      }
44    }
45
46    public IEnumerable<string> ColumnNames {
47      get {
48        return PreprocessingData.VariableNames;
49      }
50    }
51
52    public IEnumerable<string> RowNames {
53      get {
54        return rowNames;
55      }
56    }
57
58    public DataGridLogic(ITransactionalPreprocessingData preprocessingData) {
59      PreprocessingData = preprocessingData;
60      createRowNames();
61    }
62
63    private void createRowNames() {
64      rowNames = Enumerable.Range(1, Rows).Select(n => n.ToString());
65    }
66
67
68
69    public bool Validate(string value, out string errorMessage, int columnIndex) {
70      if (columnIndex < 0 || columnIndex > PreprocessingData.VariableNames.Count()) {
71        throw new ArgumentOutOfRangeException("column index is out of range");
72      }
73
74      bool valid = false;
75      errorMessage = string.Empty;
76      if (PreprocessingData.IsType<double>(columnIndex)) {
77        double val;
78        valid = double.TryParse(value, out val);
79        if (!valid) {
80          errorMessage = "Invalid Value (Valid Value Format: \"" + FormatPatterns.GetDoubleFormatPattern() + "\")";
81        }
82      } else if (PreprocessingData.IsType<string>(columnIndex)) {
83        valid = value != null;
84        if (!valid) {
85          errorMessage = "Invalid Value (string must not be null)";
86        }
87      } else if (PreprocessingData.IsType<DateTime>(columnIndex)) {
88        DateTime date;
89        valid = DateTime.TryParse(value, out date);
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
148    public void SetSelection(IDictionary<int, IList<int>> selection) {
149      PreprocessingData.Selection = selection;
150    }
151
152    public IDictionary<int, IList<int>> GetSelection() {
153      return PreprocessingData.Selection;
154    }
155    public void ClearSelection() {
156      PreprocessingData.ClearSelection();
157    }
158
159    public event EventHandler SelectionChanged {
160      add { PreprocessingData.SelectionChanged += value; }
161      remove { PreprocessingData.SelectionChanged -= value; }
162    }
163
164  }
165}
Note: See TracBrowser for help on using the repository browser.