#region License Information /* HeuristicLab * Copyright (C) 2002-2011 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.Linq; using System.Text; using System.Windows.Forms; using System.Xml; using HeuristicLab.DataImporter.Data.CommandBase; using HeuristicLab.DataImporter.Data.Model; using HeuristicLab.DataImporter.Data; using HeuristicLab.DataImporter.Command.View; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.DataImporter.Command { [StorableClass] [ViewableCommandInfoAttribute("Equidistant Sampling", 1, ColumnGroupState.Sorted, "Time Series", Position = 0, OptionsView = typeof(TimeBasedCommandView))] public class EquidistantSamplingCommand : EquidistantTimeSeriesCommandBase { private List insertedRows; private DateTimeColumn columnToSample; private EquidistantSamplingCommand() : base(null, string.Empty) { } public EquidistantSamplingCommand(DataSet dataSet, string columnGroupName) : base(dataSet, columnGroupName) { } public override string Description { get { return "Equidistant Sampling"; } } public override void Execute() { base.Execute(); if (!(ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.ElementAt(0)) is DateTimeColumn) || ColumnGroup.SortedColumnIndexes.Count != 1) throw new CommandExecutionException("ColumnGroup mus be sorted a the datetime column.", this); columnToSample = (DateTimeColumn)ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.ElementAt(0)); if (columnToSample.SortOrder != SortOrder.Ascending) throw new CommandExecutionException("ColumnGroup must be sorted ascending after the selected datetime column.", this); if (columnToSample.ContainsNullValues) throw new CommandExecutionException("The datetime column must not contain null values.", this); for (int i = 1; i < columnToSample.TotalValuesCount; i++) { if (columnToSample.GetValue(i) == columnToSample.GetValue(i - 1)) throw new CommandExecutionException("The datetime column must not contain duplicate values.", this); } insertedRows = new List(); DateTime actPoint = StartTime; int j = 0; IComparable[] row; while (j < columnToSample.TotalValuesCount && actPoint <= ((DateTime)columnToSample.GetValue(columnToSample.TotalValuesCount - 1)) + SampleFrequency) { if (actPoint < (DateTime)columnToSample.GetValue(j)) { row = ColumnGroup.GetEmptyRow(); row[ColumnGroup.SortedColumnIndexes.ElementAt(0)] = actPoint; ColumnGroup.InsertRow(j, row); actPoint = actPoint + SampleFrequency; insertedRows.Add(j); } else if (actPoint == (DateTime)columnToSample.GetValue(j)) actPoint = actPoint + SampleFrequency; j++; } ColumnGroup.FireChanged(); this.ColumnGroup = null; } public override void UndoExecute() { base.UndoExecute(); for (int i = insertedRows.Count - 1; i >= 0; i--) ColumnGroup.DeleteRow(insertedRows[i]); insertedRows.Clear(); ColumnGroup.FireChanged(); this.ColumnGroup = null; } } }