[10539] | 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;
|
---|
[10239] | 23 | using System.Collections.Generic;
|
---|
[10246] | 24 | using System.Globalization;
|
---|
[10239] | 25 | using System.Linq;
|
---|
[10246] | 26 | using HeuristicLab.Data;
|
---|
[10239] | 27 |
|
---|
| 28 | namespace HeuristicLab.DataPreprocessing {
|
---|
| 29 | public class DataGridLogic : IDataGridLogic {
|
---|
| 30 |
|
---|
[10999] | 31 | private ITransactionalPreprocessingData PreprocessingData { get; private set; }
|
---|
[10660] | 32 | private IEnumerable<string> rowNames;
|
---|
[10239] | 33 |
|
---|
[10246] | 34 | public int Rows {
|
---|
| 35 | get {
|
---|
[10999] | 36 | return PreprocessingData.Rows;
|
---|
[10246] | 37 | }
|
---|
[10239] | 38 | }
|
---|
| 39 |
|
---|
[10246] | 40 | public int Columns {
|
---|
| 41 | get {
|
---|
[10999] | 42 | return PreprocessingData.Columns;
|
---|
[10246] | 43 | }
|
---|
| 44 | }
|
---|
[10239] | 45 |
|
---|
[10246] | 46 | public IEnumerable<string> ColumnNames {
|
---|
| 47 | get {
|
---|
[10999] | 48 | return PreprocessingData.VariableNames;
|
---|
[10246] | 49 | }
|
---|
| 50 | }
|
---|
[10239] | 51 |
|
---|
[10246] | 52 | public IEnumerable<string> RowNames {
|
---|
| 53 | get {
|
---|
[10660] | 54 | return rowNames;
|
---|
[10246] | 55 | }
|
---|
| 56 | }
|
---|
[10239] | 57 |
|
---|
[10999] | 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 |
|
---|
[10246] | 69 | public bool Validate(string value, out string errorMessage, int columnIndex) {
|
---|
[10999] | 70 | if (columnIndex < 0 || columnIndex > PreprocessingData.VariableNames.Count()) {
|
---|
[10246] | 71 | throw new ArgumentOutOfRangeException("column index is out of range");
|
---|
[10239] | 72 | }
|
---|
[10900] | 73 |
|
---|
[10246] | 74 | bool valid = false;
|
---|
[10900] | 75 | errorMessage = string.Empty;
|
---|
[10999] | 76 | if (PreprocessingData.IsType<double>(columnIndex)) {
|
---|
[10246] | 77 | double val;
|
---|
| 78 | valid = double.TryParse(value, out val);
|
---|
| 79 | if (!valid) {
|
---|
| 80 | errorMessage = "Invalid Value (Valid Value Format: \"" + FormatPatterns.GetDoubleFormatPattern() + "\")";
|
---|
| 81 | }
|
---|
[10999] | 82 | } else if (PreprocessingData.IsType<string>(columnIndex)) {
|
---|
[10246] | 83 | valid = value != null;
|
---|
| 84 | if (!valid) {
|
---|
| 85 | errorMessage = "Invalid Value (string must not be null)";
|
---|
| 86 | }
|
---|
[10999] | 87 | } else if (PreprocessingData.IsType<DateTime>(columnIndex)) {
|
---|
[10246] | 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 {
|
---|
[10367] | 94 | throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
|
---|
[10246] | 95 | }
|
---|
[10239] | 96 |
|
---|
[10246] | 97 | return valid;
|
---|
| 98 | }
|
---|
[10239] | 99 |
|
---|
[10616] | 100 | public string GetValue(int columnIndex, int rowIndex) {
|
---|
[10999] | 101 | return PreprocessingData.GetCellAsString(columnIndex, rowIndex);
|
---|
[10246] | 102 | }
|
---|
| 103 |
|
---|
[10616] | 104 | public bool SetValue(string value, int columnIndex, int rowIndex) {
|
---|
[10246] | 105 | bool valid = false;
|
---|
[10999] | 106 | if (PreprocessingData.IsType<double>(columnIndex)) {
|
---|
[10246] | 107 | double val;
|
---|
| 108 | valid = double.TryParse(value, out val);
|
---|
[10634] | 109 | SetValueIfValid(columnIndex, rowIndex, valid, val);
|
---|
[10999] | 110 | } else if (PreprocessingData.IsType<string>(columnIndex)) {
|
---|
[10246] | 111 | valid = value != null;
|
---|
[10616] | 112 | SetValueIfValid(columnIndex, rowIndex, valid, value);
|
---|
[10999] | 113 | } else if (PreprocessingData.IsType<DateTime>(columnIndex)) {
|
---|
[10246] | 114 | DateTime date;
|
---|
| 115 | valid = DateTime.TryParse(value, out date);
|
---|
[10634] | 116 | SetValueIfValid(columnIndex, rowIndex, valid, date);
|
---|
[10246] | 117 | } else {
|
---|
[10367] | 118 | throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
|
---|
[10239] | 119 | }
|
---|
[10246] | 120 | return valid;
|
---|
[10239] | 121 | }
|
---|
[10571] | 122 |
|
---|
[10616] | 123 | private void SetValueIfValid<T>(int columnIndex, int rowIndex, bool valid, T value) {
|
---|
| 124 | if (valid) {
|
---|
[10999] | 125 | PreprocessingData.SetCell<T>(columnIndex, rowIndex, value);
|
---|
[10616] | 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[10622] | 129 | public void DeleteRow(List<int> rows) {
|
---|
[10999] | 130 | PreprocessingData.InTransaction(() => {
|
---|
[10622] | 131 | foreach (int rowIndex in rows) {
|
---|
[10999] | 132 | PreprocessingData.DeleteRow(rowIndex);
|
---|
[10622] | 133 | }
|
---|
| 134 | });
|
---|
[10660] | 135 | createRowNames();
|
---|
[10622] | 136 | }
|
---|
| 137 |
|
---|
[10571] | 138 | public event DataPreprocessingChangedEventHandler Changed {
|
---|
[10999] | 139 | add { PreprocessingData.Changed += value; }
|
---|
| 140 | remove { PreprocessingData.Changed -= value; }
|
---|
[10571] | 141 | }
|
---|
[10621] | 142 |
|
---|
| 143 | public bool AreAllStringColumns(IEnumerable<int> columnIndices) {
|
---|
[10999] | 144 | return columnIndices.All(x => PreprocessingData.IsType<string>(x));
|
---|
[10621] | 145 | }
|
---|
[10804] | 146 |
|
---|
| 147 |
|
---|
| 148 | public void SetSelection(IDictionary<int, IList<int>> selection) {
|
---|
[10999] | 149 | PreprocessingData.Selection = selection;
|
---|
[10804] | 150 | }
|
---|
| 151 |
|
---|
| 152 | public IDictionary<int, IList<int>> GetSelection() {
|
---|
[10999] | 153 | return PreprocessingData.Selection;
|
---|
[10804] | 154 | }
|
---|
| 155 | public void ClearSelection() {
|
---|
[10999] | 156 | PreprocessingData.ClearSelection();
|
---|
[10804] | 157 | }
|
---|
| 158 |
|
---|
| 159 | public event EventHandler SelectionChanged {
|
---|
[10999] | 160 | add { PreprocessingData.SelectionChanged += value; }
|
---|
| 161 | remove { PreprocessingData.SelectionChanged -= value; }
|
---|
[10804] | 162 | }
|
---|
| 163 |
|
---|
[10239] | 164 | }
|
---|
| 165 | }
|
---|