Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/TimeSeries/ChangeDateTimeColumnToDoubleColumn.cs @ 7267

Last change on this file since 7267 was 7267, checked in by gkronber, 12 years ago

#1734 updated copyright year in all files of the DataImporter branch

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 HeuristicLab.DataImporter.Data.CommandBase;
27using HeuristicLab.DataImporter.Data.Model;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.DataImporter.Data;
30using HeuristicLab.DataImporter.Command.View;
31using System.Windows.Forms;
32
33namespace HeuristicLab.DataImporter.Command {
34  [StorableClass]
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    private ChangeDateTimeColumnToDoubleColumn()
45      : base(null, string.Empty, null) {
46      newColumns = new List<ColumnBase>();
47      oldColumns = new List<ColumnBase>();
48      positions = new List<int>();
49    }
50
51    public ChangeDateTimeColumnToDoubleColumn(DataSet dataSet, string columnGroupName, int[] affectedColumns)
52      : base(dataSet, columnGroupName, affectedColumns) {
53      newColumns = new List<ColumnBase>();
54      oldColumns = new List<ColumnBase>();
55      positions = new List<int>();
56    }
57
58    public override string Description {
59      get { return "Changes the date values to double by substracting from a given date and converting the result to the given unit."; }
60    }
61
62    [Storable]
63    private DateTimeSpan frequency;
64    public DateTimeSpan Frequency {
65      get { return this.frequency; }
66      set { this.frequency = value; }
67    }
68
69    [Storable]
70    private DateTime startTime;
71    public DateTime StartTime {
72      get { return this.startTime; }
73      set { this.startTime = value; }
74    }
75
76    public override void Execute() {
77      base.Execute();
78      oldSortOrder = ColumnGroup.SortOrdersForColumns.ToList();
79      oldSortedColumnIndexes = new List<int>(ColumnGroup.SortedColumnIndexes);
80
81      DateTimeColumn oldColumn;
82      DoubleColumn newColumn;
83      DateTime? value;
84      double newValue;
85      for (int j = 0; j < AffectedColumns.Length; j++) {
86        oldColumn = (DateTimeColumn)ColumnGroup.Columns.ElementAt(AffectedColumns[j]);
87        positions.Add(AffectedColumns[j]);
88        oldColumns.Add(oldColumn);
89
90        newColumn = new DoubleColumn(oldColumn.Name, oldColumn.TotalValuesCount);
91        for (int i = 0; i < oldColumn.TotalValuesCount; i++) {
92          value = (DateTime?)oldColumn.GetValue(i);
93          if (!value.HasValue)
94            newColumn.AddValue(null);
95          else {
96            TimeSpan elapsedTime = value.Value - startTime;
97            if (frequency.Seconds == 1)
98              newValue = elapsedTime.TotalSeconds;
99            else if (frequency.Minutes == 1)
100              newValue = elapsedTime.TotalMinutes;
101            else if (frequency.Hours == 1)
102              newValue = elapsedTime.TotalHours;
103            else if (frequency.Days == 1)
104              newValue = elapsedTime.TotalDays;
105            else
106              throw new CommandExecutionException("Unit for ChangeDateTimeColumnToDoubleColumn not correclty set.", this);
107            newColumn.AddValue(newValue);
108          }
109        }
110        newColumns.Add(newColumn);
111      }
112
113      for (int i = 0; i < positions.Count; i++)
114        ColumnGroup.ReplaceColumn(positions[i], newColumns[i]);
115      ColumnGroup.FireChanged();
116    }
117
118    public override void UndoExecute() {
119      base.UndoExecute();
120
121      for (int i = 0; i < positions.Count; i++)
122        ColumnGroup.ReplaceColumn(positions[i], oldColumns[i]);
123
124      ColumnGroup.SortOrdersForColumns = oldSortOrder;
125      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndexes;
126      oldSortedColumnIndexes = null;
127      oldSortOrder = null;
128      oldColumns.Clear();
129      newColumns.Clear();
130      positions.Clear();
131
132      ColumnGroup.FireChanged();
133    }
134
135  }
136}
Note: See TracBrowser for help on using the repository browser.