[6134] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9615] | 3 | * Copyright (C) 2002-2013 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 |
|
---|
| 22 | using System;
|
---|
[6133] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.DataImporter.Data;
|
---|
| 26 | using HeuristicLab.DataImporter.Data.CommandBase;
|
---|
| 27 | using HeuristicLab.DataImporter.Data.Model;
|
---|
[16566] | 28 | using HEAL.Attic;
|
---|
[6133] | 29 |
|
---|
| 30 | namespace HeuristicLab.DataImporter.Command {
|
---|
[16566] | 31 | [StorableType("296E9F12-31EC-42F2-9745-B164A5FCE302")]
|
---|
[6133] | 32 | [ViewableCommandInfo("Split Dictionary-style Data", 1, ColumnGroupState.ColumnSelected | ColumnGroupState.Sorted, "Column Commands",
|
---|
[8387] | 33 | Position = 15)]
|
---|
[6133] | 34 | public class SplitDictionaryStyleDataCommand : ColumnGroupCommandWithAffectedColumnsBase {
|
---|
| 35 | private int addedColumnsCount;
|
---|
[9614] | 36 | [StorableConstructor]
|
---|
[16567] | 37 | protected SplitDictionaryStyleDataCommand(StorableConstructorFlag _) : base(_) { }
|
---|
[6133] | 38 |
|
---|
| 39 | public SplitDictionaryStyleDataCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
|
---|
| 40 | : base(dataSet, columnGroupName, affectedColumns) {
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public override string Description {
|
---|
| 44 | get { return "Split dictionary style data"; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public override void Execute() {
|
---|
| 48 | base.Execute();
|
---|
| 49 | if (ColumnGroup.SortedColumnIndexes.Except(AffectedColumns).Count() != 1 &&
|
---|
| 50 | ColumnGroup.SortedColumnIndexes.Except(AffectedColumns).FirstOrDefault() == ColumnGroup.SortedColumnIndexes.ElementAt(0))
|
---|
| 51 | throw new CommandExecutionException("Exactly one column must be sorted as first sorted column and not selected to define the equality of the rows.", this);
|
---|
| 52 | if (AffectedColumns.Intersect(ColumnGroup.SortedColumnIndexes).Count() != 1)
|
---|
| 53 | throw new CommandExecutionException("Exactly one column must be sorted and selected to define the column which holds the grouping value.", this);
|
---|
[8387] | 54 | if (!AffectedColumns.Except(ColumnGroup.SortedColumnIndexes).Any())
|
---|
[6133] | 55 | 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);
|
---|
| 56 |
|
---|
[8387] | 57 | Dictionary<IComparable, List<ColumnBase>> newColumns = new Dictionary<IComparable, List<ColumnBase>>();
|
---|
[6133] | 58 |
|
---|
[8387] | 59 | ColumnBase groupColumn = ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.Except(AffectedColumns).First());
|
---|
| 60 | if (groupColumn.ContainsNullValues) throw new CommandExecutionException("Column which defines the equality (" + groupColumn.Name + ") must not contain NULL values.", this);
|
---|
[6133] | 61 |
|
---|
[8387] | 62 | ColumnBase splitColumn = ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.Intersect(AffectedColumns).First());
|
---|
| 63 | var valueColumns = AffectedColumns.Except(ColumnGroup.SortedColumnIndexes).Select(index => ColumnGroup.GetColumn(index)).ToList();
|
---|
| 64 |
|
---|
| 65 | IComparable groupValue = groupColumn.GetValue(0); ;
|
---|
| 66 | IComparable splitValue;
|
---|
[6133] | 67 | int groupStartIndex = 0;
|
---|
| 68 | for (int row = 0; row < ColumnGroup.RowCount; row++) {
|
---|
| 69 | //check if equality value is still the same
|
---|
[8387] | 70 | if (groupValue.CompareTo(groupColumn.GetValue(row)) != 0) {
|
---|
| 71 | groupValue = groupColumn.GetValue(row);
|
---|
[6133] | 72 | groupStartIndex = row;
|
---|
| 73 | }
|
---|
[8387] | 74 | splitValue = splitColumn.GetValue(row);
|
---|
| 75 |
|
---|
[6133] | 76 | //create new column if new group value was detected
|
---|
[8387] | 77 | if (!newColumns.ContainsKey(splitValue)) {
|
---|
| 78 | newColumns.Add(splitValue, new List<ColumnBase>(valueColumns.Count));
|
---|
| 79 | int i = 0;
|
---|
| 80 | foreach (var valueColumn in valueColumns) {
|
---|
| 81 | var newColumn = valueColumn.CreateCopyOfColumnWithoutValues(valueColumn.TotalValuesCount);
|
---|
| 82 | newColumn.Name += "_" + splitValue;
|
---|
| 83 | newColumn.Resize(ColumnGroup.RowCount);
|
---|
| 84 | newColumns[splitValue].Add(newColumn);
|
---|
| 85 | i++;
|
---|
| 86 | }
|
---|
[6133] | 87 | }
|
---|
| 88 |
|
---|
[8387] | 89 |
|
---|
| 90 | for (int i = groupStartIndex; i < ColumnGroup.RowCount && groupValue.CompareTo(groupColumn.GetValue(i)) == 0; i++) {
|
---|
| 91 | for (int col = 0; col < valueColumns.Count; col++)
|
---|
| 92 | newColumns[splitValue][col].ChangeValue(i, valueColumns[col].GetValue(row));
|
---|
| 93 | }
|
---|
[6133] | 94 | }
|
---|
| 95 |
|
---|
[8387] | 96 | ColumnGroup.AddColumns(newColumns.Values.SelectMany(col => col));
|
---|
| 97 | addedColumnsCount = newColumns.Values.Sum(columns => columns.Count);
|
---|
[6133] | 98 | this.ColumnGroup.FireChanged();
|
---|
| 99 | this.ColumnGroup = null;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | public override void UndoExecute() {
|
---|
| 103 | base.UndoExecute();
|
---|
| 104 | for (int i = 0; i < addedColumnsCount; i++)
|
---|
| 105 | ColumnGroup.RemoveColumn(ColumnGroup.Columns.Count() - 1);
|
---|
| 106 | this.ColumnGroup.FireChanged();
|
---|
| 107 | this.ColumnGroup = null;
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|