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