1 | package de.ugoe.cs.autoquest.tasktrees.nodeequality; |
---|
2 | |
---|
3 | import de.ugoe.cs.autoquest.eventcore.gui.IInteraction; |
---|
4 | import de.ugoe.cs.autoquest.eventcore.gui.MouseClick; |
---|
5 | import de.ugoe.cs.autoquest.eventcore.gui.MouseDoubleClick; |
---|
6 | import de.ugoe.cs.autoquest.eventcore.gui.MouseDragAndDrop; |
---|
7 | import de.ugoe.cs.autoquest.eventcore.gui.TextInput; |
---|
8 | import de.ugoe.cs.autoquest.eventcore.gui.ValueSelection; |
---|
9 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
10 | import 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 | */ |
---|
26 | public 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 | if (interaction1.getButton() != interaction2.getButton()) { |
---|
182 | return NodeEquality.UNEQUAL; |
---|
183 | } |
---|
184 | |
---|
185 | int x1 = interaction1.getX(); |
---|
186 | int x2 = interaction2.getX(); |
---|
187 | int y1 = interaction1.getY(); |
---|
188 | int y2 = interaction2.getY(); |
---|
189 | |
---|
190 | if ((x1 == x2) && (y1 == y2)) { |
---|
191 | return NodeEquality.LEXICALLY_EQUAL; |
---|
192 | } |
---|
193 | else { |
---|
194 | return NodeEquality.SEMANTICALLY_EQUAL; |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | /** |
---|
199 | * <p> |
---|
200 | * compares two mouse double clicks. If both double clicks have the same coordinates, they are |
---|
201 | * lexically equal. Otherwise, they are semantically equal. |
---|
202 | * </p> |
---|
203 | * |
---|
204 | * @param interaction1 the first mouse double click to compare |
---|
205 | * @param interaction2 the second mouse double click to compare |
---|
206 | * |
---|
207 | * @return as described |
---|
208 | */ |
---|
209 | private NodeEquality compareMouseDoubleClicks(MouseDoubleClick interaction1, |
---|
210 | MouseDoubleClick interaction2) |
---|
211 | { |
---|
212 | if (interaction1.getButton() != interaction2.getButton()) { |
---|
213 | return NodeEquality.UNEQUAL; |
---|
214 | } |
---|
215 | |
---|
216 | int x1 = interaction1.getX(); |
---|
217 | int x2 = interaction2.getX(); |
---|
218 | int y1 = interaction1.getY(); |
---|
219 | int y2 = interaction2.getY(); |
---|
220 | |
---|
221 | if ((x1 == x2) && (y1 == y2)) { |
---|
222 | return NodeEquality.LEXICALLY_EQUAL; |
---|
223 | } |
---|
224 | else { |
---|
225 | return NodeEquality.SEMANTICALLY_EQUAL; |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | /** |
---|
230 | * <p> |
---|
231 | * compares two mouse drag and drops. If both drag and drops have the same start and end |
---|
232 | * coordinates, they are lexically equal. Otherwise, they are semantically equal. |
---|
233 | * </p> |
---|
234 | * |
---|
235 | * @param interaction1 the first mouse drag and drop to compare |
---|
236 | * @param interaction2 the second mouse drag and drop to compare |
---|
237 | * |
---|
238 | * @return as described |
---|
239 | */ |
---|
240 | private NodeEquality compareMouseDragAndDrops(MouseDragAndDrop interaction1, |
---|
241 | MouseDragAndDrop interaction2) |
---|
242 | { |
---|
243 | if (interaction1.getButton() != interaction2.getButton()) { |
---|
244 | return NodeEquality.UNEQUAL; |
---|
245 | } |
---|
246 | |
---|
247 | int x1 = interaction1.getX(); |
---|
248 | int x1Start = interaction1.getXStart(); |
---|
249 | int x2 = interaction2.getX(); |
---|
250 | int x2Start = interaction2.getXStart(); |
---|
251 | int y1 = interaction1.getY(); |
---|
252 | int y1Start = interaction1.getYStart(); |
---|
253 | int y2 = interaction2.getY(); |
---|
254 | int y2Start = interaction2.getYStart(); |
---|
255 | |
---|
256 | if ((x1Start == x2Start) && (x1 == x2) && (y1Start == y2Start) && (y1 == y2)) { |
---|
257 | return NodeEquality.LEXICALLY_EQUAL; |
---|
258 | } |
---|
259 | else { |
---|
260 | return NodeEquality.SEMANTICALLY_EQUAL; |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | } |
---|