Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9273 was 7625, checked in by mkommend, 13 years ago

#1734: merged changes in data importer from the heureka rep

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