Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7461 was 7267, checked in by gkronber, 13 years ago

#1734 updated copyright year in all files of the DataImporter branch

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