Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeValues/SearchWithMatchOperationCommandBase.cs @ 13792

Last change on this file since 13792 was 9615, checked in by mkommend, 12 years ago

#1734: Updated copyright information in all DataImporter classes.

File size: 5.2 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;
23using System.Collections.Generic;
24using System.Drawing;
25using HeuristicLab.DataImporter.Data.CommandBase;
26using HeuristicLab.DataImporter.Data.Model;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.DataImporter.Command {
30  public enum MatchOperation {
31    Equal = 0,
32    NotEqual = 1,
33    Smaller = 2,
34    Larger = 3,
35  }
36
37  [StorableClass]
38  public abstract class SearchWithMatchOperationCommandBase : SearchCommandBase {
39    [StorableConstructor]
40    protected SearchWithMatchOperationCommandBase(bool deserializing) : base(deserializing) { }
41    protected SearchWithMatchOperationCommandBase(DataSet dataSet, string columnGroupName, int[] affectedColumns) :
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:
119          compareFunction = (left, right) => {
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;
123            return left.CompareTo(right) < 0;
124          };
125          break;
126        default:
127          throw new ArgumentException("Undefined compare function.");
128      }
129      return compareFunction;
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.