#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.DataImporter.Data;
using HeuristicLab.DataImporter.Data.CommandBase;
using HeuristicLab.DataImporter.Data.Model;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.DataImporter.Command {
[StorableClass]
[ViewableCommandInfoAttribute("Delete Rows", 1, ColumnGroupState.AnySelectedColumnContainsNull,
"Handle Missing Values", Position = 4)]
public class DeleteRowsWithMissingValuesCommand : ColumnGroupCommandWithAffectedColumnsBase {
private List oldColumns;
private ICollection oldSortedColumnIndices;
[StorableConstructor]
protected DeleteRowsWithMissingValuesCommand(bool deserializing)
: base(deserializing) {
oldColumns = new List();
}
public DeleteRowsWithMissingValuesCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
: base(dataSet, columnGroupName, affectedColumns) {
oldColumns = new List();
}
public override void Execute() {
base.Execute();
ColumnGroup tempColumnGroup = new ColumnGroup();
HashSet positions = new HashSet();
ColumnBase column;
for (int col = 0; col < AffectedColumns.Length; col++) {
column = ColumnGroup.GetColumn(AffectedColumns[col]);
for (int row = 0; row < ColumnGroup.RowCount; row++) {
if (column.GetValue(row) == null && !positions.Contains(row))
positions.Add(row);
}
}
foreach (ColumnBase x in ColumnGroup.Columns) {
oldColumns.Add(x);
tempColumnGroup.AddColumn(x.CreateCopyOfColumnWithoutValues());
}
for (int i = 0; i < ColumnGroup.RowCount; i++)
if (!positions.Contains(i))
tempColumnGroup.AddRow(ColumnGroup.GetRow(i));
oldSortedColumnIndices = new List(ColumnGroup.SortedColumnIndexes);
for (int i = 0; i < ColumnGroup.Columns.Count(); i++) {
tempColumnGroup.Columns.ElementAt(i).SortOrder = ColumnGroup.GetColumn(i).SortOrder;
ColumnGroup.ReplaceColumn(i, tempColumnGroup.Columns.ElementAt(i));
}
ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
ColumnGroup.FireChanged();
ColumnGroup = null;
}
public override void UndoExecute() {
base.UndoExecute();
for (int i = 0; i < ColumnGroup.Columns.Count(); i++)
ColumnGroup.ReplaceColumn(i, oldColumns[i]);
ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
oldColumns.Clear();
oldSortedColumnIndices = null;
ColumnGroup.FireChanged();
ColumnGroup = null;
}
public override string Description {
get { return "Delete rows with missing values"; }
}
}
}