Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/Logic/FilterLogic.cs @ 15269

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

#2809: Removed SearchLogic

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