[11589] | 1 | #region License Information
|
---|
| 2 |
|
---|
| 3 | /* HeuristicLab
|
---|
[13760] | 4 | * Copyright (C) 2002-2016 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) { }
|
---|
[13027] | 39 |
|
---|
| 40 | private ModifiableDataset(ModifiableDataset original, Cloner cloner) : base(original, cloner) {
|
---|
| 41 | var variables = variableValues.Keys.ToList();
|
---|
| 42 | foreach (var v in variables) {
|
---|
| 43 | var type = GetVariableType(v);
|
---|
| 44 | if (type == typeof(DateTime)) {
|
---|
| 45 | variableValues[v] = GetDateTimeValues(v).ToList();
|
---|
| 46 | } else if (type == typeof(double)) {
|
---|
| 47 | variableValues[v] = GetDoubleValues(v).ToList();
|
---|
[13040] | 48 | } else if (type == typeof(string)) {
|
---|
| 49 | variableValues[v] = GetStringValues(v).ToList();
|
---|
| 50 | } else {
|
---|
| 51 | throw new ArgumentException("Unsupported type " + type + " for variable " + v);
|
---|
[13027] | 52 | }
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
[11589] | 55 | public override IDeepCloneable Clone(Cloner cloner) { return new ModifiableDataset(this, cloner); }
|
---|
| 56 | public ModifiableDataset() : base() { }
|
---|
[12508] | 57 |
|
---|
[11589] | 58 | public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues) : base(variableNames, variableValues) { }
|
---|
| 59 |
|
---|
| 60 | public void ReplaceRow(int row, IEnumerable<object> values) {
|
---|
| 61 | var list = values.ToList();
|
---|
| 62 | if (list.Count != variableNames.Count)
|
---|
| 63 | throw new ArgumentException("The number of values must be equal to the number of variable names.");
|
---|
| 64 | // check if all the values are of the correct type
|
---|
| 65 | for (int i = 0; i < list.Count; ++i) {
|
---|
| 66 | if (list[i].GetType() != GetVariableType(variableNames[i])) {
|
---|
| 67 | throw new ArgumentException("The type of the provided value does not match the variable type.");
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | // replace values
|
---|
| 71 | for (int i = 0; i < list.Count; ++i) {
|
---|
| 72 | variableValues[variableNames[i]][row] = list[i];
|
---|
| 73 | }
|
---|
[12489] | 74 | OnReset();
|
---|
[11589] | 75 | }
|
---|
| 76 |
|
---|
[13761] | 77 | public void ReplaceVariable(string variableName, IList values) {
|
---|
[13760] | 78 | if (!variableValues.ContainsKey(variableName))
|
---|
[15030] | 79 | throw new ArgumentException(string.Format("Variable {0} is not present in the dataset.", variableName));
|
---|
[13760] | 80 | if (values.Count != variableValues[variableName].Count)
|
---|
| 81 | throw new ArgumentException("The number of values must coincide with the number of dataset rows.");
|
---|
| 82 | if (GetVariableType(variableName) != values[0].GetType())
|
---|
| 83 | throw new ArgumentException("The type of the provided value does not match the variable type.");
|
---|
| 84 | variableValues[variableName] = values;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[11589] | 87 | public void AddRow(IEnumerable<object> values) {
|
---|
| 88 | var list = values.ToList();
|
---|
| 89 | if (list.Count != variableNames.Count)
|
---|
| 90 | throw new ArgumentException("The number of values must be equal to the number of variable names.");
|
---|
| 91 | // check if all the values are of the correct type
|
---|
| 92 | for (int i = 0; i < list.Count; ++i) {
|
---|
| 93 | if (list[i].GetType() != GetVariableType(variableNames[i])) {
|
---|
| 94 | throw new ArgumentException("The type of the provided value does not match the variable type.");
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | // add values
|
---|
| 98 | for (int i = 0; i < list.Count; ++i) {
|
---|
| 99 | variableValues[variableNames[i]].Add(list[i]);
|
---|
| 100 | }
|
---|
| 101 | rows++;
|
---|
[12489] | 102 | OnRowsChanged();
|
---|
| 103 | OnReset();
|
---|
[11589] | 104 | }
|
---|
| 105 |
|
---|
[12032] | 106 | // adds a new variable to the dataset
|
---|
| 107 | public void AddVariable<T>(string variableName, IEnumerable<T> values) {
|
---|
| 108 | if (variableValues.ContainsKey(variableName))
|
---|
| 109 | throw new ArgumentException("Variable " + variableName + " is already present in the dataset.");
|
---|
| 110 | int count = values.Count();
|
---|
| 111 | if (count != rows)
|
---|
| 112 | throw new ArgumentException("The number of values must exactly match the number of rows in the dataset.");
|
---|
| 113 | variableValues[variableName] = new List<T>(values);
|
---|
| 114 | variableNames.Add(variableName);
|
---|
[12489] | 115 | OnColumnsChanged();
|
---|
| 116 | OnColumnNamesChanged();
|
---|
| 117 | OnReset();
|
---|
[12032] | 118 | }
|
---|
| 119 |
|
---|
| 120 | public void RemoveVariable(string variableName) {
|
---|
| 121 | if (!variableValues.ContainsKey(variableName))
|
---|
| 122 | throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
|
---|
| 123 | variableValues.Remove(variableName);
|
---|
| 124 | variableNames.Remove(variableName);
|
---|
[12489] | 125 | OnColumnsChanged();
|
---|
| 126 | OnColumnNamesChanged();
|
---|
| 127 | OnReset();
|
---|
[12032] | 128 | }
|
---|
| 129 |
|
---|
[11589] | 130 | // slow, avoid to use this
|
---|
| 131 | public void RemoveRow(int row) {
|
---|
| 132 | foreach (var list in variableValues.Values)
|
---|
| 133 | list.RemoveAt(row);
|
---|
| 134 | rows--;
|
---|
[12489] | 135 | OnRowsChanged();
|
---|
| 136 | OnReset();
|
---|
[11589] | 137 | }
|
---|
| 138 |
|
---|
[12141] | 139 | public void SetVariableValue(object value, string variableName, int row) {
|
---|
[12032] | 140 | IList list;
|
---|
| 141 | variableValues.TryGetValue(variableName, out list);
|
---|
| 142 | if (list == null)
|
---|
| 143 | throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
|
---|
| 144 | if (row < 0 || list.Count < row)
|
---|
| 145 | throw new ArgumentOutOfRangeException("Invalid row value");
|
---|
[12141] | 146 | if (GetVariableType(variableName) != value.GetType())
|
---|
| 147 | throw new ArgumentException("The type of the provided value does not match the variable type.");
|
---|
| 148 |
|
---|
[12032] | 149 | list[row] = value;
|
---|
[12508] | 150 | OnItemChanged(row, variableNames.IndexOf(variableName));
|
---|
[12032] | 151 | }
|
---|
| 152 |
|
---|
[11589] | 153 | private Type GetVariableType(string variableName) {
|
---|
| 154 | IList list;
|
---|
| 155 | variableValues.TryGetValue(variableName, out list);
|
---|
| 156 | if (list == null)
|
---|
| 157 | throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
|
---|
[12190] | 158 | return list.GetType().GetGenericArguments()[0];
|
---|
[11589] | 159 | }
|
---|
[12141] | 160 |
|
---|
| 161 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
| 162 | var variableName = variableNames[columnIndex];
|
---|
| 163 | // if value represents a double
|
---|
| 164 | double dv;
|
---|
| 165 | if (double.TryParse(value, out dv)) {
|
---|
| 166 | SetVariableValue(dv, variableName, rowIndex);
|
---|
| 167 | return true;
|
---|
| 168 | }
|
---|
| 169 | // if value represents a DateTime object
|
---|
| 170 | DateTime dt;
|
---|
| 171 | if (DateTime.TryParse(value, out dt)) {
|
---|
| 172 | SetVariableValue(dt, variableName, rowIndex);
|
---|
| 173 | return true;
|
---|
| 174 | }
|
---|
| 175 | // if value is simply a string
|
---|
| 176 | SetVariableValue(value, variableName, rowIndex);
|
---|
| 177 | return true;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
[12508] | 181 | errorMessage = string.Empty;
|
---|
| 182 | return true;
|
---|
[12141] | 183 | }
|
---|
| 184 |
|
---|
[12489] | 185 | #region event handlers
|
---|
| 186 | public override event EventHandler RowsChanged;
|
---|
| 187 | private void OnRowsChanged() {
|
---|
[12508] | 188 | var handler = RowsChanged;
|
---|
| 189 | if (handler != null)
|
---|
| 190 | handler(this, EventArgs.Empty);
|
---|
[12489] | 191 | }
|
---|
| 192 |
|
---|
| 193 | public override event EventHandler ColumnsChanged;
|
---|
| 194 | private void OnColumnsChanged() {
|
---|
[12508] | 195 | var handler = ColumnsChanged;
|
---|
| 196 | if (handler != null)
|
---|
| 197 | handler(this, EventArgs.Empty);
|
---|
[12489] | 198 | }
|
---|
| 199 |
|
---|
| 200 | public override event EventHandler ColumnNamesChanged;
|
---|
| 201 | private void OnColumnNamesChanged() {
|
---|
[12508] | 202 | var handler = ColumnNamesChanged;
|
---|
| 203 | if (handler != null)
|
---|
| 204 | handler(this, EventArgs.Empty);
|
---|
[12489] | 205 | }
|
---|
| 206 |
|
---|
| 207 | public override event EventHandler Reset;
|
---|
| 208 | private void OnReset() {
|
---|
[12508] | 209 | var handler = Reset;
|
---|
| 210 | if (handler != null)
|
---|
| 211 | handler(this, EventArgs.Empty);
|
---|
[12489] | 212 | }
|
---|
| 213 |
|
---|
| 214 | public override event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
| 215 | private void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
[12508] | 216 | var handler = ItemChanged;
|
---|
| 217 | if (handler != null) {
|
---|
| 218 | handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
[12489] | 219 | }
|
---|
| 220 | }
|
---|
| 221 | #endregion
|
---|
[11589] | 222 | }
|
---|
| 223 | }
|
---|