Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/Aggregation/AggregateCommandBase.cs @ 17029

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

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

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