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.rules.patterns;
|
---|
16 |
|
---|
17 | import java.util.List;
|
---|
18 |
|
---|
19 | import org.apache.commons.lang.StringUtils;
|
---|
20 |
|
---|
21 | import com.google.common.base.Splitter;
|
---|
22 | import com.google.common.collect.Lists;
|
---|
23 |
|
---|
24 | import de.ugoe.cs.autoquest.eventcore.IEventType;
|
---|
25 | import de.ugoe.cs.autoquest.eventcore.StringEventType;
|
---|
26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
|
---|
27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
|
---|
28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
|
---|
29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel;
|
---|
30 | import de.ugoe.cs.autoquest.usability.tasktree.filters.EventTypeFilter;
|
---|
31 | import de.ugoe.cs.autoquest.usability.util.PatternsVisitorUtil;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * <p>
|
---|
35 | * TODO comment
|
---|
36 | * </p>
|
---|
37 | *
|
---|
38 | * @author Alexander Deicke
|
---|
39 | */
|
---|
40 | public abstract class UsagePatternVisitor implements NodeVisitor {
|
---|
41 |
|
---|
42 | protected EventTypeFilter eventType;
|
---|
43 |
|
---|
44 | protected UsagePattern containedPattern;
|
---|
45 |
|
---|
46 | protected boolean present = false;
|
---|
47 |
|
---|
48 | protected List<ITask> retainedChildrenTasksFromSelections = Lists.newArrayList();
|
---|
49 |
|
---|
50 | /* (non-Javadoc)
|
---|
51 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.NodeVisitor#visit(de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask)
|
---|
52 | */
|
---|
53 | public void visit(IEventTask event) {
|
---|
54 | if(!this.present && isEventVisitor()) {
|
---|
55 | IEventType eventType = event.getEventType();
|
---|
56 | if(eventType instanceof StringEventType) {
|
---|
57 | this.present = eventType.toString().equals(nameOfEventType());
|
---|
58 | } else {
|
---|
59 | this.present = eventType.getClass().equals(this.eventType.clazz());
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public boolean isEventVisitor() {
|
---|
65 | return this.eventType != null && this.containedPattern == null;
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected String nameOfEventType() {
|
---|
69 | String ret = StringUtils.EMPTY;
|
---|
70 | Iterable<String> splitted = Splitter.on("_").split(this.eventType.name());
|
---|
71 | for(String str : splitted) {
|
---|
72 | str = str.toLowerCase();
|
---|
73 | ret += Character.toString(str.charAt(0)).toUpperCase() + str.substring(1);
|
---|
74 | }
|
---|
75 | return ret;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /* (non-Javadoc)
|
---|
79 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.NodeVisitor#visit(de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection)
|
---|
80 | */
|
---|
81 | public void visit(ISelection selection) {
|
---|
82 | if(isEventVisitor()) {
|
---|
83 | retainNodesWherePatternIsPresent(selection.getChildren());
|
---|
84 | this.present = patternIsPresent();
|
---|
85 | } else {
|
---|
86 | ITaskModel taskModel = PatternsVisitorUtil.createTaskModelFromNode(selection);
|
---|
87 | this.present = containedPattern.containedIn(taskModel);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected void retainNodesWherePatternIsPresent(List<ITask> children) {
|
---|
92 | for(ITask task : children) {
|
---|
93 | this.present = false;
|
---|
94 | task.accept(this);
|
---|
95 | if(this.present) {
|
---|
96 | this.retainedChildrenTasksFromSelections.add(task);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | private boolean patternIsPresent() {
|
---|
102 | return !this.retainedChildrenTasksFromSelections.isEmpty();
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * <p>
|
---|
107 | * TODO: comment
|
---|
108 | * </p>
|
---|
109 | *
|
---|
110 | * @return
|
---|
111 | */
|
---|
112 | public boolean isPresent() {
|
---|
113 | return this.present;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * <p>
|
---|
118 | * TODO: comment
|
---|
119 | * </p>
|
---|
120 | *
|
---|
121 | */
|
---|
122 | public void reset() {
|
---|
123 | this.retainedChildrenTasksFromSelections.clear();
|
---|
124 | this.present = false;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * <p>
|
---|
129 | * TODO: comment
|
---|
130 | * </p>
|
---|
131 | *
|
---|
132 | * @return
|
---|
133 | */
|
---|
134 | public boolean hasExcludedSelectionNodes() {
|
---|
135 | return patternIsPresent();
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * <p>
|
---|
140 | * TODO: comment
|
---|
141 | * </p>
|
---|
142 | *
|
---|
143 | * @return
|
---|
144 | */
|
---|
145 | public List<ITask> getRetainedSelectionNodes() {
|
---|
146 | return this.retainedChildrenTasksFromSelections;
|
---|
147 | }
|
---|
148 |
|
---|
149 | }
|
---|