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

Last change on this file since 1293 was 1293, checked in by adeicke, 11 years ago

Added possibility to consider event target in interaction patterns.

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