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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Collections;
|
---|
26 | using HeuristicLab.Core.Views;
|
---|
27 | using HeuristicLab.DataPreprocessing.Filter;
|
---|
28 | using HeuristicLab.MainForm;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.DataPreprocessing.Views {
|
---|
31 | [View("CheckedFilterCollection View")]
|
---|
32 | [Content(typeof(FilterContent), true)]
|
---|
33 | public partial class FilterView : ItemView {
|
---|
34 | public new FilterContent Content {
|
---|
35 | get { return (FilterContent)base.Content; }
|
---|
36 | set { base.Content = value; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | public FilterView() {
|
---|
40 | InitializeComponent();
|
---|
41 | tbTotal.Text = "0";
|
---|
42 | tbRemaining.Text = "0";
|
---|
43 | tbPercentage.Text = "0%";
|
---|
44 | }
|
---|
45 |
|
---|
46 | private void InitData() {
|
---|
47 | checkedFilterView.Content = Content.Filters;
|
---|
48 | checkedFilterView.Content.ItemsAdded += Content_ItemsAdded;
|
---|
49 | checkedFilterView.Content.ItemsRemoved += Content_ItemsRemoved;
|
---|
50 | checkedFilterView.Content.CheckedItemsChanged += Content_CheckedItemsChanged;
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected override void OnContentChanged() {
|
---|
54 | base.OnContentChanged();
|
---|
55 | if (Content != null) {
|
---|
56 | InitData();
|
---|
57 | UpdateFilterInfo();
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | private void Content_CheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<IFilter> e) {
|
---|
62 | if (Content != null) {
|
---|
63 | foreach (IFilter filter in e.Items) {
|
---|
64 | filter.Active = checkedFilterView.Content.ItemChecked(filter);
|
---|
65 | }
|
---|
66 | UpdateFilterInfo();
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | private void UpdateFilterInfo() {
|
---|
71 | List<IFilter> filters = Content.Filters.ToList();
|
---|
72 | int activeFilters = filters.Count(c => c.Active);
|
---|
73 | applyFilterButton.Enabled = (activeFilters > 0);
|
---|
74 | rBtnAnd.Enabled = (activeFilters > 0);
|
---|
75 | rBtnOr.Enabled = (activeFilters > 0);
|
---|
76 | Content.PreprocessingData.ResetFilter();
|
---|
77 | bool isAndCombination = rBtnAnd.Checked;
|
---|
78 | bool[] ret;
|
---|
79 | IList<IFilter> activeFilters1 = filters.Where(f => f.Active && f.ConstraintData != null).ToList();
|
---|
80 | if (activeFilters1.Count > 0) {
|
---|
81 | var result1 = Enumerable.Repeat(!isAndCombination, Content.PreprocessingData.Rows).ToArray();
|
---|
82 |
|
---|
83 | foreach (IFilter filter in activeFilters1) {
|
---|
84 | bool[] filterResult = filter.Check();
|
---|
85 | for (int row = 0; row < result1.Length; ++row) {
|
---|
86 | result1[row] = isAndCombination ? result1[row] || filterResult[row] : result1[row] && filterResult[row];
|
---|
87 | }
|
---|
88 | }
|
---|
89 | Content.PreprocessingData.SetFilter(result1);
|
---|
90 | ret = result1;
|
---|
91 | } else {
|
---|
92 | ret = Enumerable.Repeat(false, Content.PreprocessingData.Rows).ToArray();
|
---|
93 | }
|
---|
94 | bool[] result = ret;
|
---|
95 |
|
---|
96 | int filteredCnt = result.Count(c => !c);
|
---|
97 |
|
---|
98 | tbRemaining.Text = filteredCnt.ToString();
|
---|
99 | double percentage = result.Length == 0 ? 0.0 : filteredCnt * 100 / (double)result.Length;
|
---|
100 | tbPercentage.Text = String.Format("{0:0.0000}%", percentage);
|
---|
101 | tbTotal.Text = result.Length.ToString();
|
---|
102 | }
|
---|
103 |
|
---|
104 | private void applyFilterButton_Click(object sender, EventArgs e) {
|
---|
105 | if (Content != null) {
|
---|
106 | List<IFilter> filters = Content.Filters.ToList();
|
---|
107 | //apply filters
|
---|
108 | bool isAndCombination = rBtnAnd.Checked;
|
---|
109 | Content.PreprocessingData.PersistFilter();
|
---|
110 | Content.PreprocessingData.ResetFilter();
|
---|
111 | //deactivate checked filters
|
---|
112 | filters = checkedFilterView.Content.CheckedItems.ToList();
|
---|
113 | foreach (IFilter filter in filters) {
|
---|
114 | checkedFilterView.Content.SetItemCheckedState(filter, false);
|
---|
115 | filter.Active = false;
|
---|
116 | }
|
---|
117 | UpdateFilterInfo();
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | //whenever a new filter is added the preprocessing data is set to the filter
|
---|
122 | private void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IFilter> e) {
|
---|
123 | if (Content != null) {
|
---|
124 | foreach (IFilter filter in e.Items) {
|
---|
125 | filter.ConstrainedValue = Content.PreprocessingData;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IFilter> e) {
|
---|
131 | if (Content != null) {
|
---|
132 | UpdateFilterInfo();
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | private void rBtnAnd_CheckedChanged(object sender, EventArgs e) {
|
---|
137 | if (Content != null) {
|
---|
138 | UpdateFilterInfo();
|
---|
139 | Content.IsAndCombination = rBtnAnd.Checked;
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|