Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/TimeSeries/DeleteNotEquidistantRowsCommand.cs @ 18209

Last change on this file since 18209 was 16994, checked in by gkronber, 6 years ago

#2520 Update plugin dependencies and references for HL.DataImporter for new persistence

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