source: branches/2520_PersistenceReintegration/HeuristicLab.DataPreprocessing/3.4/Filter/ComparisonFilter.cs @ 16474

Last change on this file since 16474 was 16474, checked in by gkronber, 7 months ago

#2520: checked and added StorableType attribute in projects up to HeuristicLab.MainForm

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