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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.DataPreprocessing.Filter;
|
---|
25 | using HeuristicLab.DataPreprocessing.Interfaces;
|
---|
26 | namespace HeuristicLab.DataPreprocessing {
|
---|
27 | public class FilterLogic : IFilterLogic {
|
---|
28 |
|
---|
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 new bool[preprocessingData.Rows];
|
---|
40 | }
|
---|
41 |
|
---|
42 | bool[] result = CreateBoolArray(preprocessingData.Rows, !isAndCombination);
|
---|
43 |
|
---|
44 | foreach (IFilter filter in activeFilters) {
|
---|
45 | bool[] filterResult = filter.Check();
|
---|
46 | for (int row = 0; row < result.Length; ++row) {
|
---|
47 | result[row] = Result(result[row], filterResult[row], isAndCombination);
|
---|
48 | }
|
---|
49 | }
|
---|
50 | return result;
|
---|
51 | }
|
---|
52 |
|
---|
53 | public bool[] Preview(IList<IFilter> filters, bool isAndCombination) {
|
---|
54 | var result = GetFilterResult(filters, isAndCombination);
|
---|
55 | preprocessingData.SetFilter(result);
|
---|
56 | return result;
|
---|
57 | }
|
---|
58 |
|
---|
59 | public bool[] CreateBoolArray(int rows, bool value) {
|
---|
60 | return Enumerable.Repeat<bool>(value, rows).ToArray();
|
---|
61 | }
|
---|
62 |
|
---|
63 | public bool Result(bool current, bool addition, bool isAndCombination) {
|
---|
64 | return isAndCombination ? current || addition : current && addition;
|
---|
65 | }
|
---|
66 |
|
---|
67 | public void Apply(IList<IFilter> filters, bool isAndCombination) {
|
---|
68 | preprocessingData.SetFilter(Preview(filters, isAndCombination));
|
---|
69 | preprocessingData.PersistFilter();
|
---|
70 | }
|
---|
71 |
|
---|
72 | public void Reset() {
|
---|
73 | preprocessingData.ResetFilter();
|
---|
74 | }
|
---|
75 |
|
---|
76 | public IPreprocessingData PreprocessingData {
|
---|
77 | get { return preprocessingData; }
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|