Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10999 was 10999, checked in by rstoll, 8 years ago
  • Reordered Code
File size: 3.4 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
30    public IFilteredPreprocessingData PreprocessingData { get; private set; }
31
32    public bool IsFiltered {
33      get {
34        return PreprocessingData.IsFiltered;
35      }
36    }
37
38    public FilterLogic(IFilteredPreprocessingData preprocessingData) {
39      PreprocessingData = preprocessingData;
40    }
41
42    public bool[] GetFilterResult(IList<IFilter> filters, bool isAndCombination) {
43      IList<IFilter> activeFilters = filters.Where(f => f.Active && f.ConstraintData != null).ToList<IFilter>();
44
45      if (activeFilters.Count == 0) {
46        return CreateBoolArray(PreprocessingData.Rows, false);
47      }
48      return GetActiveFilterResult(activeFilters, isAndCombination);
49    }
50
51    private bool[] GetActiveFilterResult(IList<IFilter> activeFilters, bool isAndCombination) {
52      bool[] result = CreateBoolArray(PreprocessingData.Rows, !isAndCombination);
53
54      foreach (IFilter filter in activeFilters) {
55        bool[] filterResult = filter.Check();
56        for (int row = 0; row < result.Length; ++row) {
57          result[row] = Result(result[row], filterResult[row], isAndCombination);
58        }
59      }
60      return result;
61    }
62
63    public bool[] Preview(IList<IFilter> filters, bool isAndCombination) {
64      IList<IFilter> activeFilters = filters.Where(f => f.Active && f.ConstraintData != null).ToList<IFilter>();
65      if (activeFilters.Count > 0) {
66        var result = GetActiveFilterResult(activeFilters, isAndCombination);
67        PreprocessingData.SetFilter(result);
68        return result;
69      } else {
70        return CreateBoolArray(PreprocessingData.Rows, false);
71      }
72    }
73
74    public bool[] CreateBoolArray(int rows, bool value) {
75      return Enumerable.Repeat<bool>(value, rows).ToArray();
76    }
77
78    public bool Result(bool current, bool addition, bool isAndCombination) {
79      return isAndCombination ? current || addition : current && addition;
80    }
81
82    public void Apply(IList<IFilter> filters, bool isAndCombination) {
83      PreprocessingData.PersistFilter();
84      Reset();
85    }
86
87    public void Reset() {
88      PreprocessingData.ResetFilter();
89    }
90
91    public event EventHandler FilterChanged {
92      add { PreprocessingData.FilterChanged += value; }
93      remove { PreprocessingData.FilterChanged -= value; }
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.