[11589] | 1 | #region License Information
|
---|
| 2 |
|
---|
| 3 | /* HeuristicLab
|
---|
[12032] | 4 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11589] | 5 | *
|
---|
| 6 | * This file is part of HeuristicLab.
|
---|
| 7 | *
|
---|
| 8 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 9 | * it under the terms of the GNU General Public License as published by
|
---|
| 10 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 11 | * (at your option) any later version.
|
---|
| 12 | *
|
---|
| 13 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 16 | * GNU General Public License for more details.
|
---|
| 17 | *
|
---|
| 18 | * You should have received a copy of the GNU General Public License
|
---|
| 19 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | #endregion
|
---|
| 23 |
|
---|
| 24 | using System;
|
---|
| 25 | using System.Collections;
|
---|
| 26 | using System.Collections.Generic;
|
---|
| 27 | using System.Linq;
|
---|
| 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
[12141] | 30 | using HeuristicLab.Data;
|
---|
[11589] | 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
| 34 | [Item("ModifiableDataset", "Represents a dataset containing data that should be analyzed, which can be modified by adding or replacing variables and values.")]
|
---|
| 35 | [StorableClass]
|
---|
[12511] | 36 | public sealed class ModifiableDataset : Dataset, IStringConvertibleMatrix {
|
---|
[11589] | 37 | [StorableConstructor]
|
---|
| 38 | private ModifiableDataset(bool deserializing) : base(deserializing) { }
|
---|
| 39 | private ModifiableDataset(ModifiableDataset original, Cloner cloner) : base(original, cloner) { }
|
---|
| 40 | public override IDeepCloneable Clone(Cloner cloner) { return new ModifiableDataset(this, cloner); }
|
---|
| 41 | public ModifiableDataset() : base() { }
|
---|
[12508] | 42 |
|
---|
| 43 | public ModifiableDataset(Dataset dataset) : base(dataset) { }
|
---|
[11589] | 44 | public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues) : base(variableNames, variableValues) { }
|
---|
| 45 |
|
---|
| 46 | public void ReplaceRow(int row, IEnumerable<object> values) {
|
---|
| 47 | var list = values.ToList();
|
---|
| 48 | if (list.Count != variableNames.Count)
|
---|
| 49 | throw new ArgumentException("The number of values must be equal to the number of variable names.");
|
---|
| 50 | // check if all the values are of the correct type
|
---|
| 51 | for (int i = 0; i < list.Count; ++i) {
|
---|
| 52 | if (list[i].GetType() != GetVariableType(variableNames[i])) {
|
---|
| 53 | throw new ArgumentException("The type of the provided value does not match the variable type.");
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 | // replace values
|
---|
| 57 | for (int i = 0; i < list.Count; ++i) {
|
---|
| 58 | variableValues[variableNames[i]][row] = list[i];
|
---|
| 59 | }
|
---|
[12489] | 60 | OnReset();
|
---|
[11589] | 61 | }
|
---|
| 62 |
|
---|
| 63 | public void AddRow(IEnumerable<object> values) {
|
---|
| 64 | var list = values.ToList();
|
---|
| 65 | if (list.Count != variableNames.Count)
|
---|
| 66 | throw new ArgumentException("The number of values must be equal to the number of variable names.");
|
---|
| 67 | // check if all the values are of the correct type
|
---|
| 68 | for (int i = 0; i < list.Count; ++i) {
|
---|
| 69 | if (list[i].GetType() != GetVariableType(variableNames[i])) {
|
---|
| 70 | throw new ArgumentException("The type of the provided value does not match the variable type.");
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | // add values
|
---|
| 74 | for (int i = 0; i < list.Count; ++i) {
|
---|
| 75 | variableValues[variableNames[i]].Add(list[i]);
|
---|
| 76 | }
|
---|
| 77 | rows++;
|
---|
[12489] | 78 | OnRowsChanged();
|
---|
| 79 | OnReset();
|
---|
[11589] | 80 | }
|
---|
| 81 |
|
---|
[12032] | 82 | // adds a new variable to the dataset
|
---|
| 83 | public void AddVariable<T>(string variableName, IEnumerable<T> values) {
|
---|
| 84 | if (variableValues.ContainsKey(variableName))
|
---|
| 85 | throw new ArgumentException("Variable " + variableName + " is already present in the dataset.");
|
---|
| 86 | int count = values.Count();
|
---|
| 87 | if (count != rows)
|
---|
| 88 | throw new ArgumentException("The number of values must exactly match the number of rows in the dataset.");
|
---|
| 89 | variableValues[variableName] = new List<T>(values);
|
---|
| 90 | variableNames.Add(variableName);
|
---|
[12489] | 91 | OnColumnsChanged();
|
---|
| 92 | OnColumnNamesChanged();
|
---|
| 93 | OnReset();
|
---|
[12032] | 94 | }
|
---|
| 95 |
|
---|
| 96 | public void RemoveVariable(string variableName) {
|
---|
| 97 | if (!variableValues.ContainsKey(variableName))
|
---|
| 98 | throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
|
---|
| 99 | variableValues.Remove(variableName);
|
---|
| 100 | variableNames.Remove(variableName);
|
---|
[12489] | 101 | OnColumnsChanged();
|
---|
| 102 | OnColumnNamesChanged();
|
---|
| 103 | OnReset();
|
---|
[12032] | 104 | }
|
---|
| 105 |
|
---|
[11589] | 106 | // slow, avoid to use this
|
---|
| 107 | public void RemoveRow(int row) {
|
---|
| 108 | foreach (var list in variableValues.Values)
|
---|
| 109 | list.RemoveAt(row);
|
---|
| 110 | rows--;
|
---|
[12489] | 111 | OnRowsChanged();
|
---|
| 112 | OnReset();
|
---|
[11589] | 113 | }
|
---|
| 114 |
|
---|
[12141] | 115 | public void SetVariableValue(object value, string variableName, int row) {
|
---|
[12032] | 116 | IList list;
|
---|
| 117 | variableValues.TryGetValue(variableName, out list);
|
---|
| 118 | if (list == null)
|
---|
| 119 | throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
|
---|
| 120 | if (row < 0 || list.Count < row)
|
---|
| 121 | throw new ArgumentOutOfRangeException("Invalid row value");
|
---|
[12141] | 122 | if (GetVariableType(variableName) != value.GetType())
|
---|
| 123 | throw new ArgumentException("The type of the provided value does not match the variable type.");
|
---|
| 124 |
|
---|
[12032] | 125 | list[row] = value;
|
---|
[12508] | 126 | OnItemChanged(row, variableNames.IndexOf(variableName));
|
---|
[12032] | 127 | }
|
---|
| 128 |
|
---|
[11589] | 129 | private Type GetVariableType(string variableName) {
|
---|
| 130 | IList list;
|
---|
| 131 | variableValues.TryGetValue(variableName, out list);
|
---|
| 132 | if (list == null)
|
---|
| 133 | throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
|
---|
[12190] | 134 | return list.GetType().GetGenericArguments()[0];
|
---|
[11589] | 135 | }
|
---|
[12141] | 136 |
|
---|
| 137 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
| 138 | var variableName = variableNames[columnIndex];
|
---|
| 139 | // if value represents a double
|
---|
| 140 | double dv;
|
---|
| 141 | if (double.TryParse(value, out dv)) {
|
---|
| 142 | SetVariableValue(dv, variableName, rowIndex);
|
---|
| 143 | return true;
|
---|
| 144 | }
|
---|
| 145 | // if value represents a DateTime object
|
---|
| 146 | DateTime dt;
|
---|
| 147 | if (DateTime.TryParse(value, out dt)) {
|
---|
| 148 | SetVariableValue(dt, variableName, rowIndex);
|
---|
| 149 | return true;
|
---|
| 150 | }
|
---|
| 151 | // if value is simply a string
|
---|
| 152 | SetVariableValue(value, variableName, rowIndex);
|
---|
| 153 | return true;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
[12508] | 157 | errorMessage = string.Empty;
|
---|
| 158 | return true;
|
---|
[12141] | 159 | }
|
---|
| 160 |
|
---|
[12489] | 161 | #region event handlers
|
---|
| 162 | public override event EventHandler RowsChanged;
|
---|
| 163 | private void OnRowsChanged() {
|
---|
[12508] | 164 | var handler = RowsChanged;
|
---|
| 165 | if (handler != null)
|
---|
| 166 | handler(this, EventArgs.Empty);
|
---|
[12489] | 167 | }
|
---|
| 168 |
|
---|
| 169 | public override event EventHandler ColumnsChanged;
|
---|
| 170 | private void OnColumnsChanged() {
|
---|
[12508] | 171 | var handler = ColumnsChanged;
|
---|
| 172 | if (handler != null)
|
---|
| 173 | handler(this, EventArgs.Empty);
|
---|
[12489] | 174 | }
|
---|
| 175 |
|
---|
| 176 | public override event EventHandler ColumnNamesChanged;
|
---|
| 177 | private void OnColumnNamesChanged() {
|
---|
[12508] | 178 | var handler = ColumnNamesChanged;
|
---|
| 179 | if (handler != null)
|
---|
| 180 | handler(this, EventArgs.Empty);
|
---|
[12489] | 181 | }
|
---|
| 182 |
|
---|
| 183 | public override event EventHandler Reset;
|
---|
| 184 | private void OnReset() {
|
---|
[12508] | 185 | var handler = Reset;
|
---|
| 186 | if (handler != null)
|
---|
| 187 | handler(this, EventArgs.Empty);
|
---|
[12489] | 188 | }
|
---|
| 189 |
|
---|
| 190 | public override event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
| 191 | private void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
[12508] | 192 | var handler = ItemChanged;
|
---|
| 193 | if (handler != null) {
|
---|
| 194 | handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
[12489] | 195 | }
|
---|
| 196 | }
|
---|
| 197 | #endregion
|
---|
[11589] | 198 | }
|
---|
| 199 | }
|
---|