#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.Generic; using System.Collections.ObjectModel; using System.Globalization; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.DataAnalysis { [Item("SimpleDataset", "A dataset which allows access to a single row of updateable variables")] [StorableClass] public class SimpleDataset : NamedItem, IDataset { [Storable] private List names; [Storable] private List values; public int Rows { get { return 1; } set { throw new InvalidOperationException(); } } public int Columns { get { return names.Count; } set { throw new InvalidOperationException(); } } public IEnumerable ColumnNames { get; set; } public IEnumerable RowNames { get; set; } public bool SortableView { get; set; } public bool ReadOnly { get; private set; } public bool Validate(string value, out string errorMessage) { throw new NotImplementedException(); } public string GetValue(int rowIndex, int columnIndex) { throw new NotImplementedException(); } public bool SetValue(string value, int rowIndex, int columnIndex) { throw new NotImplementedException(); } public event EventHandler ColumnsChanged; public event EventHandler RowsChanged; public event EventHandler ColumnNamesChanged; public event EventHandler RowNamesChanged; public event EventHandler SortableViewChanged; public event EventHandler> ItemChanged; public event EventHandler Reset; public SimpleDataset() { } public SimpleDataset(IEnumerable variableNames, IEnumerable variableValues) { if (variableNames.Count() != variableValues.Count()) throw new ArgumentException("The variable names and values should be the same length."); names = new List(variableNames); values = new List(variableValues); } private SimpleDataset(SimpleDataset original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new SimpleDataset(this, cloner); } [StorableConstructor] private SimpleDataset(bool deserializing) : base(deserializing) { } public double GetDoubleValue(string variableName, int row) { int i = names.IndexOf(variableName); if (i == -1) throw new ArgumentException("The given variable name is not present in the dataset."); return values[names.IndexOf(variableName)]; } public IEnumerable GetValues(string variableName, IEnumerable rows) { throw new NotImplementedException(); } public IEnumerable GetValues(string variableName) { throw new NotImplementedException(); } public IEnumerable GetDoubleValues(string variableName, IEnumerable rows) { yield return GetDoubleValue(variableName, 0); } public IEnumerable GetDoubleValues(string variableName) { yield return GetDoubleValue(variableName, 0); } public ReadOnlyCollection GetReadOnlyDoubleValues(string variableName) { return GetDoubleValues(variableName, Enumerable.Empty()).ToList().AsReadOnly(); } public IEnumerable GetStringValues(string variableName) { yield return GetDoubleValue(variableName, 0).ToString(CultureInfo.InvariantCulture); } public IEnumerable GetDateTimeValues(string variableName) { throw new NotImplementedException(); } public void SetDoubleValue(string variableName, double value) { int i = names.IndexOf(variableName); if (i == -1) throw new ArgumentException("The given variable name is not present in the dataset."); } public IEnumerable VariableNames { get { return names; } set { if (value.Count() != values.Count()) throw new ArgumentException("The names count should equal the values count."); names = value.ToList(); } } public IEnumerable VariableValues { get { return values; } set { if (value.Count() != names.Count()) throw new ArgumentException("The values count should equal the names count."); values = new List(value); } } public IEnumerable DoubleVariables { get { return names; } // all variables are of type double } } }