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

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