#region License Information /* HeuristicLab * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections; using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.DataAnalysis { [Item("ModifiableDataset", "Represents a dataset containing data that should be analyzed, which can be modified by adding or replacing variables and values.")] [StorableClass] public class ModifiableDataset : Dataset { [StorableConstructor] private ModifiableDataset(bool deserializing) : base(deserializing) { } private ModifiableDataset(ModifiableDataset original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new ModifiableDataset(this, cloner); } public ModifiableDataset() : base() { } public ModifiableDataset(IEnumerable variableNames, IEnumerable variableValues) : base(variableNames, variableValues) { } public void ReplaceRow(int row, IEnumerable values) { var list = values.ToList(); if (list.Count != variableNames.Count) throw new ArgumentException("The number of values must be equal to the number of variable names."); // check if all the values are of the correct type for (int i = 0; i < list.Count; ++i) { if (list[i].GetType() != GetVariableType(variableNames[i])) { throw new ArgumentException("The type of the provided value does not match the variable type."); } } // replace values for (int i = 0; i < list.Count; ++i) { variableValues[variableNames[i]][row] = list[i]; } } public void AddRow(IEnumerable values) { var list = values.ToList(); if (list.Count != variableNames.Count) throw new ArgumentException("The number of values must be equal to the number of variable names."); // check if all the values are of the correct type for (int i = 0; i < list.Count; ++i) { if (list[i].GetType() != GetVariableType(variableNames[i])) { throw new ArgumentException("The type of the provided value does not match the variable type."); } } // add values for (int i = 0; i < list.Count; ++i) { variableValues[variableNames[i]].Add(list[i]); } rows++; } // slow, avoid to use this public void RemoveRow(int row) { foreach (var list in variableValues.Values) list.RemoveAt(row); rows--; } private Type GetVariableType(string variableName) { IList list; variableValues.TryGetValue(variableName, out list); if (list == null) throw new ArgumentException("The variable " + variableName + " does not exist in the dataset."); return list[0].GetType(); } } }