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.Arrays;
|
---|
18 | import java.util.List;
|
---|
19 |
|
---|
20 | import com.google.common.base.Optional;
|
---|
21 | import com.google.common.base.Predicate;
|
---|
22 | import com.google.common.collect.Iterables;
|
---|
23 |
|
---|
24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
|
---|
25 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel;
|
---|
26 | import de.ugoe.cs.autoquest.usability.tasktree.IterativeDFSFilterStrategy;
|
---|
27 | import de.ugoe.cs.autoquest.usability.tasktree.filters.TaskModelFilter;
|
---|
28 | import de.ugoe.cs.autoquest.usability.tasktree.filters.TaskTypeFilter;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * <p>
|
---|
32 | * TODO comment
|
---|
33 | * </p>
|
---|
34 | *
|
---|
35 | * @author Alexander Deicke
|
---|
36 | */
|
---|
37 | public class UsagePattern {
|
---|
38 |
|
---|
39 | private TaskModelFilter taskTreeFilter = new TaskModelFilter(new IterativeDFSFilterStrategy());
|
---|
40 |
|
---|
41 | private TaskTypeFilter concernedTask;
|
---|
42 |
|
---|
43 | private List<UsagePatternVisitor> patternVisitors;
|
---|
44 |
|
---|
45 | private boolean present = false;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * <p>
|
---|
49 | * TODO: comment
|
---|
50 | * </p>
|
---|
51 | *
|
---|
52 | * @param concernedNode
|
---|
53 | * @param eventType
|
---|
54 | */
|
---|
55 | public UsagePattern(TaskTypeFilter concernedNode,
|
---|
56 | UsagePatternVisitor... patternVisitor)
|
---|
57 | {
|
---|
58 | this.patternVisitors = Arrays.asList(patternVisitor);
|
---|
59 | this.concernedTask = concernedNode;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public boolean containedIn(ITaskModel taskModel) {
|
---|
63 | List<ITask> allConcernedTasks = filterAllConcernedTasksFrom(taskModel);
|
---|
64 | for(ITask concernedTask : allConcernedTasks) {
|
---|
65 | checkTask(concernedTask);
|
---|
66 | if(this.present) break;
|
---|
67 | }
|
---|
68 | return this.present;
|
---|
69 | }
|
---|
70 |
|
---|
71 | private void checkTask(ITask concernedTask) {
|
---|
72 | applyAllVisitors(concernedTask);
|
---|
73 | if(allVisitorsArePresent()) {
|
---|
74 | this.present = true;
|
---|
75 | } else {
|
---|
76 | resetAllVisitors();
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | public boolean containedIn(ITask task) {
|
---|
81 | checkTask(task);
|
---|
82 | return this.present;
|
---|
83 | }
|
---|
84 |
|
---|
85 | private void applyAllVisitors(ITask concernedTask) {
|
---|
86 | Optional<UsagePatternVisitor> previousVisitor = Optional.absent();
|
---|
87 | for(UsagePatternVisitor visitor : patternVisitors) {
|
---|
88 | if(appliedOnSelectionNode(previousVisitor)) {
|
---|
89 | for(ITask selection : previousVisitor.get().getRetainedSelectionNodes()) {
|
---|
90 | selection.accept(visitor);
|
---|
91 | }
|
---|
92 | } else {
|
---|
93 | previousVisitor = Optional.of(visitor);
|
---|
94 | concernedTask.accept(visitor);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | private boolean appliedOnSelectionNode(Optional<UsagePatternVisitor> previousVisitor) {
|
---|
100 | return previousVisitor.isPresent() && previousVisitor.get().hasExcludedSelectionNodes();
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * <p>
|
---|
105 | * TODO: comment
|
---|
106 | * </p>
|
---|
107 | *
|
---|
108 | * @param taskTree
|
---|
109 | * @return
|
---|
110 | */
|
---|
111 | private List<ITask> filterAllConcernedTasksFrom(ITaskModel taskModel) {
|
---|
112 | return this.taskTreeFilter.filterByNodeType(this.concernedTask).from(taskModel).tasksMatchedFilter();
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * <p>
|
---|
117 | * TODO: comment
|
---|
118 | * </p>
|
---|
119 | *
|
---|
120 | * @return
|
---|
121 | */
|
---|
122 | private boolean allVisitorsArePresent() {
|
---|
123 | Iterable<UsagePatternVisitor> allPresent = Iterables.filter(this.patternVisitors, new Predicate<UsagePatternVisitor>() {
|
---|
124 |
|
---|
125 | public boolean apply(UsagePatternVisitor visitor) {
|
---|
126 | return visitor.isPresent();
|
---|
127 | }
|
---|
128 |
|
---|
129 | });
|
---|
130 | return Iterables.size(allPresent) == this.patternVisitors.size();
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * <p>
|
---|
135 | * TODO: comment
|
---|
136 | * </p>
|
---|
137 | *
|
---|
138 | */
|
---|
139 | private void resetAllVisitors() {
|
---|
140 | for(UsagePatternVisitor visitor : this.patternVisitors) {
|
---|
141 | visitor.reset();
|
---|
142 | }
|
---|
143 |
|
---|
144 | }
|
---|
145 |
|
---|
146 | }
|
---|