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

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

Adjustments due to changed Visitor interface.

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