Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/TimeSeries/ChangeDateTimeColumnToDoubleColumn.cs @ 16566

Last change on this file since 16566 was 16566, checked in by gkronber, 5 years ago

#2520: updated HeuristicLab.DataImporter addon for new Persistence (introduced in 3.3.16)

File size: 5.0 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HEAL.Attic;
32
33namespace HeuristicLab.DataImporter.Command {
34  [StorableType("F5336C3B-3975-4A36-B3AA-5867C9E18BE3")]
35  [ViewableCommandInfoAttribute("Change DateTimeColumn to DoubleColumn", 1, ColumnGroupState.DateTimeColumnSelected, "Time Series",
36Position = 5, OptionsView = typeof(ChangeDateTimeCommandView))]
37  public class ChangeDateTimeColumnToDoubleColumn : ColumnGroupCommandWithAffectedColumnsBase {
38    private List<ColumnBase> newColumns;
39    private List<ColumnBase> oldColumns;
40    private List<int> positions;
41    private ICollection<int> oldSortedColumnIndexes;
42    private IEnumerable<SortOrder> oldSortOrder;
43
44    [StorableConstructor]
45    protected ChangeDateTimeColumnToDoubleColumn(bool deserializing)
46      : base(deserializing) {
47      newColumns = new List<ColumnBase>();
48      oldColumns = new List<ColumnBase>();
49      positions = new List<int>();
50    }
51
52    public ChangeDateTimeColumnToDoubleColumn(DataSet dataSet, string columnGroupName, int[] affectedColumns)
53      : base(dataSet, columnGroupName, affectedColumns) {
54      newColumns = new List<ColumnBase>();
55      oldColumns = new List<ColumnBase>();
56      positions = new List<int>();
57    }
58
59    public override string Description {
60      get { return "Changes the date values to double by substracting from a given date and converting the result to the given unit."; }
61    }
62
63    [Storable]
64    private DateTimeSpan frequency;
65    public DateTimeSpan Frequency {
66      get { return this.frequency; }
67      set { this.frequency = value; }
68    }
69
70    [Storable]
71    private DateTime startTime;
72    public DateTime StartTime {
73      get { return this.startTime; }
74      set { this.startTime = value; }
75    }
76
77    public override void Execute() {
78      base.Execute();
79      oldSortOrder = ColumnGroup.SortOrdersForColumns.ToList();
80      oldSortedColumnIndexes = new List<int>(ColumnGroup.SortedColumnIndexes);
81
82      DateTimeColumn oldColumn;
83      DoubleColumn newColumn;
84      DateTime? value;
85      double newValue;
86      for (int j = 0; j < AffectedColumns.Length; j++) {
87        oldColumn = (DateTimeColumn)ColumnGroup.Columns.ElementAt(AffectedColumns[j]);
88        positions.Add(AffectedColumns[j]);
89        oldColumns.Add(oldColumn);
90
91        newColumn = new DoubleColumn(oldColumn.Name, oldColumn.TotalValuesCount);
92        for (int i = 0; i < oldColumn.TotalValuesCount; i++) {
93          value = (DateTime?)oldColumn.GetValue(i);
94          if (!value.HasValue)
95            newColumn.AddValue(null);
96          else {
97            TimeSpan elapsedTime = value.Value - startTime;
98            if (frequency.Seconds == 1)
99              newValue = elapsedTime.TotalSeconds;
100            else if (frequency.Minutes == 1)
101              newValue = elapsedTime.TotalMinutes;
102            else if (frequency.Hours == 1)
103              newValue = elapsedTime.TotalHours;
104            else if (frequency.Days == 1)
105              newValue = elapsedTime.TotalDays;
106            else
107              throw new CommandExecutionException("Unit for ChangeDateTimeColumnToDoubleColumn not correclty set.", this);
108            newColumn.AddValue(newValue);
109          }
110        }
111        newColumns.Add(newColumn);
112      }
113
114      for (int i = 0; i < positions.Count; i++)
115        ColumnGroup.ReplaceColumn(positions[i], newColumns[i]);
116      ColumnGroup.FireChanged();
117    }
118
119    public override void UndoExecute() {
120      base.UndoExecute();
121
122      for (int i = 0; i < positions.Count; i++)
123        ColumnGroup.ReplaceColumn(positions[i], oldColumns[i]);
124
125      ColumnGroup.SortOrdersForColumns = oldSortOrder;
126      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndexes;
127      oldSortedColumnIndexes = null;
128      oldSortOrder = null;
129      oldColumns.Clear();
130      newColumns.Clear();
131      positions.Clear();
132
133      ColumnGroup.FireChanged();
134    }
135
136  }
137}
Note: See TracBrowser for help on using the repository browser.