Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 4.7 KB
RevLine 
[6134]1#region License Information
2/* HeuristicLab
[7267]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6134]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;
[6133]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.Sorted)
77        throw new CommandExecutionException("ColumnGroup must be sorted to use aggregation commands.", this);
78      ColumnBase col;
79      for (int i = 0; i < AffectedColumns.Length; i++) {
80        col = ColumnGroup.Columns.ElementAt(AffectedColumns[i]);
81        if (col.DataType == typeof(double?) && !ColumnGroup.SortedColumnIndexes.Contains(AffectedColumns[i])) {
82          OldColumns.Add(col);
83          NewColumns.Add(col.CreateCopyOfColumnWithoutValues());
84          Positions.Add(AffectedColumns[i]);
85        }
86      }
87      IComparable keyValue = ColumnGroup.KeyString(0);
88      IComparable aggregatedValue;
89      int startIndex = 0;
90      int endIndex = -1;
91      for (int i = 1; i <= ColumnGroup.RowCount; i++) {
92        if (i == ColumnGroup.RowCount || keyValue.CompareTo(ColumnGroup.KeyString(i)) != 0) {
93          endIndex = i;
94          for (int colIndex = 0; colIndex < OldColumns.Count; colIndex++) {
95            aggregatedValue = AggregationFunction(OldColumns[colIndex], startIndex, endIndex);
96            //add aggregatedValue for each row with the same keyColumnValue
97            for (int j = startIndex; j < endIndex; j++) {
98              if (!changeNullValuesOnly || OldColumns[colIndex].GetValue(j) == null)
99                NewColumns[colIndex].AddValue(aggregatedValue);
100              else
101                NewColumns[colIndex].AddValue(OldColumns[colIndex].GetValue(j));
102            }
103          }
104          if (i != ColumnGroup.RowCount) {
105            startIndex = i;
106            keyValue = ColumnGroup.KeyString(i);
107          }
108        }
109      }
110
111      for (int i = 0; i < Positions.Count; i++) {
112        ColumnGroup.ReplaceColumn(Positions[i], NewColumns[i]);
113        OldColumns[i].Selected = false;
114      }
115      NewColumns.Clear();
116      ColumnGroup.FireChanged();
117      ColumnGroup = null;
118    }
119
120    public override void UndoExecute() {
121      base.UndoExecute();
122      for (int i = 0; i < Positions.Count; i++) {
123        ColumnGroup.ReplaceColumn(Positions[i], OldColumns[i]);
124      }
125      OldColumns.Clear();
126      Positions.Clear();
127      ColumnGroup.FireChanged();
128      ColumnGroup = null;
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.