[1040] | 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 |
|
---|
[1030] | 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 |
|
---|
[1040] | 26 | /**
|
---|
| 27 | * <p>
|
---|
| 28 | * TODO comment
|
---|
| 29 | * </p>
|
---|
| 30 | *
|
---|
| 31 | * @author Alexander Deicke
|
---|
| 32 | */
|
---|
[1030] | 33 | public class IterativeDFSFilterStrategy implements TaskTreeFilterStrategy {
|
---|
[1040] | 34 |
|
---|
[1030] | 35 | private FilterStatistic filterStatistic;
|
---|
[1040] | 36 |
|
---|
[1030] | 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 | }
|
---|
[1040] | 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 |
|
---|
[1030] | 64 | private void traverse(ITaskTree taskTree) {
|
---|
| 65 | Stack<ITaskTreeNode> unvisitedNodes = new Stack<ITaskTreeNode>();
|
---|
| 66 | unvisitedNodes.push(taskTree.getRoot());
|
---|
[1040] | 67 | while (stillUnvisitedNodes(unvisitedNodes)) {
|
---|
[1030] | 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 | }
|
---|
[1040] | 81 |
|
---|
[1030] | 82 | private void processChildrenOfCurrentNode(Stack<ITaskTreeNode> unvisitedNodes,
|
---|
[1040] | 83 | ITaskTreeNode node)
|
---|
| 84 | {
|
---|
| 85 | for (ITaskTreeNode child : node.getChildren()) {
|
---|
[1030] | 86 | unvisitedNodes.push(child);
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | }
|
---|