[6134] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7267] | 3 | * Copyright (C) 2002-2012 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.Text;
|
---|
| 26 | using System.Windows.Forms;
|
---|
| 27 | using System.Xml;
|
---|
| 28 | using HeuristicLab.DataImporter.Data.CommandBase;
|
---|
| 29 | using HeuristicLab.DataImporter.Data.Model;
|
---|
| 30 | using HeuristicLab.DataImporter.Data;
|
---|
| 31 | using HeuristicLab.DataImporter.Command.View;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.DataImporter.Command {
|
---|
| 35 | [StorableClass]
|
---|
| 36 | [ViewableCommandInfoAttribute("Delete not equidistant rows", 1, ColumnGroupState.Sorted, "Time Series",
|
---|
| 37 | Position = 1, OptionsView = typeof(TimeBasedCommandView))]
|
---|
| 38 | public class DeleteNotEquidistantRowsCommand : EquidistantTimeSeriesCommandBase {
|
---|
| 39 | private DateTimeColumn columnToSample;
|
---|
| 40 | private List<ColumnBase> oldColumns;
|
---|
| 41 | private ICollection<int> oldSortedColumnIndices;
|
---|
| 42 | private IEnumerable<SortOrder> oldSortOrder;
|
---|
| 43 |
|
---|
| 44 | private DeleteNotEquidistantRowsCommand()
|
---|
| 45 | : base(null, string.Empty) {
|
---|
| 46 | oldColumns = new List<ColumnBase>();
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public DeleteNotEquidistantRowsCommand(DataSet dataSet, string columnGroupName)
|
---|
| 50 | : base(dataSet, columnGroupName) {
|
---|
| 51 | oldColumns = new List<ColumnBase>();
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public override string Description {
|
---|
| 55 | get { return "Delete not equidistant rows"; }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public override void Execute() {
|
---|
| 59 | base.Execute();
|
---|
| 60 | if (!(ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.ElementAt(0)) is DateTimeColumn)
|
---|
| 61 | || ColumnGroup.SortedColumnIndexes.Count != 1)
|
---|
| 62 | throw new CommandExecutionException("ColumnGroup must be sorted after a datetime column.", this);
|
---|
| 63 | columnToSample = (DateTimeColumn)ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.ElementAt(0));
|
---|
| 64 | if (columnToSample.SortOrder != SortOrder.Ascending)
|
---|
| 65 | throw new CommandExecutionException("ColumnGroup must be sorted ascending after the selected datetime column.", this);
|
---|
| 66 | if (columnToSample.ContainsNullValues)
|
---|
| 67 | throw new CommandExecutionException("The datetime column must not contain null values.", this);
|
---|
| 68 |
|
---|
| 69 | oldSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
|
---|
| 70 | oldSortOrder = ColumnGroup.SortOrdersForColumns.ToList();
|
---|
| 71 |
|
---|
| 72 | DateTime value;
|
---|
| 73 | DateTime actPoint = StartTime;
|
---|
| 74 | ColumnGroup tempColumnGroup = new ColumnGroup();
|
---|
| 75 | List<int> positions = new List<int>();
|
---|
| 76 |
|
---|
| 77 | for (int i = 0; i < ColumnGroup.RowCount; i++) {
|
---|
| 78 | value = (DateTime)columnToSample.GetValue(i);
|
---|
| 79 | while (actPoint < value)
|
---|
| 80 | actPoint += SampleFrequency;
|
---|
| 81 | if (actPoint == value) {
|
---|
| 82 | positions.Add(i);
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | foreach (ColumnBase x in ColumnGroup.Columns) {
|
---|
| 87 | oldColumns.Add(x);
|
---|
| 88 | tempColumnGroup.AddColumn(x.CreateCopyOfColumnWithoutValues());
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | for (int i = 0; i < positions.Count; i++)
|
---|
| 92 | tempColumnGroup.AddRow(ColumnGroup.GetRow(positions[i]));
|
---|
| 93 |
|
---|
| 94 | for (int i = 0; i < ColumnGroup.Columns.Count(); i++)
|
---|
| 95 | ColumnGroup.ReplaceColumn(i, tempColumnGroup.Columns.ElementAt(i));
|
---|
| 96 |
|
---|
| 97 | ColumnGroup.SortOrdersForColumns = oldSortOrder;
|
---|
| 98 | ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
|
---|
| 99 | ColumnGroup.FireChanged();
|
---|
| 100 | this.ColumnGroup = null;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | public override void UndoExecute() {
|
---|
| 104 | base.UndoExecute();
|
---|
| 105 | for (int i = 0; i < ColumnGroup.Columns.Count(); i++)
|
---|
| 106 | ColumnGroup.ReplaceColumn(i, oldColumns[i]);
|
---|
| 107 | ColumnGroup.SortOrdersForColumns = oldSortOrder;
|
---|
| 108 | ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
|
---|
| 109 | oldSortedColumnIndices = null;
|
---|
| 110 | oldSortOrder = null;
|
---|
| 111 | oldColumns.Clear();
|
---|
| 112 | ColumnGroup.FireChanged();
|
---|
| 113 | this.ColumnGroup = null;
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | }
|
---|