Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/Implementations/FilterLogic.cs @ 10939

Last change on this file since 10939 was 10939, checked in by sbreuer, 8 years ago
  • do filtering in transaction, so the content changed event is only fired once
  • fix bug with apply filter
File size: 3.5 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.DataPreprocessing.Filter;
26using HeuristicLab.DataPreprocessing.Interfaces;
27namespace HeuristicLab.DataPreprocessing {
28  public class FilterLogic : IFilterLogic {
29    private IFilteredPreprocessingData preprocessingData;
30
31    public FilterLogic(IFilteredPreprocessingData preprocessingData) {
32      this.preprocessingData = preprocessingData;
33    }
34
35    public bool[] GetFilterResult(IList<IFilter> filters, bool isAndCombination) {
36      IList<IFilter> activeFilters = filters.Where(f => f.Active && f.ConstraintData != null).ToList<IFilter>();
37
38      if (activeFilters.Count == 0) {
39        return CreateBoolArray(preprocessingData.Rows, false);
40      }
41      return GetActiveFilterResult(activeFilters, isAndCombination);
42    }
43
44    private bool[] GetActiveFilterResult(IList<IFilter> activeFilters, bool isAndCombination) {
45      bool[] result = CreateBoolArray(preprocessingData.Rows, !isAndCombination);
46
47      foreach (IFilter filter in activeFilters) {
48        bool[] filterResult = filter.Check();
49        for (int row = 0; row < result.Length; ++row) {
50          result[row] = Result(result[row], filterResult[row], isAndCombination);
51        }
52      }
53      return result;
54    }
55
56    public bool[] Preview(IList<IFilter> filters, bool isAndCombination) {
57      IList<IFilter> activeFilters = filters.Where(f => f.Active && f.ConstraintData != null).ToList<IFilter>();
58      if (activeFilters.Count > 0) {
59        var result = GetActiveFilterResult(activeFilters, isAndCombination);
60        preprocessingData.SetFilter(result);
61        return result;
62      } else {
63        return CreateBoolArray(preprocessingData.Rows, false);
64      }
65    }
66
67    public bool[] CreateBoolArray(int rows, bool value) {
68      return Enumerable.Repeat<bool>(value, rows).ToArray();
69    }
70
71    public bool Result(bool current, bool addition, bool isAndCombination) {
72      return isAndCombination ? current || addition : current && addition;
73    }
74
75    public void Apply(IList<IFilter> filters, bool isAndCombination) {
76      preprocessingData.PersistFilter();
77      Reset();
78    }
79
80    public void Reset() {
81      preprocessingData.ResetFilter();
82    }
83
84    public bool IsFiltered {
85      get {
86        return preprocessingData.IsFiltered;
87      }
88    }
89
90    public IPreprocessingData PreprocessingData {
91      get { return preprocessingData; }
92    }
93
94    public event EventHandler FilterChanged {
95      add { preprocessingData.FilterChanged += value; }
96      remove { preprocessingData.FilterChanged -= value; }
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.