Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/TimeSeries/EquidistantSamplingCommand.cs @ 6212

Last change on this file since 6212 was 6134, checked in by gkronber, 14 years ago

#1471: added plugin for DbExplorer interfaces, deleted .resx files, set svn:ignore properties, and added license header

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Text;
26using System.Windows.Forms;
27using System.Xml;
28using HeuristicLab.DataImporter.Data.CommandBase;
29using HeuristicLab.DataImporter.Data.Model;
30using HeuristicLab.DataImporter.Data;
31using HeuristicLab.DataImporter.Command.View;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34namespace HeuristicLab.DataImporter.Command {
35  [StorableClass]
36  [ViewableCommandInfoAttribute("Equidistant Sampling", 1, ColumnGroupState.Sorted, "Time Series",
37Position = 0, OptionsView = typeof(TimeBasedCommandView))]
38  public class EquidistantSamplingCommand : EquidistantTimeSeriesCommandBase {
39    private List<int> insertedRows;
40    private DateTimeColumn columnToSample;
41
42    private EquidistantSamplingCommand()
43      : base(null, string.Empty) {
44    }
45
46    public EquidistantSamplingCommand(DataSet dataSet, string columnGroupName)
47      : base(dataSet, columnGroupName) {
48    }
49
50    public override string Description {
51      get { return "Equidistant Sampling"; }
52    }
53
54    public override void Execute() {
55      base.Execute();
56      if (!(ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.ElementAt(0)) is DateTimeColumn)
57    || ColumnGroup.SortedColumnIndexes.Count != 1)
58        throw new CommandExecutionException("ColumnGroup mus be sorted a the datetime column.", this);
59
60      columnToSample = (DateTimeColumn)ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.ElementAt(0));
61      if (columnToSample.SortOrder != SortOrder.Ascending)
62        throw new CommandExecutionException("ColumnGroup must be sorted ascending after the selected datetime column.", this);
63      if (columnToSample.ContainsNullValues)
64        throw new CommandExecutionException("The datetime column must not contain null values.", this);
65
66      for (int i = 1; i < columnToSample.TotalValuesCount; i++) {
67        if (columnToSample.GetValue(i) == columnToSample.GetValue(i - 1))
68          throw new CommandExecutionException("The datetime column must not contain duplicate values.", this);
69      }
70
71      insertedRows = new List<int>();
72      DateTime actPoint = StartTime;
73      int j = 0;
74      IComparable[] row;
75      while (j < columnToSample.TotalValuesCount &&
76        actPoint <= ((DateTime)columnToSample.GetValue(columnToSample.TotalValuesCount - 1)) + SampleFrequency) {
77        if (actPoint < (DateTime)columnToSample.GetValue(j)) {
78          row = ColumnGroup.GetEmptyRow();
79          row[ColumnGroup.SortedColumnIndexes.ElementAt(0)] = actPoint;
80          ColumnGroup.InsertRow(j, row);
81          actPoint = actPoint + SampleFrequency;
82          insertedRows.Add(j);
83        } else if (actPoint == (DateTime)columnToSample.GetValue(j))
84          actPoint = actPoint + SampleFrequency;
85        j++;
86      }
87      ColumnGroup.FireChanged();
88      this.ColumnGroup = null;
89    }
90
91    public override void UndoExecute() {
92      base.UndoExecute();
93      for (int i = insertedRows.Count - 1; i >= 0; i--)
94        ColumnGroup.DeleteRow(insertedRows[i]);
95      insertedRows.Clear();
96      ColumnGroup.FireChanged();
97      this.ColumnGroup = null;
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.