Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeColumnGroup/SplitDictionaryStyleDataCommand.cs @ 8541

Last change on this file since 8541 was 8387, checked in by mkommend, 12 years ago

#1908: Updated SplitDictionaryStyleDataCommand to allow splitting multiple columns. Additionally made minor code improvements in the DataImporter.

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
29
30namespace HeuristicLab.DataImporter.Command {
31  [StorableClass]
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
37    private SplitDictionaryStyleDataCommand()
38      : base(null, string.Empty, null) {
39    }
40
41    public SplitDictionaryStyleDataCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
42      : base(dataSet, columnGroupName, affectedColumns) {
43    }
44
45    public override string Description {
46      get { return "Split dictionary style data"; }
47    }
48
49    public override void Execute() {
50      base.Execute();
51      if (ColumnGroup.SortedColumnIndexes.Except(AffectedColumns).Count() != 1 &&
52        ColumnGroup.SortedColumnIndexes.Except(AffectedColumns).FirstOrDefault() == ColumnGroup.SortedColumnIndexes.ElementAt(0))
53        throw new CommandExecutionException("Exactly one column must be sorted as first sorted column and not selected to define the equality of the rows.", this);
54      if (AffectedColumns.Intersect(ColumnGroup.SortedColumnIndexes).Count() != 1)
55        throw new CommandExecutionException("Exactly one column must be sorted and selected to define the column which holds the grouping value.", this);
56      if (!AffectedColumns.Except(ColumnGroup.SortedColumnIndexes).Any())
57        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);
58
59      Dictionary<IComparable, List<ColumnBase>> newColumns = new Dictionary<IComparable, List<ColumnBase>>();
60
61      ColumnBase groupColumn = ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.Except(AffectedColumns).First());
62      if (groupColumn.ContainsNullValues) throw new CommandExecutionException("Column which defines the equality (" + groupColumn.Name + ") must not contain NULL values.", this);
63
64      ColumnBase splitColumn = ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.Intersect(AffectedColumns).First());
65      var valueColumns = AffectedColumns.Except(ColumnGroup.SortedColumnIndexes).Select(index => ColumnGroup.GetColumn(index)).ToList();
66
67      IComparable groupValue = groupColumn.GetValue(0); ;
68      IComparable splitValue;
69      int groupStartIndex = 0;
70      for (int row = 0; row < ColumnGroup.RowCount; row++) {
71        //check if equality value is still the same
72        if (groupValue.CompareTo(groupColumn.GetValue(row)) != 0) {
73          groupValue = groupColumn.GetValue(row);
74          groupStartIndex = row;
75        }
76        splitValue = splitColumn.GetValue(row);
77
78        //create new column if new group value was detected
79        if (!newColumns.ContainsKey(splitValue)) {
80          newColumns.Add(splitValue, new List<ColumnBase>(valueColumns.Count));
81          int i = 0;
82          foreach (var valueColumn in valueColumns) {
83            var newColumn = valueColumn.CreateCopyOfColumnWithoutValues(valueColumn.TotalValuesCount);
84            newColumn.Name += "_" + splitValue;
85            newColumn.Resize(ColumnGroup.RowCount);
86            newColumns[splitValue].Add(newColumn);
87            i++;
88          }
89        }
90
91
92        for (int i = groupStartIndex; i < ColumnGroup.RowCount && groupValue.CompareTo(groupColumn.GetValue(i)) == 0; i++) {
93          for (int col = 0; col < valueColumns.Count; col++)
94            newColumns[splitValue][col].ChangeValue(i, valueColumns[col].GetValue(row));
95        }
96      }
97
98      ColumnGroup.AddColumns(newColumns.Values.SelectMany(col => col));
99      addedColumnsCount = newColumns.Values.Sum(columns => columns.Count);
100      this.ColumnGroup.FireChanged();
101      this.ColumnGroup = null;
102    }
103
104    public override void UndoExecute() {
105      base.UndoExecute();
106      for (int i = 0; i < addedColumnsCount; i++)
107        ColumnGroup.RemoveColumn(ColumnGroup.Columns.Count() - 1);
108      this.ColumnGroup.FireChanged();
109      this.ColumnGroup = null;
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.