source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/nodeequality/GUIEventTaskComparisonRule.java @ 1056

Last change on this file since 1056 was 1056, checked in by pharms, 11 years ago
  • extended comparison to also correctly compare mouse double clicks
File size: 9.4 KB
Line 
1package de.ugoe.cs.autoquest.tasktrees.nodeequality;
2
3import de.ugoe.cs.autoquest.eventcore.gui.IInteraction;
4import de.ugoe.cs.autoquest.eventcore.gui.MouseClick;
5import de.ugoe.cs.autoquest.eventcore.gui.MouseDoubleClick;
6import de.ugoe.cs.autoquest.eventcore.gui.MouseDragAndDrop;
7import de.ugoe.cs.autoquest.eventcore.gui.TextInput;
8import de.ugoe.cs.autoquest.eventcore.gui.ValueSelection;
9import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
10import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
11
12/**
13 * <p>
14 * This rule compares GUI event tasks (i.e. it is more concrete, than the
15 * {@link EventTaskComparisonRule}). Two GUI event tasks are only equal if their event type and
16 * target are equal. The returned equality is even more fine-grained for events whose type is
17 * {@link TextInput} and {@link ValueSelection}. For text inputs, lexical equality is returned if
18 * the same text is entered using the same key interactions. Syntactical equality is returned if
19 * the same text is entered using different key interactions. Semantical equality is returned if
20 * different text is entered, but into the same event target. Value selections are syntactically
21 * equal, if the same value is selected. Otherwise they are semantically equal.
22 * </p>
23 *
24 * @author Patrick Harms
25 */
26public class GUIEventTaskComparisonRule implements NodeComparisonRule {
27   
28    /*
29     * (non-Javadoc)
30     *
31     * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode)
32     */
33    @Override
34    public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) {
35        if ((!(node1 instanceof IEventTask)) || (!(node2 instanceof IEventTask))) {
36            return null;
37        }
38       
39        IEventTask task1 = (IEventTask) node1;
40        IEventTask task2 = (IEventTask) node2;
41       
42        if ((!(task1.getEventType() instanceof IInteraction)) ||
43            (!(task2.getEventType() instanceof IInteraction)))
44        {
45            return null;
46        }
47
48        if (node1 == node2) {
49            return NodeEquality.IDENTICAL;
50        }
51
52        if (!task1.getEventTarget().equals(task2.getEventTarget())) {
53            return NodeEquality.UNEQUAL;
54        }
55       
56        IInteraction interaction1 = (IInteraction) task1.getEventType();
57        IInteraction interaction2 = (IInteraction) task2.getEventType();
58       
59        return compareInteractions(interaction1, interaction2);
60    }
61
62    /**
63     * <p>
64     * compares two interactions. The method delegates two
65     * {@link #compareTextInputs(TextInput, TextInput)} and
66     * {@link #compareValueSelections(ValueSelection, ValueSelection)} for text inputs and value
67     * selections. Otherwise it uses the equal method of the interactions for comparison. In this
68     * case, if the interactions equal method returns true, this method returns lexical equality.
69     * </p>
70     *
71     * @param interaction1 the first interaction to compare
72     * @param interaction2 the second interaction to compare
73     *
74     * @return as described
75     */
76    private NodeEquality compareInteractions(IInteraction interaction1, IInteraction interaction2) {
77        if (interaction1 == interaction2) {
78            return NodeEquality.LEXICALLY_EQUAL;
79        }
80        else if ((interaction1 instanceof TextInput) && (interaction2 instanceof TextInput)) {
81            return compareTextInputs((TextInput) interaction1, (TextInput) interaction2);
82        }
83        else if ((interaction1 instanceof ValueSelection) &&
84                 (interaction2 instanceof ValueSelection))
85        {
86            return compareValueSelections
87                ((ValueSelection<?>) interaction1, (ValueSelection<?>) interaction2);
88        }
89        else if ((interaction1 instanceof MouseClick) &&
90                 (interaction2 instanceof MouseClick))
91        {
92            return compareMouseClicks((MouseClick) interaction1, (MouseClick) interaction2);
93        }
94        else if ((interaction1 instanceof MouseDoubleClick) &&
95                 (interaction2 instanceof MouseDoubleClick))
96        {
97            return compareMouseDoubleClicks
98                ((MouseDoubleClick) interaction1, (MouseDoubleClick) interaction2);
99        }
100        else if ((interaction1 instanceof MouseDragAndDrop) &&
101                 (interaction2 instanceof MouseDragAndDrop))
102        {
103            return compareMouseDragAndDrops
104                ((MouseDragAndDrop) interaction1, (MouseDragAndDrop) interaction2);
105        }
106        else if (interaction1.equals(interaction2)) {
107            return NodeEquality.LEXICALLY_EQUAL;
108        }
109        else {
110            return NodeEquality.UNEQUAL;
111        }
112    }
113
114    /**
115     * <p>
116     * compares two text inputs. If both text inputs have the same entered text and text input
117     * events, they are lexically equal. If they only have the same entered text, they are
118     * syntactically equal. If they are only both text inputs, they are semantically equal.
119     * (the equality of the event targets is checked beforehand).
120     * </p>
121     *
122     * @param interaction1 the first text input to compare
123     * @param interaction2 the second text input to compare
124     *
125     * @return as described
126     */
127    private NodeEquality compareTextInputs(TextInput interaction1, TextInput interaction2) {
128        if (interaction1.getEnteredText().equals(interaction2.getEnteredText())) {
129            if (interaction1.getTextInputEvents().equals(interaction2.getTextInputEvents())) {
130                return NodeEquality.LEXICALLY_EQUAL;
131            }
132            else {
133                return NodeEquality.SYNTACTICALLY_EQUAL;
134            }
135        }
136        else {
137            return NodeEquality.SEMANTICALLY_EQUAL;
138        }
139    }
140
141    /**
142     * <p>
143     * compares two value selections. If both value selections have the same selected value, they
144     * are syntactically equal, otherwise they are semantically equal.
145     * (the equality of the event targets is checked beforehand).
146     * </p>
147     *
148     * @param interaction1 the first value selection to compare
149     * @param interaction2 the second value selection to compare
150     *
151     * @return as described
152     */
153    private NodeEquality compareValueSelections(ValueSelection<?> interaction1,
154                                                ValueSelection<?> interaction2)
155    {
156        Object value1 = interaction1.getSelectedValue();
157        Object value2 = interaction2.getSelectedValue();
158       
159        if ((value1 == value2) || ((value1 != null) && (value1.equals(value2)))) {
160            return NodeEquality.LEXICALLY_EQUAL;
161        }
162        else {
163            return NodeEquality.SEMANTICALLY_EQUAL;
164        }
165    }
166
167    /**
168     * <p>
169     * compares two mouse clicks. If both clicks have the same coordinates, they are lexically
170     * equal. Otherwise, they are semantically equal.
171     * </p>
172     *
173     * @param interaction1 the first mouse click to compare
174     * @param interaction2 the second mouse click to compare
175     *
176     * @return as described
177     */
178    private NodeEquality compareMouseClicks(MouseClick interaction1,
179                                            MouseClick interaction2)
180    {
181        int x1 = interaction1.getX();
182        int x2 = interaction2.getX();
183        int y1 = interaction1.getY();
184        int y2 = interaction2.getY();
185       
186        if ((x1 == x2) && (y1 == y2)) {
187            return NodeEquality.LEXICALLY_EQUAL;
188        }
189        else {
190            return NodeEquality.SEMANTICALLY_EQUAL;
191        }
192    }
193
194    /**
195     * <p>
196     * compares two mouse double clicks. If both double clicks have the same coordinates, they are
197     * lexically equal. Otherwise, they are semantically equal.
198     * </p>
199     *
200     * @param interaction1 the first mouse double click to compare
201     * @param interaction2 the second mouse double click to compare
202     *
203     * @return as described
204     */
205    private NodeEquality compareMouseDoubleClicks(MouseDoubleClick interaction1,
206                                                  MouseDoubleClick interaction2)
207    {
208        int x1 = interaction1.getX();
209        int x2 = interaction2.getX();
210        int y1 = interaction1.getY();
211        int y2 = interaction2.getY();
212       
213        if ((x1 == x2) && (y1 == y2)) {
214            return NodeEquality.LEXICALLY_EQUAL;
215        }
216        else {
217            return NodeEquality.SEMANTICALLY_EQUAL;
218        }
219    }
220
221    /**
222     * <p>
223     * compares two mouse drag and drops. If both drag and drops have the same start and end
224     * coordinates, they are lexically equal. Otherwise, they are semantically equal.
225     * </p>
226     *
227     * @param interaction1 the first mouse drag and drop to compare
228     * @param interaction2 the second mouse drag and drop to compare
229     *
230     * @return as described
231     */
232    private NodeEquality compareMouseDragAndDrops(MouseDragAndDrop interaction1,
233                                                  MouseDragAndDrop interaction2)
234    {
235        int x1 = interaction1.getX();
236        int x1Start = interaction1.getXStart();
237        int x2 = interaction2.getX();
238        int x2Start = interaction2.getXStart();
239        int y1 = interaction1.getY();
240        int y1Start = interaction1.getYStart();
241        int y2 = interaction2.getY();
242        int y2Start = interaction2.getYStart();
243       
244        if ((x1Start == x2Start) && (x1 == x2) && (y1Start == y2Start) && (y1 == y2)) {
245            return NodeEquality.LEXICALLY_EQUAL;
246        }
247        else {
248            return NodeEquality.SEMANTICALLY_EQUAL;
249        }
250    }
251
252}
Note: See TracBrowser for help on using the repository browser.