[10539] | 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
|
---|
[10244] | 21 |
|
---|
[10900] | 22 | using System;
|
---|
[10619] | 23 | using System.Collections.Generic;
|
---|
[10666] | 24 | using System.Linq;
|
---|
[10843] | 25 | using HeuristicLab.DataPreprocessing.Filter;
|
---|
[10783] | 26 | using HeuristicLab.DataPreprocessing.Interfaces;
|
---|
[10539] | 27 | namespace HeuristicLab.DataPreprocessing {
|
---|
| 28 | public class FilterLogic : IFilterLogic {
|
---|
[10619] | 29 |
|
---|
[10999] | 30 | public IFilteredPreprocessingData PreprocessingData { get; private set; }
|
---|
| 31 |
|
---|
| 32 | public bool IsFiltered {
|
---|
| 33 | get {
|
---|
| 34 | return PreprocessingData.IsFiltered;
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[10843] | 38 | public FilterLogic(IFilteredPreprocessingData preprocessingData) {
|
---|
[10999] | 39 | PreprocessingData = preprocessingData;
|
---|
[10619] | 40 | }
|
---|
| 41 |
|
---|
[10844] | 42 | public bool[] GetFilterResult(IList<IFilter> filters, bool isAndCombination) {
|
---|
[10843] | 43 | IList<IFilter> activeFilters = filters.Where(f => f.Active && f.ConstraintData != null).ToList<IFilter>();
|
---|
| 44 |
|
---|
| 45 | if (activeFilters.Count == 0) {
|
---|
[10999] | 46 | return CreateBoolArray(PreprocessingData.Rows, false);
|
---|
[10843] | 47 | }
|
---|
[10900] | 48 | return GetActiveFilterResult(activeFilters, isAndCombination);
|
---|
| 49 | }
|
---|
[10696] | 50 |
|
---|
[10900] | 51 | private bool[] GetActiveFilterResult(IList<IFilter> activeFilters, bool isAndCombination) {
|
---|
[10999] | 52 | bool[] result = CreateBoolArray(PreprocessingData.Rows, !isAndCombination);
|
---|
[10802] | 53 |
|
---|
[10843] | 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 | }
|
---|
[10844] | 60 | return result;
|
---|
| 61 | }
|
---|
[10619] | 62 |
|
---|
[10844] | 63 | public bool[] Preview(IList<IFilter> filters, bool isAndCombination) {
|
---|
[10900] | 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);
|
---|
[10999] | 67 | PreprocessingData.SetFilter(result);
|
---|
[10900] | 68 | return result;
|
---|
| 69 | } else {
|
---|
[10999] | 70 | return CreateBoolArray(PreprocessingData.Rows, false);
|
---|
[10900] | 71 | }
|
---|
[10619] | 72 | }
|
---|
| 73 |
|
---|
[10843] | 74 | public bool[] CreateBoolArray(int rows, bool value) {
|
---|
| 75 | return Enumerable.Repeat<bool>(value, rows).ToArray();
|
---|
[10802] | 76 | }
|
---|
| 77 |
|
---|
[10843] | 78 | public bool Result(bool current, bool addition, bool isAndCombination) {
|
---|
[10849] | 79 | return isAndCombination ? current || addition : current && addition;
|
---|
[10802] | 80 | }
|
---|
| 81 |
|
---|
[10843] | 82 | public void Apply(IList<IFilter> filters, bool isAndCombination) {
|
---|
[10999] | 83 | PreprocessingData.PersistFilter();
|
---|
[10939] | 84 | Reset();
|
---|
[10783] | 85 | }
|
---|
[10619] | 86 |
|
---|
[10783] | 87 | public void Reset() {
|
---|
[10999] | 88 | PreprocessingData.ResetFilter();
|
---|
[10783] | 89 | }
|
---|
[10638] | 90 |
|
---|
[10900] | 91 | public event EventHandler FilterChanged {
|
---|
[10999] | 92 | add { PreprocessingData.FilterChanged += value; }
|
---|
| 93 | remove { PreprocessingData.FilterChanged -= value; }
|
---|
[10900] | 94 | }
|
---|
[10244] | 95 | }
|
---|
| 96 | }
|
---|