Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

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