Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/MissingValues/DeleteRowsWithMissingValuesCommand.cs @ 12296

Last change on this file since 12296 was 9615, checked in by mkommend, 11 years ago

#1734: Updated copyright information in all DataImporter classes.

File size: 3.6 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.DataImporter.Data;
25using HeuristicLab.DataImporter.Data.CommandBase;
26using HeuristicLab.DataImporter.Data.Model;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.DataImporter.Command {
30  [StorableClass]
31  [ViewableCommandInfoAttribute("Delete Rows", 1, ColumnGroupState.AnySelectedColumnContainsNull,
32    "Handle Missing Values", Position = 4)]
33  public class DeleteRowsWithMissingValuesCommand : ColumnGroupCommandWithAffectedColumnsBase {
34    private List<ColumnBase> oldColumns;
35    private ICollection<int> oldSortedColumnIndices;
36
37    [StorableConstructor]
38    protected DeleteRowsWithMissingValuesCommand(bool deserializing)
39      : base(deserializing) {
40      oldColumns = new List<ColumnBase>();
41    }
42
43    public DeleteRowsWithMissingValuesCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
44      : base(dataSet, columnGroupName, affectedColumns) {
45      oldColumns = new List<ColumnBase>();
46    }
47
48    public override void Execute() {
49      base.Execute();
50      ColumnGroup tempColumnGroup = new ColumnGroup();
51      HashSet<int> positions = new HashSet<int>();
52      ColumnBase column;
53
54      for (int col = 0; col < AffectedColumns.Length; col++) {
55        column = ColumnGroup.GetColumn(AffectedColumns[col]);
56        for (int row = 0; row < ColumnGroup.RowCount; row++) {
57          if (column.GetValue(row) == null && !positions.Contains(row))
58            positions.Add(row);
59        }
60      }
61
62      foreach (ColumnBase x in ColumnGroup.Columns) {
63        oldColumns.Add(x);
64        tempColumnGroup.AddColumn(x.CreateCopyOfColumnWithoutValues());
65      }
66
67      for (int i = 0; i < ColumnGroup.RowCount; i++)
68        if (!positions.Contains(i))
69          tempColumnGroup.AddRow(ColumnGroup.GetRow(i));
70
71      oldSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
72      for (int i = 0; i < ColumnGroup.Columns.Count(); i++) {
73        tempColumnGroup.Columns.ElementAt(i).SortOrder = ColumnGroup.GetColumn(i).SortOrder;
74        ColumnGroup.ReplaceColumn(i, tempColumnGroup.Columns.ElementAt(i));
75      }
76
77      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
78      ColumnGroup.FireChanged();
79      ColumnGroup = null;
80    }
81
82    public override void UndoExecute() {
83      base.UndoExecute();
84      for (int i = 0; i < ColumnGroup.Columns.Count(); i++)
85        ColumnGroup.ReplaceColumn(i, oldColumns[i]);
86      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
87      oldColumns.Clear();
88      oldSortedColumnIndices = null;
89      ColumnGroup.FireChanged();
90      ColumnGroup = null;
91    }
92
93    public override string Description {
94      get { return "Delete rows with missing values"; }
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.