Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/TimeSeries/DeleteNotEquidistantRowsCommand.cs @ 14349

Last change on this file since 14349 was 9615, checked in by mkommend, 12 years ago

#1734: Updated copyright information in all DataImporter classes.

File size: 4.5 KB
RevLine 
[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
22using System;
[6133]23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
[9614]26using HeuristicLab.DataImporter.Command.View;
27using HeuristicLab.DataImporter.Data;
[6133]28using HeuristicLab.DataImporter.Data.CommandBase;
29using HeuristicLab.DataImporter.Data.Model;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.DataImporter.Command {
33  [StorableClass]
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
[9614]42    [StorableConstructor]
43    protected DeleteNotEquidistantRowsCommand(bool deserializing)
44      : base(deserializing) {
[6133]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}
Note: See TracBrowser for help on using the repository browser.