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