[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.List;
|
---|
| 18 |
|
---|
| 19 | import org.apache.commons.lang.StringUtils;
|
---|
| 20 |
|
---|
| 21 | import com.google.common.base.Splitter;
|
---|
[1319] | 22 | import com.google.common.collect.Iterables;
|
---|
[1150] | 23 | import com.google.common.collect.Lists;
|
---|
| 24 |
|
---|
[1319] | 25 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
[1293] | 26 | import de.ugoe.cs.autoquest.eventcore.IEventTarget;
|
---|
[1150] | 27 | import de.ugoe.cs.autoquest.eventcore.IEventType;
|
---|
| 28 | import de.ugoe.cs.autoquest.eventcore.StringEventType;
|
---|
| 29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
|
---|
[1319] | 30 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance;
|
---|
[1217] | 31 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
|
---|
| 32 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
|
---|
[1150] | 33 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
|
---|
[1217] | 34 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
|
---|
[1152] | 35 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
|
---|
[1319] | 36 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
|
---|
[1160] | 37 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskVisitor;
|
---|
[1293] | 38 | import de.ugoe.cs.autoquest.test.DummyGUIElement;
|
---|
| 39 | import de.ugoe.cs.autoquest.usability.taskmodel.filter.types.EventTargetFilter;
|
---|
[1217] | 40 | import de.ugoe.cs.autoquest.usability.taskmodel.filter.types.EventTypeFilter;
|
---|
| 41 | import de.ugoe.cs.autoquest.usability.taskmodel.filter.types.TaskTypeFilter;
|
---|
[1150] | 42 |
|
---|
| 43 | /**
|
---|
| 44 | * <p>
|
---|
| 45 | * TODO comment
|
---|
| 46 | * </p>
|
---|
| 47 | *
|
---|
| 48 | * @author Alexander Deicke
|
---|
| 49 | */
|
---|
[1204] | 50 | public abstract class InteractionPatternVisitor implements ITaskVisitor {
|
---|
[1217] | 51 |
|
---|
| 52 | protected TaskTypeFilter taskType;
|
---|
| 53 |
|
---|
[1150] | 54 | protected EventTypeFilter eventType;
|
---|
[1293] | 55 |
|
---|
| 56 | protected EventTargetFilter eventTarget;
|
---|
[1217] | 57 |
|
---|
[1204] | 58 | protected InteractionPattern containedPattern;
|
---|
[1217] | 59 |
|
---|
[1150] | 60 | protected boolean present = false;
|
---|
[1217] | 61 |
|
---|
[1204] | 62 | protected List<ITask> retainedSelectionTasks = Lists.newArrayList();
|
---|
[1217] | 63 |
|
---|
| 64 | /*
|
---|
| 65 | * (non-Javadoc)
|
---|
| 66 | *
|
---|
| 67 | * @see
|
---|
| 68 | * de.ugoe.cs.autoquest.tasktrees.treeifc.NodeVisitor#visit(de.ugoe.cs.autoquest.tasktrees.treeifc
|
---|
| 69 | * .IEventTask)
|
---|
[1150] | 70 | */
|
---|
| 71 | public void visit(IEventTask event) {
|
---|
[1217] | 72 | if (!this.present && isEventVisitor()) {
|
---|
[1319] | 73 | Event eventRepresentative = ((IEventTaskInstance) event.getInstances().iterator().next()).getEvent();
|
---|
| 74 | boolean matchesEventType = matchesEventType(eventRepresentative.getType());
|
---|
| 75 | boolean matchesEventTarget = matchesEventTarget(eventRepresentative.getTarget());
|
---|
[1293] | 76 | this.present = eventTarget != null ? matchesEventType && matchesEventTarget : matchesEventType;
|
---|
[1150] | 77 | }
|
---|
[1319] | 78 | System.out.printf("%s [%s, %s, %s]: %s\n", event, this.eventType, this.eventTarget, this.taskType, this.present);
|
---|
| 79 | System.out.println(this.getClass().getSimpleName() + " "+ this.hashCode());
|
---|
[1150] | 80 | }
|
---|
[1217] | 81 |
|
---|
[1293] | 82 | private boolean matchesEventType(IEventType eventType) {
|
---|
| 83 | if (eventType instanceof StringEventType) {
|
---|
| 84 | return eventType.toString().equals(nameOfEventType());
|
---|
| 85 | }
|
---|
| 86 | else {
|
---|
| 87 | return eventType.getClass().equals(this.eventType.clazz());
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | private boolean matchesEventTarget(IEventTarget eventTarget) {
|
---|
| 92 | if(this.eventTarget != null) {
|
---|
| 93 | if(eventTarget instanceof DummyGUIElement) {
|
---|
| 94 | return false;
|
---|
| 95 | } else {
|
---|
| 96 | return eventTarget.getClass().equals(this.eventTarget.clazz());
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | return false;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | public boolean isEventVisitor() {
|
---|
[1150] | 103 | return this.eventType != null && this.containedPattern == null;
|
---|
| 104 | }
|
---|
[1217] | 105 |
|
---|
[1150] | 106 | protected String nameOfEventType() {
|
---|
| 107 | String ret = StringUtils.EMPTY;
|
---|
| 108 | Iterable<String> splitted = Splitter.on("_").split(this.eventType.name());
|
---|
[1217] | 109 | for (String str : splitted) {
|
---|
[1150] | 110 | str = str.toLowerCase();
|
---|
| 111 | ret += Character.toString(str.charAt(0)).toUpperCase() + str.substring(1);
|
---|
| 112 | }
|
---|
| 113 | return ret;
|
---|
| 114 | }
|
---|
[1217] | 115 |
|
---|
| 116 | /*
|
---|
| 117 | * (non-Javadoc)
|
---|
| 118 | *
|
---|
| 119 | * @see
|
---|
| 120 | * de.ugoe.cs.autoquest.tasktrees.treeifc.TaskVisitor#accept(de.ugoe.cs.autoquest.tasktrees.
|
---|
| 121 | * treeifc.ITask)
|
---|
[1160] | 122 | */
|
---|
| 123 | @Override
|
---|
| 124 | public void visit(ITask task) {
|
---|
[1217] | 125 | if (task instanceof ISequence) {
|
---|
| 126 | this.visit((ISequence) task);
|
---|
| 127 | }
|
---|
| 128 | else if (task instanceof IIteration) {
|
---|
| 129 | this.visit((IIteration) task);
|
---|
| 130 | }
|
---|
| 131 | else if (task instanceof ISelection) {
|
---|
| 132 | this.visit((ISelection) task);
|
---|
| 133 | }
|
---|
| 134 | else {
|
---|
| 135 | this.visit((IOptional) task);
|
---|
| 136 | }
|
---|
[1160] | 137 | }
|
---|
[1217] | 138 |
|
---|
| 139 | /*
|
---|
| 140 | * (non-Javadoc)
|
---|
| 141 | *
|
---|
| 142 | * @see
|
---|
| 143 | * de.ugoe.cs.autoquest.tasktrees.treeifc.NodeVisitor#visit(de.ugoe.cs.autoquest.tasktrees.treeifc
|
---|
| 144 | * .ISelection)
|
---|
[1150] | 145 | */
|
---|
| 146 | public void visit(ISelection selection) {
|
---|
[1217] | 147 | if (isEventVisitor()) {
|
---|
[1204] | 148 | retainNodesWherePatternIsPresent(selection);
|
---|
[1150] | 149 | this.present = patternIsPresent();
|
---|
| 150 | }
|
---|
[1217] | 151 | else {
|
---|
| 152 | this.present = containedPattern.containedIn(selection);
|
---|
| 153 | }
|
---|
[1150] | 154 | }
|
---|
| 155 |
|
---|
[1204] | 156 | @SuppressWarnings("unchecked")
|
---|
[1217] | 157 | protected void retainNodesWherePatternIsPresent(ISelection selection) {
|
---|
| 158 | for (ITask task : selection.getChildren()) {
|
---|
[1150] | 159 | this.present = false;
|
---|
[1152] | 160 | task.accept(this);
|
---|
[1217] | 161 | if (this.present && this.taskType.filterPredicate().apply(selection)) {
|
---|
[1204] | 162 | this.retainedSelectionTasks.add(selection);
|
---|
| 163 | }
|
---|
[1217] | 164 | if (this.present) {
|
---|
| 165 | break;
|
---|
[1150] | 166 | }
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
[1217] | 169 |
|
---|
[1150] | 170 | private boolean patternIsPresent() {
|
---|
[1204] | 171 | return !this.retainedSelectionTasks.isEmpty();
|
---|
[1150] | 172 | }
|
---|
[1217] | 173 |
|
---|
[1150] | 174 | /**
|
---|
| 175 | * <p>
|
---|
| 176 | * TODO: comment
|
---|
| 177 | * </p>
|
---|
[1217] | 178 | *
|
---|
[1150] | 179 | * @return
|
---|
| 180 | */
|
---|
| 181 | public boolean isPresent() {
|
---|
| 182 | return this.present;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | /**
|
---|
| 186 | * <p>
|
---|
| 187 | * TODO: comment
|
---|
| 188 | * </p>
|
---|
[1217] | 189 | *
|
---|
[1150] | 190 | */
|
---|
| 191 | public void reset() {
|
---|
[1204] | 192 | this.retainedSelectionTasks.clear();
|
---|
[1150] | 193 | this.present = false;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | /**
|
---|
| 197 | * <p>
|
---|
| 198 | * TODO: comment
|
---|
| 199 | * </p>
|
---|
[1217] | 200 | *
|
---|
[1150] | 201 | * @return
|
---|
| 202 | */
|
---|
| 203 | public boolean hasExcludedSelectionNodes() {
|
---|
| 204 | return patternIsPresent();
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | /**
|
---|
| 208 | * <p>
|
---|
| 209 | * TODO: comment
|
---|
| 210 | * </p>
|
---|
[1217] | 211 | *
|
---|
[1150] | 212 | * @return
|
---|
| 213 | */
|
---|
[1152] | 214 | public List<ITask> getRetainedSelectionNodes() {
|
---|
[1204] | 215 | return this.retainedSelectionTasks;
|
---|
[1150] | 216 | }
|
---|
[1217] | 217 |
|
---|
[1150] | 218 | }
|
---|