source: trunk/autoquest-core-usability-evaluation/src/main/java/de/ugoe/cs/autoquest/usability/rules/patterns/InteractionPatternVisitor.java @ 1319

Last change on this file since 1319 was 1319, checked in by khartmann, 11 years ago
  • Reworked Filters to use the first instance of a task to provide type and target
  • Added a function to extract all tasks matching a given filter
  • Added simple console feedback for matched usability problems
  • Property svn:mime-type set to text/plain
File size: 6.7 KB
Line 
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
15package de.ugoe.cs.autoquest.usability.rules.patterns;
16
17import java.util.List;
18
19import org.apache.commons.lang.StringUtils;
20
21import com.google.common.base.Splitter;
22import com.google.common.collect.Iterables;
23import com.google.common.collect.Lists;
24
25import de.ugoe.cs.autoquest.eventcore.Event;
26import de.ugoe.cs.autoquest.eventcore.IEventTarget;
27import de.ugoe.cs.autoquest.eventcore.IEventType;
28import de.ugoe.cs.autoquest.eventcore.StringEventType;
29import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
30import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance;
31import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
32import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
33import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
34import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
35import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
36import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
37import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskVisitor;
38import de.ugoe.cs.autoquest.test.DummyGUIElement;
39import de.ugoe.cs.autoquest.usability.taskmodel.filter.types.EventTargetFilter;
40import de.ugoe.cs.autoquest.usability.taskmodel.filter.types.EventTypeFilter;
41import de.ugoe.cs.autoquest.usability.taskmodel.filter.types.TaskTypeFilter;
42
43/**
44 * <p>
45 * TODO comment
46 * </p>
47 *
48 * @author Alexander Deicke
49 */
50public abstract class InteractionPatternVisitor implements ITaskVisitor {
51
52    protected TaskTypeFilter taskType;
53
54    protected EventTypeFilter eventType;
55   
56    protected EventTargetFilter eventTarget;
57
58    protected InteractionPattern containedPattern;
59
60    protected boolean present = false;
61
62    protected List<ITask> retainedSelectionTasks = Lists.newArrayList();
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)
70     */
71    public void visit(IEventTask event) {
72        if (!this.present && isEventVisitor()) {
73                Event eventRepresentative = ((IEventTaskInstance) event.getInstances().iterator().next()).getEvent();
74                boolean matchesEventType = matchesEventType(eventRepresentative.getType());
75                boolean matchesEventTarget = matchesEventTarget(eventRepresentative.getTarget());
76                this.present = eventTarget != null ? matchesEventType && matchesEventTarget : matchesEventType;
77        }
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());
80    }
81
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() {
103        return this.eventType != null && this.containedPattern == null;
104    }
105
106    protected String nameOfEventType() {
107        String ret = StringUtils.EMPTY;
108        Iterable<String> splitted = Splitter.on("_").split(this.eventType.name());
109        for (String str : splitted) {
110            str = str.toLowerCase();
111            ret += Character.toString(str.charAt(0)).toUpperCase() + str.substring(1);
112        }
113        return ret;
114    }
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)
122     */
123    @Override
124    public void visit(ITask task) {
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        }
137    }
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)
145     */
146    public void visit(ISelection selection) {
147        if (isEventVisitor()) {
148            retainNodesWherePatternIsPresent(selection);
149            this.present = patternIsPresent();
150        }
151        else {
152            this.present = containedPattern.containedIn(selection);
153        }
154    }
155
156    @SuppressWarnings("unchecked")
157    protected void retainNodesWherePatternIsPresent(ISelection selection) {
158        for (ITask task : selection.getChildren()) {
159            this.present = false;
160            task.accept(this);
161            if (this.present && this.taskType.filterPredicate().apply(selection)) {
162                this.retainedSelectionTasks.add(selection);
163            }
164            if (this.present) {
165                break;
166            }
167        }
168    }
169
170    private boolean patternIsPresent() {
171        return !this.retainedSelectionTasks.isEmpty();
172    }
173
174    /**
175     * <p>
176     * TODO: comment
177     * </p>
178     *
179     * @return
180     */
181    public boolean isPresent() {
182        return this.present;
183    }
184
185    /**
186     * <p>
187     * TODO: comment
188     * </p>
189     *
190     */
191    public void reset() {
192        this.retainedSelectionTasks.clear();
193        this.present = false;
194    }
195
196    /**
197     * <p>
198     * TODO: comment
199     * </p>
200     *
201     * @return
202     */
203    public boolean hasExcludedSelectionNodes() {
204        return patternIsPresent();
205    }
206
207    /**
208     * <p>
209     * TODO: comment
210     * </p>
211     *
212     * @return
213     */
214    public List<ITask> getRetainedSelectionNodes() {
215        return this.retainedSelectionTasks;
216    }
217
218}
Note: See TracBrowser for help on using the repository browser.