Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/ModifiableDataset.cs @ 13001

Last change on this file since 13001 was 12511, checked in by mkommend, 9 years ago

#2276: Sealed ModifiableDataset.

File size: 7.5 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
24using System;
25using System.Collections;
26using System.Collections.Generic;
27using System.Linq;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace 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]
36  public sealed class ModifiableDataset : Dataset, IStringConvertibleMatrix {
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() { }
42
43    public ModifiableDataset(Dataset dataset) : base(dataset) { }
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      }
60      OnReset();
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++;
78      OnRowsChanged();
79      OnReset();
80    }
81
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);
91      OnColumnsChanged();
92      OnColumnNamesChanged();
93      OnReset();
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);
101      OnColumnsChanged();
102      OnColumnNamesChanged();
103      OnReset();
104    }
105
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--;
111      OnRowsChanged();
112      OnReset();
113    }
114
115    public void SetVariableValue(object value, string variableName, int row) {
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");
122      if (GetVariableType(variableName) != value.GetType())
123        throw new ArgumentException("The type of the provided value does not match the variable type.");
124
125      list[row] = value;
126      OnItemChanged(row, variableNames.IndexOf(variableName));
127    }
128
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.");
134      return list.GetType().GetGenericArguments()[0];
135    }
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) {
157      errorMessage = string.Empty;
158      return true;
159    }
160
161    #region event handlers
162    public override event EventHandler RowsChanged;
163    private void OnRowsChanged() {
164      var handler = RowsChanged;
165      if (handler != null)
166        handler(this, EventArgs.Empty);
167    }
168
169    public override event EventHandler ColumnsChanged;
170    private void OnColumnsChanged() {
171      var handler = ColumnsChanged;
172      if (handler != null)
173        handler(this, EventArgs.Empty);
174    }
175
176    public override event EventHandler ColumnNamesChanged;
177    private void OnColumnNamesChanged() {
178      var handler = ColumnNamesChanged;
179      if (handler != null)
180        handler(this, EventArgs.Empty);
181    }
182
183    public override event EventHandler Reset;
184    private void OnReset() {
185      var handler = Reset;
186      if (handler != null)
187        handler(this, EventArgs.Empty);
188    }
189
190    public override event EventHandler<EventArgs<int, int>> ItemChanged;
191    private void OnItemChanged(int rowIndex, int columnIndex) {
192      var handler = ItemChanged;
193      if (handler != null) {
194        handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
195      }
196    }
197    #endregion
198  }
199}
Note: See TracBrowser for help on using the repository browser.