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