Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/Aggregation/AggregateCommandBase.cs @ 13243

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

#1734: Updated copyright information in all DataImporter classes.

File size: 4.7 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 HeuristicLab.DataImporter.Data.CommandBase;
26using HeuristicLab.DataImporter.Data.Model;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.DataImporter.Command {
30  [StorableClass]
31  public abstract class AggregateCommandBase : ColumnGroupCommandWithAffectedColumnsBase {
32    [StorableConstructor]
33    protected AggregateCommandBase(bool deserializing)
34      : base(deserializing) {
35      oldColumns = new List<ColumnBase>();
36      newColumns = new List<ColumnBase>();
37      positions = new List<int>();
38    }
39
40    protected AggregateCommandBase(DataSet dataSet, string columnGroupName, int[] affectedColumns)
41      : base(dataSet, columnGroupName, affectedColumns) {
42      oldColumns = new List<ColumnBase>();
43      newColumns = new List<ColumnBase>();
44      positions = new List<int>();
45      changeNullValuesOnly = true;
46    }
47
48    private List<ColumnBase> oldColumns;
49    protected List<ColumnBase> OldColumns {
50      get { return this.oldColumns; }
51    }
52
53    private List<ColumnBase> newColumns;
54    protected List<ColumnBase> NewColumns {
55      get { return this.newColumns; }
56    }
57
58    private List<int> positions;
59    protected List<int> Positions {
60      get { return this.positions; }
61    }
62
63    [Storable]
64    private bool changeNullValuesOnly;
65    public bool ChangeNullValueOnly {
66      get { return this.changeNullValuesOnly; }
67      set { this.changeNullValuesOnly = value; }
68    }
69
70    protected abstract IComparable AggregationFunction(ColumnBase col, int startIndex, int endIndex);
71
72    public override void Execute() {
73      base.Execute();
74      if (!ColumnGroup.Columns.Any()) return;
75      if (!ColumnGroup.Sorted)
76        throw new CommandExecutionException("ColumnGroup must be sorted to use aggregation commands.", this);
77      ColumnBase col;
78      for (int i = 0; i < AffectedColumns.Length; i++) {
79        col = ColumnGroup.Columns.ElementAt(AffectedColumns[i]);
80        if (col.DataType == typeof(double?) && !ColumnGroup.SortedColumnIndexes.Contains(AffectedColumns[i])) {
81          OldColumns.Add(col);
82          NewColumns.Add(col.CreateCopyOfColumnWithoutValues());
83          Positions.Add(AffectedColumns[i]);
84        }
85      }
86      IComparable keyValue = ColumnGroup.KeyString(0);
87      IComparable aggregatedValue;
88      int startIndex = 0;
89      int endIndex = -1;
90      for (int i = 1; i <= ColumnGroup.RowCount; i++) {
91        if (i == ColumnGroup.RowCount || keyValue.CompareTo(ColumnGroup.KeyString(i)) != 0) {
92          endIndex = i;
93          for (int colIndex = 0; colIndex < OldColumns.Count; colIndex++) {
94            aggregatedValue = AggregationFunction(OldColumns[colIndex], startIndex, endIndex);
95            //add aggregatedValue for each row with the same keyColumnValue
96            for (int j = startIndex; j < endIndex; j++) {
97              if (!changeNullValuesOnly || OldColumns[colIndex].GetValue(j) == null)
98                NewColumns[colIndex].AddValue(aggregatedValue);
99              else
100                NewColumns[colIndex].AddValue(OldColumns[colIndex].GetValue(j));
101            }
102          }
103          if (i != ColumnGroup.RowCount) {
104            startIndex = i;
105            keyValue = ColumnGroup.KeyString(i);
106          }
107        }
108      }
109
110      for (int i = 0; i < Positions.Count; i++) {
111        ColumnGroup.ReplaceColumn(Positions[i], NewColumns[i]);
112        OldColumns[i].Selected = false;
113      }
114      NewColumns.Clear();
115      ColumnGroup.FireChanged();
116      ColumnGroup = null;
117    }
118
119    public override void UndoExecute() {
120      base.UndoExecute();
121      for (int i = 0; i < Positions.Count; i++) {
122        ColumnGroup.ReplaceColumn(Positions[i], OldColumns[i]);
123      }
124      OldColumns.Clear();
125      Positions.Clear();
126      ColumnGroup.FireChanged();
127      ColumnGroup = null;
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.