#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.DataImporter.Data.CommandBase; using HeuristicLab.DataImporter.Data.Model; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.DataImporter.Command { [StorableClass] public abstract class AggregateCommandBase : ColumnGroupCommandWithAffectedColumnsBase { [StorableConstructor] protected AggregateCommandBase(bool deserializing) : base(deserializing) { oldColumns = new List(); newColumns = new List(); positions = new List(); } protected AggregateCommandBase(DataSet dataSet, string columnGroupName, int[] affectedColumns) : base(dataSet, columnGroupName, affectedColumns) { oldColumns = new List(); newColumns = new List(); positions = new List(); changeNullValuesOnly = true; } private List oldColumns; protected List OldColumns { get { return this.oldColumns; } } private List newColumns; protected List NewColumns { get { return this.newColumns; } } private List positions; protected List Positions { get { return this.positions; } } [Storable] private bool changeNullValuesOnly; public bool ChangeNullValueOnly { get { return this.changeNullValuesOnly; } set { this.changeNullValuesOnly = value; } } protected abstract IComparable AggregationFunction(ColumnBase col, int startIndex, int endIndex); public override void Execute() { base.Execute(); if (!ColumnGroup.Columns.Any()) return; if (!ColumnGroup.Sorted) throw new CommandExecutionException("ColumnGroup must be sorted to use aggregation commands.", this); ColumnBase col; for (int i = 0; i < AffectedColumns.Length; i++) { col = ColumnGroup.Columns.ElementAt(AffectedColumns[i]); if (col.DataType == typeof(double?) && !ColumnGroup.SortedColumnIndexes.Contains(AffectedColumns[i])) { OldColumns.Add(col); NewColumns.Add(col.CreateCopyOfColumnWithoutValues()); Positions.Add(AffectedColumns[i]); } } IComparable keyValue = ColumnGroup.KeyString(0); IComparable aggregatedValue; int startIndex = 0; int endIndex = -1; for (int i = 1; i <= ColumnGroup.RowCount; i++) { if (i == ColumnGroup.RowCount || keyValue.CompareTo(ColumnGroup.KeyString(i)) != 0) { endIndex = i; for (int colIndex = 0; colIndex < OldColumns.Count; colIndex++) { aggregatedValue = AggregationFunction(OldColumns[colIndex], startIndex, endIndex); //add aggregatedValue for each row with the same keyColumnValue for (int j = startIndex; j < endIndex; j++) { if (!changeNullValuesOnly || OldColumns[colIndex].GetValue(j) == null) NewColumns[colIndex].AddValue(aggregatedValue); else NewColumns[colIndex].AddValue(OldColumns[colIndex].GetValue(j)); } } if (i != ColumnGroup.RowCount) { startIndex = i; keyValue = ColumnGroup.KeyString(i); } } } for (int i = 0; i < Positions.Count; i++) { ColumnGroup.ReplaceColumn(Positions[i], NewColumns[i]); OldColumns[i].Selected = false; } NewColumns.Clear(); ColumnGroup.FireChanged(); ColumnGroup = null; } public override void UndoExecute() { base.UndoExecute(); for (int i = 0; i < Positions.Count; i++) { ColumnGroup.ReplaceColumn(Positions[i], OldColumns[i]); } OldColumns.Clear(); Positions.Clear(); ColumnGroup.FireChanged(); ColumnGroup = null; } } }