#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; using HeuristicLab.DataImporter.Data.CommandBase; using HeuristicLab.DataImporter.Data.Model; using HEAL.Attic; namespace HeuristicLab.DataImporter.Command { [StorableType("296E9F12-31EC-42F2-9745-B164A5FCE302")] [ViewableCommandInfo("Split Dictionary-style Data", 1, ColumnGroupState.ColumnSelected | ColumnGroupState.Sorted, "Column Commands", Position = 15)] public class SplitDictionaryStyleDataCommand : ColumnGroupCommandWithAffectedColumnsBase { private int addedColumnsCount; [StorableConstructor] protected SplitDictionaryStyleDataCommand(StorableConstructorFlag _) : base(_) { } 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(); 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).Any()) 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); Dictionary> newColumns = new Dictionary>(); ColumnBase groupColumn = ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.Except(AffectedColumns).First()); if (groupColumn.ContainsNullValues) throw new CommandExecutionException("Column which defines the equality (" + groupColumn.Name + ") must not contain NULL values.", this); ColumnBase splitColumn = ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.Intersect(AffectedColumns).First()); var valueColumns = AffectedColumns.Except(ColumnGroup.SortedColumnIndexes).Select(index => ColumnGroup.GetColumn(index)).ToList(); IComparable groupValue = groupColumn.GetValue(0); ; IComparable splitValue; int groupStartIndex = 0; for (int row = 0; row < ColumnGroup.RowCount; row++) { //check if equality value is still the same if (groupValue.CompareTo(groupColumn.GetValue(row)) != 0) { groupValue = groupColumn.GetValue(row); groupStartIndex = row; } splitValue = splitColumn.GetValue(row); //create new column if new group value was detected if (!newColumns.ContainsKey(splitValue)) { newColumns.Add(splitValue, new List(valueColumns.Count)); int i = 0; foreach (var valueColumn in valueColumns) { var newColumn = valueColumn.CreateCopyOfColumnWithoutValues(valueColumn.TotalValuesCount); newColumn.Name += "_" + splitValue; newColumn.Resize(ColumnGroup.RowCount); newColumns[splitValue].Add(newColumn); i++; } } for (int i = groupStartIndex; i < ColumnGroup.RowCount && groupValue.CompareTo(groupColumn.GetValue(i)) == 0; i++) { for (int col = 0; col < valueColumns.Count; col++) newColumns[splitValue][col].ChangeValue(i, valueColumns[col].GetValue(row)); } } ColumnGroup.AddColumns(newColumns.Values.SelectMany(col => col)); addedColumnsCount = newColumns.Values.Sum(columns => columns.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; } } }