1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
2 | //
|
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
4 | // you may not use this file except in compliance with the License.
|
---|
5 | // You may obtain a copy of the License at
|
---|
6 | //
|
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
8 | //
|
---|
9 | // Unless required by applicable law or agreed to in writing, software
|
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
12 | // See the License for the specific language governing permissions and
|
---|
13 | // limitations under the License.
|
---|
14 |
|
---|
15 | package de.ugoe.cs.autoquest.usability.tasktree.filter;
|
---|
16 |
|
---|
17 | import java.util.Stack;
|
---|
18 |
|
---|
19 | import com.google.common.base.Predicate;
|
---|
20 |
|
---|
21 | import de.ugoe.cs.autoquest.eventcore.IEventTarget;
|
---|
22 | import de.ugoe.cs.autoquest.eventcore.IEventType;
|
---|
23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTree;
|
---|
24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * <p>
|
---|
28 | * TODO comment
|
---|
29 | * </p>
|
---|
30 | *
|
---|
31 | * @author Alexander Deicke
|
---|
32 | */
|
---|
33 | public class IterativeDFSFilterStrategy implements TaskTreeFilterStrategy {
|
---|
34 |
|
---|
35 | private FilterStatistic filterStatistic;
|
---|
36 |
|
---|
37 | @SuppressWarnings("unchecked")
|
---|
38 | @Override
|
---|
39 | public FilterStatistic filter(ITaskTree taskTree, EventTargetFilter eventTarget) {
|
---|
40 | Predicate<IEventTarget> filterPredicate = eventTarget.filterPredicate();
|
---|
41 | this.filterStatistic = new FilterStatistic(filterPredicate);
|
---|
42 | traverse(taskTree);
|
---|
43 | return this.filterStatistic;
|
---|
44 | }
|
---|
45 |
|
---|
46 | @SuppressWarnings("unchecked")
|
---|
47 | @Override
|
---|
48 | public FilterStatistic filter(ITaskTree taskTree, EventTypeFilter eventType) {
|
---|
49 | Predicate<IEventType> filterPredicate = eventType.filterPredicate();
|
---|
50 | this.filterStatistic = new FilterStatistic(filterPredicate);
|
---|
51 | traverse(taskTree);
|
---|
52 | return this.filterStatistic;
|
---|
53 | }
|
---|
54 |
|
---|
55 | @SuppressWarnings("unchecked")
|
---|
56 | @Override
|
---|
57 | public FilterStatistic filter(ITaskTree taskTree, TaskTreeNodeTypeFilter nodeType) {
|
---|
58 | Predicate<ITaskTreeNode> filterPredicate = nodeType.filterPredicate();
|
---|
59 | this.filterStatistic = new FilterStatistic(filterPredicate);
|
---|
60 | traverse(taskTree);
|
---|
61 | return this.filterStatistic;
|
---|
62 | }
|
---|
63 |
|
---|
64 | private void traverse(ITaskTree taskTree) {
|
---|
65 | Stack<ITaskTreeNode> unvisitedNodes = new Stack<ITaskTreeNode>();
|
---|
66 | unvisitedNodes.push(taskTree.getRoot());
|
---|
67 | while (stillUnvisitedNodes(unvisitedNodes)) {
|
---|
68 | ITaskTreeNode node = unvisitedNodes.pop();
|
---|
69 | processCurrentNode(node);
|
---|
70 | processChildrenOfCurrentNode(unvisitedNodes, node);
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | private boolean stillUnvisitedNodes(Stack<ITaskTreeNode> unvisitedNodes) {
|
---|
75 | return !unvisitedNodes.isEmpty();
|
---|
76 | }
|
---|
77 |
|
---|
78 | private void processCurrentNode(ITaskTreeNode node) {
|
---|
79 | this.filterStatistic.addNode(node);
|
---|
80 | }
|
---|
81 |
|
---|
82 | private void processChildrenOfCurrentNode(Stack<ITaskTreeNode> unvisitedNodes,
|
---|
83 | ITaskTreeNode node)
|
---|
84 | {
|
---|
85 | for (ITaskTreeNode child : node.getChildren()) {
|
---|
86 | unvisitedNodes.push(child);
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | }
|
---|