#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;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using HeuristicLab.DataImporter.Command.View;
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 not equidistant rows", 1, ColumnGroupState.Sorted, "Time Series",
Position = 1, OptionsView = typeof(TimeBasedCommandView))]
public class DeleteNotEquidistantRowsCommand : EquidistantTimeSeriesCommandBase {
private DateTimeColumn columnToSample;
private List oldColumns;
private ICollection oldSortedColumnIndices;
private IEnumerable oldSortOrder;
[StorableConstructor]
protected DeleteNotEquidistantRowsCommand(bool deserializing)
: base(deserializing) {
oldColumns = new List();
}
public DeleteNotEquidistantRowsCommand(DataSet dataSet, string columnGroupName)
: base(dataSet, columnGroupName) {
oldColumns = new List();
}
public override string Description {
get { return "Delete not equidistant rows"; }
}
public override void Execute() {
base.Execute();
if (!(ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.ElementAt(0)) is DateTimeColumn)
|| ColumnGroup.SortedColumnIndexes.Count != 1)
throw new CommandExecutionException("ColumnGroup must be sorted after a datetime column.", this);
columnToSample = (DateTimeColumn)ColumnGroup.GetColumn(ColumnGroup.SortedColumnIndexes.ElementAt(0));
if (columnToSample.SortOrder != SortOrder.Ascending)
throw new CommandExecutionException("ColumnGroup must be sorted ascending after the selected datetime column.", this);
if (columnToSample.ContainsNullValues)
throw new CommandExecutionException("The datetime column must not contain null values.", this);
oldSortedColumnIndices = new List(ColumnGroup.SortedColumnIndexes);
oldSortOrder = ColumnGroup.SortOrdersForColumns.ToList();
DateTime value;
DateTime actPoint = StartTime;
ColumnGroup tempColumnGroup = new ColumnGroup();
List positions = new List();
for (int i = 0; i < ColumnGroup.RowCount; i++) {
value = (DateTime)columnToSample.GetValue(i);
while (actPoint < value)
actPoint += SampleFrequency;
if (actPoint == value) {
positions.Add(i);
}
}
foreach (ColumnBase x in ColumnGroup.Columns) {
oldColumns.Add(x);
tempColumnGroup.AddColumn(x.CreateCopyOfColumnWithoutValues());
}
for (int i = 0; i < positions.Count; i++)
tempColumnGroup.AddRow(ColumnGroup.GetRow(positions[i]));
for (int i = 0; i < ColumnGroup.Columns.Count(); i++)
ColumnGroup.ReplaceColumn(i, tempColumnGroup.Columns.ElementAt(i));
ColumnGroup.SortOrdersForColumns = oldSortOrder;
ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
ColumnGroup.FireChanged();
this.ColumnGroup = null;
}
public override void UndoExecute() {
base.UndoExecute();
for (int i = 0; i < ColumnGroup.Columns.Count(); i++)
ColumnGroup.ReplaceColumn(i, oldColumns[i]);
ColumnGroup.SortOrdersForColumns = oldSortOrder;
ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
oldSortedColumnIndices = null;
oldSortOrder = null;
oldColumns.Clear();
ColumnGroup.FireChanged();
this.ColumnGroup = null;
}
}
}