Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataPreprocessing/3.4/Logic/Filter/ComparisonFilter.cs @ 15110

Last change on this file since 15110 was 15110, checked in by pfleck, 7 years ago

#2709: merged branch to trunk

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