Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16566 was 16566, checked in by gkronber, 5 years ago

#2520: updated HeuristicLab.DataImporter addon for new Persistence (introduced in 3.3.16)

File size: 5.3 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HEAL.Attic;
30
31namespace HeuristicLab.DataImporter.Command {
32  [StorableType("296E9F12-31EC-42F2-9745-B164A5FCE302")]
33  [ViewableCommandInfo("Split Dictionary-style Data", 1, ColumnGroupState.ColumnSelected | ColumnGroupState.Sorted, "Column Commands",
34    Position = 15)]
35  public class SplitDictionaryStyleDataCommand : ColumnGroupCommandWithAffectedColumnsBase {
36    private int addedColumnsCount;
37    [StorableConstructor]
38    protected SplitDictionaryStyleDataCommand(bool deserializing) : base(deserializing) { }
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);
55      if (!AffectedColumns.Except(ColumnGroup.SortedColumnIndexes).Any())
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
58      Dictionary<IComparable, List<ColumnBase>> newColumns = new Dictionary<IComparable, List<ColumnBase>>();
59
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);
62
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;
68      int groupStartIndex = 0;
69      for (int row = 0; row < ColumnGroup.RowCount; row++) {
70        //check if equality value is still the same
71        if (groupValue.CompareTo(groupColumn.GetValue(row)) != 0) {
72          groupValue = groupColumn.GetValue(row);
73          groupStartIndex = row;
74        }
75        splitValue = splitColumn.GetValue(row);
76
77        //create new column if new group value was detected
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          }
88        }
89
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        }
95      }
96
97      ColumnGroup.AddColumns(newColumns.Values.SelectMany(col => col));
98      addedColumnsCount = newColumns.Values.Sum(columns => columns.Count);
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}
Note: See TracBrowser for help on using the repository browser.