[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;
|
---|
| 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;
|
---|
[1152] | 28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
|
---|
[1160] | 29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskVisitor;
|
---|
[1150] | 30 | import de.ugoe.cs.autoquest.usability.tasktree.filters.EventTypeFilter;
|
---|
[1204] | 31 | import de.ugoe.cs.autoquest.usability.tasktree.filters.TaskTypeFilter;
|
---|
[1150] | 32 |
|
---|
| 33 | /**
|
---|
| 34 | * <p>
|
---|
| 35 | * TODO comment
|
---|
| 36 | * </p>
|
---|
| 37 | *
|
---|
| 38 | * @author Alexander Deicke
|
---|
| 39 | */
|
---|
[1204] | 40 | public abstract class InteractionPatternVisitor implements ITaskVisitor {
|
---|
[1150] | 41 |
|
---|
[1204] | 42 | protected TaskTypeFilter taskType;
|
---|
| 43 |
|
---|
[1150] | 44 | protected EventTypeFilter eventType;
|
---|
| 45 |
|
---|
[1204] | 46 | protected InteractionPattern containedPattern;
|
---|
[1150] | 47 |
|
---|
| 48 | protected boolean present = false;
|
---|
| 49 |
|
---|
[1204] | 50 | protected List<ITask> retainedSelectionTasks = Lists.newArrayList();
|
---|
[1150] | 51 |
|
---|
| 52 | /* (non-Javadoc)
|
---|
| 53 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.NodeVisitor#visit(de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask)
|
---|
| 54 | */
|
---|
| 55 | public void visit(IEventTask event) {
|
---|
| 56 | if(!this.present && isEventVisitor()) {
|
---|
| 57 | IEventType eventType = event.getEventType();
|
---|
| 58 | if(eventType instanceof StringEventType) {
|
---|
| 59 | this.present = eventType.toString().equals(nameOfEventType());
|
---|
| 60 | } else {
|
---|
| 61 | this.present = eventType.getClass().equals(this.eventType.clazz());
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | public boolean isEventVisitor() {
|
---|
| 67 | return this.eventType != null && this.containedPattern == null;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | protected String nameOfEventType() {
|
---|
| 71 | String ret = StringUtils.EMPTY;
|
---|
| 72 | Iterable<String> splitted = Splitter.on("_").split(this.eventType.name());
|
---|
| 73 | for(String str : splitted) {
|
---|
| 74 | str = str.toLowerCase();
|
---|
| 75 | ret += Character.toString(str.charAt(0)).toUpperCase() + str.substring(1);
|
---|
| 76 | }
|
---|
| 77 | return ret;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /* (non-Javadoc)
|
---|
[1160] | 81 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.TaskVisitor#accept(de.ugoe.cs.autoquest.tasktrees.treeifc.ITask)
|
---|
| 82 | */
|
---|
| 83 | @Override
|
---|
| 84 | public void visit(ITask task) {
|
---|
| 85 | task.accept(this);
|
---|
| 86 |
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /* (non-Javadoc)
|
---|
[1150] | 90 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.NodeVisitor#visit(de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection)
|
---|
| 91 | */
|
---|
| 92 | public void visit(ISelection selection) {
|
---|
| 93 | if(isEventVisitor()) {
|
---|
[1204] | 94 | retainNodesWherePatternIsPresent(selection);
|
---|
[1150] | 95 | this.present = patternIsPresent();
|
---|
| 96 | } else {
|
---|
[1160] | 97 | this.present = containedPattern.containedIn(selection);
|
---|
[1150] | 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[1204] | 101 | @SuppressWarnings("unchecked")
|
---|
| 102 | protected void retainNodesWherePatternIsPresent(ISelection selection) {
|
---|
| 103 | for(ITask task : selection.getChildren()) {
|
---|
[1150] | 104 | this.present = false;
|
---|
[1152] | 105 | task.accept(this);
|
---|
[1204] | 106 | if(this.present && this.taskType.filterPredicate().apply(selection)) {
|
---|
| 107 | this.retainedSelectionTasks.add(selection);
|
---|
| 108 | }
|
---|
[1150] | 109 | if(this.present) {
|
---|
[1204] | 110 | break;
|
---|
[1150] | 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | private boolean patternIsPresent() {
|
---|
[1204] | 116 | return !this.retainedSelectionTasks.isEmpty();
|
---|
[1150] | 117 | }
|
---|
| 118 |
|
---|
| 119 | /**
|
---|
| 120 | * <p>
|
---|
| 121 | * TODO: comment
|
---|
| 122 | * </p>
|
---|
| 123 | *
|
---|
| 124 | * @return
|
---|
| 125 | */
|
---|
| 126 | public boolean isPresent() {
|
---|
| 127 | return this.present;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | /**
|
---|
| 131 | * <p>
|
---|
| 132 | * TODO: comment
|
---|
| 133 | * </p>
|
---|
| 134 | *
|
---|
| 135 | */
|
---|
| 136 | public void reset() {
|
---|
[1204] | 137 | this.retainedSelectionTasks.clear();
|
---|
[1150] | 138 | this.present = false;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | /**
|
---|
| 142 | * <p>
|
---|
| 143 | * TODO: comment
|
---|
| 144 | * </p>
|
---|
| 145 | *
|
---|
| 146 | * @return
|
---|
| 147 | */
|
---|
| 148 | public boolean hasExcludedSelectionNodes() {
|
---|
| 149 | return patternIsPresent();
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | /**
|
---|
| 153 | * <p>
|
---|
| 154 | * TODO: comment
|
---|
| 155 | * </p>
|
---|
| 156 | *
|
---|
| 157 | * @return
|
---|
| 158 | */
|
---|
[1152] | 159 | public List<ITask> getRetainedSelectionNodes() {
|
---|
[1204] | 160 | return this.retainedSelectionTasks;
|
---|
[1150] | 161 | }
|
---|
| 162 |
|
---|
| 163 | }
|
---|