Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeValues/SearchWithMatchOperationCommandBase.cs @ 16566

Last change on this file since 16566 was 16566, checked in by gkronber, 6 years ago

#2520: updated HeuristicLab.DataImporter addon for new Persistence (introduced in 3.3.16)

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