#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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 System.Text; using HeuristicLab.DataImporter.Data; using HeuristicLab.DataImporter.Data.CommandBase; using HeuristicLab.DataImporter.Data.Model; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.DataImporter.Command { [StorableClass] [ViewableCommandInfo("Split Dictionary-style Data", 1, ColumnGroupState.ColumnSelected | ColumnGroupState.Sorted, "Column Commands", Position = 15, SelectedColumns = 2)] public class SplitDictionaryStyleDataCommand : ColumnGroupCommandWithAffectedColumnsBase { private int addedColumnsCount; private SplitDictionaryStyleDataCommand() : base(null, string.Empty, null) { } public SplitDictionaryStyleDataCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns) : base(dataSet, columnGroupName, affectedColumns) { } public override string Description { get { return "Split dictionary style data"; } } public override void Execute() { base.Execute(); Dictionary newColumns = new Dictionary(); ; if (ColumnGroup.SortedColumnIndexes.Except(AffectedColumns).Count() != 1 && ColumnGroup.SortedColumnIndexes.Except(AffectedColumns).FirstOrDefault() == ColumnGroup.SortedColumnIndexes.ElementAt(0)) throw new CommandExecutionException("Exactly one column must be sorted as first sorted column and not selected to define the equality of the rows.", this); if (AffectedColumns.Intersect(ColumnGroup.SortedColumnIndexes).Count() != 1) throw new CommandExecutionException("Exactly one column must be sorted and selected to define the column which holds the grouping value.", this); if (AffectedColumns.Except(ColumnGroup.SortedColumnIndexes).Count() != 1) throw new CommandExecutionException("At least one column must not be sorted but selected to define the column which holds the values for new columns.", this); ColumnBase equalityColumn = ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.Except(AffectedColumns).ElementAt(0)); if (equalityColumn.ContainsNullValues) throw new CommandExecutionException("Column which defines the equality (" + equalityColumn.Name + ") must not contain NULL values.", this); ColumnBase groupColumn = ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.Intersect(AffectedColumns).ElementAt(0)); ColumnBase valueColumn = ColumnGroup.GetColumn(AffectedColumns.Except(ColumnGroup.SortedColumnIndexes).ElementAt(0)); IComparable equalityValue = equalityColumn.GetValue(0); ; IComparable groupValue; int groupStartIndex = 0; for (int row = 0; row < ColumnGroup.RowCount; row++) { //check if equality value is still the same if (equalityValue.CompareTo(equalityColumn.GetValue(row)) != 0) { equalityValue = equalityColumn.GetValue(row); groupStartIndex = row; } groupValue = groupColumn.GetValue(row); //create new column if new group value was detected if (!newColumns.ContainsKey(groupValue)) { newColumns.Add(groupValue, valueColumn.CreateCopyOfColumnWithoutValues()); newColumns[groupValue].Resize(ColumnGroup.RowCount); newColumns[groupValue].Name += "_" + groupValue; } for (int i = groupStartIndex; i < row; i++) newColumns[groupValue].ChangeValue(i, valueColumn.GetValue(row)); for (int i = row; i < ColumnGroup.RowCount && equalityValue.CompareTo(equalityColumn.GetValue(i)) == 0; i++) newColumns[groupValue].ChangeValue(i, valueColumn.GetValue(row)); } ColumnGroup.AddColumns(newColumns.Values); addedColumnsCount = newColumns.Count; this.ColumnGroup.FireChanged(); this.ColumnGroup = null; } public override void UndoExecute() { base.UndoExecute(); for (int i = 0; i < addedColumnsCount; i++) ColumnGroup.RemoveColumn(ColumnGroup.Columns.Count() - 1); this.ColumnGroup.FireChanged(); this.ColumnGroup = null; } } }