Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6486 was 6134, checked in by gkronber, 14 years ago

#1471: added plugin for DbExplorer interfaces, deleted .resx files, set svn:ignore properties, and added license header

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 System.Text;
26using HeuristicLab.DataImporter.Data;
27using HeuristicLab.DataImporter.Data.CommandBase;
28using HeuristicLab.DataImporter.Data.Model;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.DataImporter.Command {
32  [StorableClass]
33  [ViewableCommandInfo("Split Dictionary-style Data", 1, ColumnGroupState.ColumnSelected | ColumnGroupState.Sorted, "Column Commands",
34    Position = 15, SelectedColumns = 2)]
35  public class SplitDictionaryStyleDataCommand : ColumnGroupCommandWithAffectedColumnsBase {
36    private int addedColumnsCount;
37
38    private SplitDictionaryStyleDataCommand()
39      : base(null, string.Empty, null) {
40    }
41
42    public SplitDictionaryStyleDataCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
43      : base(dataSet, columnGroupName, affectedColumns) {
44    }
45
46    public override string Description {
47      get { return "Split dictionary style data"; }
48    }
49
50    public override void Execute() {
51      base.Execute();
52      Dictionary<IComparable, ColumnBase> newColumns = new Dictionary<IComparable, ColumnBase>(); ;
53      if (ColumnGroup.SortedColumnIndexes.Except(AffectedColumns).Count() != 1 &&
54        ColumnGroup.SortedColumnIndexes.Except(AffectedColumns).FirstOrDefault() == ColumnGroup.SortedColumnIndexes.ElementAt(0))
55        throw new CommandExecutionException("Exactly one column must be sorted as first sorted column and not selected to define the equality of the rows.", this);
56      if (AffectedColumns.Intersect(ColumnGroup.SortedColumnIndexes).Count() != 1)
57        throw new CommandExecutionException("Exactly one column must be sorted and selected to define the column which holds the grouping value.", this);
58      if (AffectedColumns.Except(ColumnGroup.SortedColumnIndexes).Count() != 1)
59        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);
60
61      ColumnBase equalityColumn = ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.Except(AffectedColumns).ElementAt(0));
62      if (equalityColumn.ContainsNullValues)
63        throw new CommandExecutionException("Column which defines the equality (" + equalityColumn.Name + ") must not contain NULL values.", this);
64
65      ColumnBase groupColumn = ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.Intersect(AffectedColumns).ElementAt(0));
66      ColumnBase valueColumn = ColumnGroup.GetColumn(AffectedColumns.Except(ColumnGroup.SortedColumnIndexes).ElementAt(0));
67
68      IComparable equalityValue = equalityColumn.GetValue(0); ;
69      IComparable groupValue;
70      int groupStartIndex = 0;
71      for (int row = 0; row < ColumnGroup.RowCount; row++) {
72        //check if equality value is still the same
73        if (equalityValue.CompareTo(equalityColumn.GetValue(row)) != 0) {
74          equalityValue = equalityColumn.GetValue(row);
75          groupStartIndex = row;
76        }
77        groupValue = groupColumn.GetValue(row);
78        //create new column if new group value was detected
79        if (!newColumns.ContainsKey(groupValue)) {
80          newColumns.Add(groupValue, valueColumn.CreateCopyOfColumnWithoutValues());
81          newColumns[groupValue].Resize(ColumnGroup.RowCount);
82          newColumns[groupValue].Name += "_" + groupValue;
83        }
84
85        for (int i = groupStartIndex; i < row; i++)
86          newColumns[groupValue].ChangeValue(i, valueColumn.GetValue(row));
87        for (int i = row; i < ColumnGroup.RowCount && equalityValue.CompareTo(equalityColumn.GetValue(i)) == 0; i++)
88          newColumns[groupValue].ChangeValue(i, valueColumn.GetValue(row));
89      }
90
91      ColumnGroup.AddColumns(newColumns.Values);
92      addedColumnsCount = newColumns.Count;
93      this.ColumnGroup.FireChanged();
94      this.ColumnGroup = null;
95    }
96
97    public override void UndoExecute() {
98      base.UndoExecute();
99      for (int i = 0; i < addedColumnsCount; i++)
100        ColumnGroup.RemoveColumn(ColumnGroup.Columns.Count() - 1);
101      this.ColumnGroup.FireChanged();
102      this.ColumnGroup = null;
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.