[6134] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9615] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6134] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[6133] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Windows.Forms;
|
---|
[9614] | 26 | using HeuristicLab.DataImporter.Command.View;
|
---|
| 27 | using HeuristicLab.DataImporter.Data;
|
---|
[6133] | 28 | using HeuristicLab.DataImporter.Data.CommandBase;
|
---|
| 29 | using HeuristicLab.DataImporter.Data.Model;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[16566] | 31 | using HEAL.Attic;
|
---|
[6133] | 32 |
|
---|
| 33 | namespace HeuristicLab.DataImporter.Command {
|
---|
[16566] | 34 | [StorableType("68DF5755-DE5B-4DED-A73C-D234F503A428")]
|
---|
[6133] | 35 | [ViewableCommandInfoAttribute("Equidistant Sampling", 1, ColumnGroupState.Sorted, "Time Series",
|
---|
| 36 | Position = 0, OptionsView = typeof(TimeBasedCommandView))]
|
---|
| 37 | public class EquidistantSamplingCommand : EquidistantTimeSeriesCommandBase {
|
---|
| 38 | private List<int> insertedRows;
|
---|
| 39 | private DateTimeColumn columnToSample;
|
---|
| 40 |
|
---|
[9614] | 41 | [StorableConstructor]
|
---|
[16567] | 42 | protected EquidistantSamplingCommand(StorableConstructorFlag _) : base(_) { }
|
---|
[6133] | 43 | public EquidistantSamplingCommand(DataSet dataSet, string columnGroupName)
|
---|
| 44 | : base(dataSet, columnGroupName) {
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public override string Description {
|
---|
| 48 | get { return "Equidistant Sampling"; }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public override void Execute() {
|
---|
| 52 | base.Execute();
|
---|
| 53 | if (!(ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.ElementAt(0)) is DateTimeColumn)
|
---|
| 54 | || ColumnGroup.SortedColumnIndexes.Count != 1)
|
---|
| 55 | throw new CommandExecutionException("ColumnGroup mus be sorted a the datetime column.", this);
|
---|
| 56 |
|
---|
| 57 | columnToSample = (DateTimeColumn)ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.ElementAt(0));
|
---|
| 58 | if (columnToSample.SortOrder != SortOrder.Ascending)
|
---|
| 59 | throw new CommandExecutionException("ColumnGroup must be sorted ascending after the selected datetime column.", this);
|
---|
| 60 | if (columnToSample.ContainsNullValues)
|
---|
| 61 | throw new CommandExecutionException("The datetime column must not contain null values.", this);
|
---|
| 62 |
|
---|
| 63 | for (int i = 1; i < columnToSample.TotalValuesCount; i++) {
|
---|
| 64 | if (columnToSample.GetValue(i) == columnToSample.GetValue(i - 1))
|
---|
| 65 | throw new CommandExecutionException("The datetime column must not contain duplicate values.", this);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | insertedRows = new List<int>();
|
---|
| 69 | DateTime actPoint = StartTime;
|
---|
| 70 | int j = 0;
|
---|
| 71 | IComparable[] row;
|
---|
| 72 | while (j < columnToSample.TotalValuesCount &&
|
---|
| 73 | actPoint <= ((DateTime)columnToSample.GetValue(columnToSample.TotalValuesCount - 1)) + SampleFrequency) {
|
---|
| 74 | if (actPoint < (DateTime)columnToSample.GetValue(j)) {
|
---|
| 75 | row = ColumnGroup.GetEmptyRow();
|
---|
| 76 | row[ColumnGroup.SortedColumnIndexes.ElementAt(0)] = actPoint;
|
---|
| 77 | ColumnGroup.InsertRow(j, row);
|
---|
| 78 | actPoint = actPoint + SampleFrequency;
|
---|
| 79 | insertedRows.Add(j);
|
---|
| 80 | } else if (actPoint == (DateTime)columnToSample.GetValue(j))
|
---|
| 81 | actPoint = actPoint + SampleFrequency;
|
---|
| 82 | j++;
|
---|
| 83 | }
|
---|
| 84 | ColumnGroup.FireChanged();
|
---|
| 85 | this.ColumnGroup = null;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | public override void UndoExecute() {
|
---|
| 89 | base.UndoExecute();
|
---|
| 90 | for (int i = insertedRows.Count - 1; i >= 0; i--)
|
---|
| 91 | ColumnGroup.DeleteRow(insertedRows[i]);
|
---|
| 92 | insertedRows.Clear();
|
---|
| 93 | ColumnGroup.FireChanged();
|
---|
| 94 | this.ColumnGroup = null;
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|