#region License Information /* HeuristicLab * Copyright (C) 2002-2011 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.Text; using HeuristicLab.DataImporter.Data.CommandBase; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.DataImporter.Data.Model; using System.Drawing; namespace HeuristicLab.DataImporter.Command { public enum MatchOperation { Equal = 0, NotEqual = 1, Smaller = 2, Larger = 3, } [StorableClass] public abstract class SearchWithMatchOperationCommandBase : SearchCommandBase { private SearchWithMatchOperationCommandBase() : base(null, string.Empty, null) { } public SearchWithMatchOperationCommandBase(DataSet dataSet, string columnGroupName, int[] affectedColumns) : base(dataSet, columnGroupName, affectedColumns) { } [Storable] private MatchOperation matchOperation; public MatchOperation MatchOperation { get { return this.matchOperation; } set { this.matchOperation = value; } } protected Point[] GetAffectedCells() { List affectedCells = new List(); Func compareFunction = this.GetCompareFunction(this.matchOperation); ColumnBase column; IComparable compareValue; IComparable value; for (int col = 0; col < AffectedColumns.Length; col++) { column = ColumnGroup.GetColumn(AffectedColumns[col]); compareValue = this.GetCompareValue(column, SearchValue); for (int row = 0; row < column.TotalValuesCount; row++) { value = column.GetValue(row); if (compareFunction(compareValue, value)) affectedCells.Add(new Point(AffectedColumns[col], row)); } } return affectedCells.ToArray(); } private IComparable GetCompareValue(ColumnBase column, string searchValue) { if (string.IsNullOrEmpty(searchValue)) return null; IComparable compareValue; if (column is DoubleColumn) { double v; if (!double.TryParse(SearchValue, out v)) throw new CommandExecutionException("Can't use value " + SearchValue + " to filter a DoubleColumn.", this); compareValue = v; } else if (column is DateTimeColumn) { DateTime d; if (!DateTime.TryParse(SearchValue, out d)) throw new CommandExecutionException("Can't use value " + SearchValue + " to filter a DateTimeColumn.", this); compareValue = d; } else if (column is StringColumn) { compareValue = SearchValue; } else throw new CommandExecutionException("Column type " + column.GetType() + " is not supported.", this); return compareValue; } private Func GetCompareFunction(MatchOperation matchOperation) { Func compareFunction; switch (matchOperation) { case MatchOperation.Equal: compareFunction = (left, right) => { if (left == null && right == null) return true; else if (left != null && right == null) return false; else if (left == null && right != null) return false; return left.CompareTo(right) == 0; }; break; case MatchOperation.NotEqual: compareFunction = (left, right) => { if (left == null && right == null) return false; else if (left != null && right == null) return true; else if (left == null && right != null) return true; return left.CompareTo(right) != 0; }; break; case MatchOperation.Smaller: compareFunction = (left, right) => { if (left == null && right == null) return false; else if (left != null && right == null) return false; else if (left == null && right != null) return false; return left.CompareTo(right) > 0; }; break; case MatchOperation.Larger: compareFunction = (left, right) => { if (left == null && right == null) return false; else if (left != null && right == null) return false; else if (left == null && right != null) return false; return left.CompareTo(right) < 0; }; break; default: throw new ArgumentException("Undefined compare function."); } return compareFunction; } } }