Free cookie consent management tool by TermsFeed Policy Generator

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

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