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