Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/Implementations/Filter/ComparisonFilter.cs @ 10947

Last change on this file since 10947 was 10947, checked in by rstoll, 10 years ago
  • Changed ComparisonFilter, using DoubleValue and DateTime rather than just StringValue
  • Included better input validation for ComparisonFilterView
  • Bug "Search and Sorted DataGridContentView" fixed
File size: 4.8 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.Drawing;
24using HeuristicLab.Common;
25using HeuristicLab.Common.Resources;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28
29namespace HeuristicLab.DataPreprocessing.Filter {
30  [Item("ComparisonFilter", "A filter which compares the member of the preprocessing data with the constraint data.")]
31  public class ComparisonFilter : ComparisonConstraint, IFilter {
32
33   
34
35    protected ComparisonFilter(bool deserializing) : base(deserializing) {
36   
37    }
38
39    protected ComparisonFilter(ComparisonFilter original, Cloner cloner)
40      : base(original, cloner) {
41      constraintColumn = original.constraintColumn;
42    }
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new ComparisonFilter(this, cloner);
45    }
46
47    public ComparisonFilter() : base() { }
48    public ComparisonFilter(IPreprocessingData constrainedValue, ConstraintOperation constraintOperation, object constraintData)
49      : base(constrainedValue, constraintOperation, constraintData) {
50    }
51
52    public ComparisonFilter(IPreprocessingData constrainedValue, ConstraintOperation constraintOperation, object constraintData, bool active)
53      : base(constrainedValue, constraintOperation, constraintData, active) {
54    }
55
56
57    public override string ItemName {
58      get { return "ComparisonFilter"; }
59    }
60
61    public override Image ItemImage {
62      get { return VSImageLibrary.Filter; }
63    }
64
65    public new IPreprocessingData ConstrainedValue {
66      get { return (IPreprocessingData)base.ConstrainedValue; }
67      set { base.ConstrainedValue = value; }
68    }
69
70    public new IStringConvertibleValue ConstraintData {
71      get { return (IStringConvertibleValue)base.ConstraintData; }
72      set {
73        if (!(value is IComparable))
74          throw new ArgumentException("Only IComparables allowed for ConstraintData");
75        base.ConstraintData = value;
76      }
77    }
78
79
80    private int constraintColumn;
81    public int ConstraintColumn {
82      get { return constraintColumn; }
83      set {
84        if (ConstrainedValue.Columns < value)
85          throw new ArgumentException("Could not set ConstraintData to not existing column index.");
86
87        if (constraintColumn != value) {
88          constraintColumn = value;
89          this.OnConstraintColumnChanged();
90          this.OnToStringChanged();
91        }
92      }
93    }
94
95
96    public new bool[] Check() {
97      bool[] result = new bool[ConstrainedValue.Rows];
98
99      if (!Active)
100        return result;
101
102      for (int row = 0; row < ConstrainedValue.Rows; ++row) {
103        object item = null;
104        if (ConstrainedValue.IsType<double>(constraintColumn)) {
105          item = new HeuristicLab.Data.DoubleValue(ConstrainedValue.GetCell<double>(ConstraintColumn, row));
106        } else if (ConstrainedValue.IsType<DateTime>(constraintColumn)) {
107          item = new HeuristicLab.Data.DateTimeValue(ConstrainedValue.GetCell<DateTime>(ConstraintColumn, row));
108        } else {
109          item = new StringValue(ConstrainedValue.GetCell<string>(ConstraintColumn, row));
110        }
111
112        result[row] = !base.Check(item);
113      }
114
115      return result;
116    }
117
118    public new bool[] Check(out string errorMessage) {
119      errorMessage = string.Empty;
120      return this.Check();
121    }
122
123    public event EventHandler ConstraintColumnChanged;
124    protected virtual void OnConstraintColumnChanged() {
125      EventHandler handler = ConstraintColumnChanged;
126      if (handler != null)
127        handler(this, EventArgs.Empty);
128    }
129
130    public override string ToString() {
131      string s = string.Empty;
132      if (ConstrainedValue != null)
133        s += ConstrainedValue.GetVariableName(ConstraintColumn) + " ";
134
135      if (ConstraintOperation != null)
136        s += ConstraintOperation.ToString() + " ";
137
138      if (ConstraintData != null)
139        s += ConstraintData.ToString();
140      else
141        s += "null";
142
143      s += ".";
144      return s;
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.