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

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

Added usage patterns and mechanism for detecting them.

  • Property svn:mime-type set to text/plain
File size: 4.6 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.ISelection;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTree;
29import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
30import de.ugoe.cs.autoquest.tasktrees.treeifc.NodeVisitor;
31import de.ugoe.cs.autoquest.usability.tasktree.filters.EventTypeFilter;
32import de.ugoe.cs.autoquest.usability.util.PatternsVisitorUtil;
33
34/**
35 * <p>
36 * TODO comment
37 * </p>
38 *
39 * @author Alexander Deicke
40 */
41public abstract class UsagePatternVisitor implements NodeVisitor {
42   
43    protected EventTypeFilter eventType;
44   
45    protected UsagePattern containedPattern;
46   
47    protected boolean present = false;
48   
49    protected List<ITaskTreeNode> retainedChildrenNodesFromSelectionNodes = Lists.newArrayList();
50   
51    /* (non-Javadoc)
52     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.NodeVisitor#visit(de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask)
53     */
54    public void visit(IEventTask event) {
55        if(!this.present && isEventVisitor()) {
56            IEventType eventType = event.getEventType();
57            if(eventType instanceof StringEventType) {
58                this.present = eventType.toString().equals(nameOfEventType());
59            } else {
60                this.present = eventType.getClass().equals(this.eventType.clazz());
61            }
62        }
63    }
64   
65    public boolean isEventVisitor() {
66        return this.eventType != null && this.containedPattern == null;
67    }
68   
69    protected String nameOfEventType() {
70        String ret = StringUtils.EMPTY;
71        Iterable<String> splitted = Splitter.on("_").split(this.eventType.name());
72        for(String str : splitted) {
73            str = str.toLowerCase();
74            ret += Character.toString(str.charAt(0)).toUpperCase() + str.substring(1);
75        }
76        return ret;
77    }
78   
79    /* (non-Javadoc)
80     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.NodeVisitor#visit(de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection)
81     */
82    public void visit(ISelection selection) {
83        if(isEventVisitor()) {
84            retainNodesWherePatternIsPresent(selection.getChildren());
85            this.present = patternIsPresent();
86        } else {
87            ITaskTree taskTree = PatternsVisitorUtil.createTaskTreeFromNode(selection);
88            this.present = containedPattern.containedIn(taskTree); 
89        }
90    }
91
92    protected void retainNodesWherePatternIsPresent(List<ITaskTreeNode> children) {
93        for(ITaskTreeNode node : children) {
94            this.present = false;
95            node.accept(this);
96            if(this.present) {
97                this.retainedChildrenNodesFromSelectionNodes.add(node);
98            }
99        }
100    }
101   
102    private boolean patternIsPresent() {
103        return !this.retainedChildrenNodesFromSelectionNodes.isEmpty();
104    }
105   
106    /**
107     * <p>
108     * TODO: comment
109     * </p>
110     *
111     * @return
112     */
113    public boolean isPresent() {
114        return this.present;
115    }
116
117    /**
118     * <p>
119     * TODO: comment
120     * </p>
121     *
122     */
123    public void reset() {
124        this.retainedChildrenNodesFromSelectionNodes.clear();
125        this.present = false;
126    }
127
128    /**
129     * <p>
130     * TODO: comment
131     * </p>
132     *
133     * @return
134     */
135    public boolean hasExcludedSelectionNodes() {
136        return patternIsPresent();
137    }
138
139    /**
140     * <p>
141     * TODO: comment
142     * </p>
143     *
144     * @return
145     */
146    public List<ITaskTreeNode> getRetainedSelectionNodes() {
147        return this.retainedChildrenNodesFromSelectionNodes;
148    }
149   
150}
Note: See TracBrowser for help on using the repository browser.