Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8387


Ignore:
Timestamp:
08/02/12 14:48:20 (12 years ago)
Author:
mkommend
Message:

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

Location:
branches/HeuristicLab.DataImporter
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeColumnGroup/SplitDictionaryStyleDataCommand.cs

    r7267 r8387  
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using System.Text;
    2625using HeuristicLab.DataImporter.Data;
    2726using HeuristicLab.DataImporter.Data.CommandBase;
     
    3231  [StorableClass]
    3332  [ViewableCommandInfo("Split Dictionary-style Data", 1, ColumnGroupState.ColumnSelected | ColumnGroupState.Sorted, "Column Commands",
    34     Position = 15, SelectedColumns = 2)]
     33    Position = 15)]
    3534  public class SplitDictionaryStyleDataCommand : ColumnGroupCommandWithAffectedColumnsBase {
    3635    private int addedColumnsCount;
     
    5049    public override void Execute() {
    5150      base.Execute();
    52       Dictionary<IComparable, ColumnBase> newColumns = new Dictionary<IComparable, ColumnBase>(); ;
    5351      if (ColumnGroup.SortedColumnIndexes.Except(AffectedColumns).Count() != 1 &&
    5452        ColumnGroup.SortedColumnIndexes.Except(AffectedColumns).FirstOrDefault() == ColumnGroup.SortedColumnIndexes.ElementAt(0))
     
    5654      if (AffectedColumns.Intersect(ColumnGroup.SortedColumnIndexes).Count() != 1)
    5755        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)
     56      if (!AffectedColumns.Except(ColumnGroup.SortedColumnIndexes).Any())
    5957        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);
    6058
    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);
     59      Dictionary<IComparable, List<ColumnBase>> newColumns = new Dictionary<IComparable, List<ColumnBase>>();
    6460
    65       ColumnBase groupColumn = ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.Intersect(AffectedColumns).ElementAt(0));
    66       ColumnBase valueColumn = ColumnGroup.GetColumn(AffectedColumns.Except(ColumnGroup.SortedColumnIndexes).ElementAt(0));
     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);
    6763
    68       IComparable equalityValue = equalityColumn.GetValue(0); ;
    69       IComparable groupValue;
     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;
    7069      int groupStartIndex = 0;
    7170      for (int row = 0; row < ColumnGroup.RowCount; row++) {
    7271        //check if equality value is still the same
    73         if (equalityValue.CompareTo(equalityColumn.GetValue(row)) != 0) {
    74           equalityValue = equalityColumn.GetValue(row);
     72        if (groupValue.CompareTo(groupColumn.GetValue(row)) != 0) {
     73          groupValue = groupColumn.GetValue(row);
    7574          groupStartIndex = row;
    7675        }
    77         groupValue = groupColumn.GetValue(row);
     76        splitValue = splitColumn.GetValue(row);
     77
    7878        //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;
     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          }
    8389        }
    8490
    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));
     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        }
    8996      }
    9097
    91       ColumnGroup.AddColumns(newColumns.Values);
    92       addedColumnsCount = newColumns.Count;
     98      ColumnGroup.AddColumns(newColumns.Values.SelectMany(col => col));
     99      addedColumnsCount = newColumns.Values.Sum(columns => columns.Count);
    93100      this.ColumnGroup.FireChanged();
    94101      this.ColumnGroup = null;
  • branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/Command/DeleteColumnCommand.cs

    r7625 r8387  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using System.Linq;
    25 using System.Text;
    2624using System.Windows.Forms;
    2725using HeuristicLab.DataImporter.Data.CommandBase;
     
    5149
    5250      ColumnBase column;
    53       foreach (int columnIndex in AffectedColumns.Reverse()) {
     51      foreach (int columnIndex in AffectedColumns.OrderByDescending(i => i)) {
    5452        column = ColumnGroup.GetColumn(columnIndex);
    5553        deletedColumns.Insert(0, column);
  • branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/CommandBase/ColumnGroupCommandBase.cs

    r7267 r8387  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    2422using System.Linq;
    25 using System.Text;
    26 using System.Xml;
    2723using HeuristicLab.DataImporter.Data.Model;
    2824using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    4743
    4844    protected void UpdateColumnGroup() {
    49       this.columnGroup = this.DataSet.ColumnGroups.Where(cg => cg.Name == this.columnGroupName).FirstOrDefault();
     45      this.columnGroup = this.DataSet.ColumnGroups.FirstOrDefault(cg => cg.Name == this.columnGroupName);
    5046    }
    5147
  • branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/Model/ColumnBase.cs

    r7267 r8387  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Collections;
    25 using System.Linq;
    26 using System.Text;
    27 using System.Xml;
    28 using System.Globalization;
    2924using System.Windows.Forms;
    3025using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
Note: See TracChangeset for help on using the changeset viewer.