Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeColumnGroup/SplitDictionaryStyleDataCommand.cs @ 17717

Last change on this file since 17717 was 16994, checked in by gkronber, 6 years ago

#2520 Update plugin dependencies and references for HL.DataImporter for new persistence

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.DataImporter.Data;
26using HeuristicLab.DataImporter.Data.CommandBase;
27using HeuristicLab.DataImporter.Data.Model;
28using HEAL.Attic;
29
30namespace HeuristicLab.DataImporter.Command {
31  [StorableType("296E9F12-31EC-42F2-9745-B164A5FCE302")]
32  [ViewableCommandInfo("Split Dictionary-style Data", 1, ColumnGroupState.ColumnSelected | ColumnGroupState.Sorted, "Column Commands",
33    Position = 15)]
34  public class SplitDictionaryStyleDataCommand : ColumnGroupCommandWithAffectedColumnsBase {
35    private int addedColumnsCount;
36    [StorableConstructor]
37    protected SplitDictionaryStyleDataCommand(StorableConstructorFlag _) : base(_) { }
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);
54      if (!AffectedColumns.Except(ColumnGroup.SortedColumnIndexes).Any())
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
57      Dictionary<IComparable, List<ColumnBase>> newColumns = new Dictionary<IComparable, List<ColumnBase>>();
58
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);
61
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;
67      int groupStartIndex = 0;
68      for (int row = 0; row < ColumnGroup.RowCount; row++) {
69        //check if equality value is still the same
70        if (groupValue.CompareTo(groupColumn.GetValue(row)) != 0) {
71          groupValue = groupColumn.GetValue(row);
72          groupStartIndex = row;
73        }
74        splitValue = splitColumn.GetValue(row);
75
76        //create new column if new group value was detected
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          }
87        }
88
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        }
94      }
95
96      ColumnGroup.AddColumns(newColumns.Values.SelectMany(col => col));
97      addedColumnsCount = newColumns.Values.Sum(columns => columns.Count);
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}
Note: See TracBrowser for help on using the repository browser.