[6134] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9615] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6134] | 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 |
|
---|
| 22 | using System;
|
---|
[6133] | 23 | using System.Collections.Generic;
|
---|
[9614] | 24 | using System.Drawing;
|
---|
[6133] | 25 | using HeuristicLab.DataImporter.Data.CommandBase;
|
---|
[9614] | 26 | using HeuristicLab.DataImporter.Data.Model;
|
---|
[16566] | 27 | using HEAL.Attic;
|
---|
[6133] | 28 |
|
---|
| 29 | namespace HeuristicLab.DataImporter.Command {
|
---|
| 30 | public enum MatchOperation {
|
---|
| 31 | Equal = 0,
|
---|
| 32 | NotEqual = 1,
|
---|
| 33 | Smaller = 2,
|
---|
| 34 | Larger = 3,
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[16566] | 37 | [StorableType("8207BA3B-BA78-454C-93B2-03E687CB8662")]
|
---|
[6133] | 38 | public abstract class SearchWithMatchOperationCommandBase : SearchCommandBase {
|
---|
[9614] | 39 | [StorableConstructor]
|
---|
[16567] | 40 | protected SearchWithMatchOperationCommandBase(StorableConstructorFlag _) : base(_) { }
|
---|
[9614] | 41 | protected SearchWithMatchOperationCommandBase(DataSet dataSet, string columnGroupName, int[] affectedColumns) :
|
---|
[6133] | 42 | base(dataSet, columnGroupName, affectedColumns) {
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | [Storable]
|
---|
| 46 | private MatchOperation matchOperation;
|
---|
| 47 | public MatchOperation MatchOperation {
|
---|
| 48 | get { return this.matchOperation; }
|
---|
| 49 | set { this.matchOperation = value; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | protected Point[] GetAffectedCells() {
|
---|
| 53 | List<Point> affectedCells = new List<Point>();
|
---|
| 54 | Func<IComparable, IComparable, bool> compareFunction = this.GetCompareFunction(this.matchOperation);
|
---|
| 55 | ColumnBase column;
|
---|
| 56 | IComparable compareValue;
|
---|
| 57 | IComparable value;
|
---|
| 58 |
|
---|
| 59 | for (int col = 0; col < AffectedColumns.Length; col++) {
|
---|
| 60 | column = ColumnGroup.GetColumn(AffectedColumns[col]);
|
---|
| 61 | compareValue = this.GetCompareValue(column, SearchValue);
|
---|
| 62 | for (int row = 0; row < column.TotalValuesCount; row++) {
|
---|
| 63 | value = column.GetValue(row);
|
---|
| 64 | if (compareFunction(compareValue, value))
|
---|
| 65 | affectedCells.Add(new Point(AffectedColumns[col], row));
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | return affectedCells.ToArray();
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | private IComparable GetCompareValue(ColumnBase column, string searchValue) {
|
---|
| 73 | if (string.IsNullOrEmpty(searchValue))
|
---|
| 74 | return null;
|
---|
| 75 |
|
---|
| 76 | IComparable compareValue;
|
---|
| 77 | if (column is DoubleColumn) {
|
---|
| 78 | double v;
|
---|
| 79 | if (!double.TryParse(SearchValue, out v)) throw new CommandExecutionException("Can't use value " + SearchValue + " to filter a DoubleColumn.", this);
|
---|
| 80 | compareValue = v;
|
---|
| 81 | } else if (column is DateTimeColumn) {
|
---|
| 82 | DateTime d;
|
---|
| 83 | if (!DateTime.TryParse(SearchValue, out d)) throw new CommandExecutionException("Can't use value " + SearchValue + " to filter a DateTimeColumn.", this);
|
---|
| 84 | compareValue = d;
|
---|
| 85 | } else if (column is StringColumn) {
|
---|
| 86 | compareValue = SearchValue;
|
---|
| 87 | } else throw new CommandExecutionException("Column type " + column.GetType() + " is not supported.", this);
|
---|
| 88 | return compareValue;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | private Func<IComparable, IComparable, bool> GetCompareFunction(MatchOperation matchOperation) {
|
---|
| 92 | Func<IComparable, IComparable, bool> compareFunction;
|
---|
| 93 | switch (matchOperation) {
|
---|
| 94 | case MatchOperation.Equal:
|
---|
| 95 | compareFunction = (left, right) => {
|
---|
| 96 | if (left == null && right == null) return true;
|
---|
| 97 | else if (left != null && right == null) return false;
|
---|
| 98 | else if (left == null && right != null) return false;
|
---|
| 99 | return left.CompareTo(right) == 0;
|
---|
| 100 | };
|
---|
| 101 | break;
|
---|
| 102 | case MatchOperation.NotEqual:
|
---|
| 103 | compareFunction = (left, right) => {
|
---|
| 104 | if (left == null && right == null) return false;
|
---|
| 105 | else if (left != null && right == null) return true;
|
---|
| 106 | else if (left == null && right != null) return true;
|
---|
| 107 | return left.CompareTo(right) != 0;
|
---|
| 108 | };
|
---|
| 109 | break;
|
---|
| 110 | case MatchOperation.Smaller:
|
---|
| 111 | compareFunction = (left, right) => {
|
---|
| 112 | if (left == null && right == null) return false;
|
---|
| 113 | else if (left != null && right == null) return false;
|
---|
| 114 | else if (left == null && right != null) return false;
|
---|
| 115 | return left.CompareTo(right) > 0;
|
---|
| 116 | };
|
---|
| 117 | break;
|
---|
| 118 | case MatchOperation.Larger:
|
---|
[9614] | 119 | compareFunction = (left, right) => {
|
---|
[6133] | 120 | if (left == null && right == null) return false;
|
---|
| 121 | else if (left != null && right == null) return false;
|
---|
| 122 | else if (left == null && right != null) return false;
|
---|
[9614] | 123 | return left.CompareTo(right) < 0;
|
---|
| 124 | };
|
---|
[6133] | 125 | break;
|
---|
| 126 | default:
|
---|
| 127 | throw new ArgumentException("Undefined compare function.");
|
---|
| 128 | }
|
---|
| 129 | return compareFunction;
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|