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