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.TextInput; |
---|
5 | import de.ugoe.cs.autoquest.eventcore.gui.ValueSelection; |
---|
6 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
7 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
8 | |
---|
9 | /** |
---|
10 | * <p> |
---|
11 | * This rule compares GUI event tasks (i.e. it is more concrete, than the |
---|
12 | * {@link EventTaskComparisonRule}). Two GUI event tasks are only equal if their event type and |
---|
13 | * target are equal. The returned equality is even more fine-grained for events whose type is |
---|
14 | * {@link TextInput} and {@link ValueSelection}. For text inputs, lexical equality is returned if |
---|
15 | * the same text is entered using the same key interactions. Syntactical equality is returned if |
---|
16 | * the same text is entered using different key interactions. Semantical equality is returned if |
---|
17 | * different text is entered, but into the same event target. Value selections are syntactically |
---|
18 | * equal, if the same value is selected. Otherwise they are semantically equal. |
---|
19 | * </p> |
---|
20 | * |
---|
21 | * @author Patrick Harms |
---|
22 | */ |
---|
23 | public class GUIEventTaskComparisonRule implements NodeComparisonRule { |
---|
24 | |
---|
25 | /* |
---|
26 | * (non-Javadoc) |
---|
27 | * |
---|
28 | * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode) |
---|
29 | */ |
---|
30 | @Override |
---|
31 | public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) { |
---|
32 | if ((!(node1 instanceof IEventTask)) || (!(node2 instanceof IEventTask))) { |
---|
33 | return null; |
---|
34 | } |
---|
35 | |
---|
36 | IEventTask task1 = (IEventTask) node1; |
---|
37 | IEventTask task2 = (IEventTask) node2; |
---|
38 | |
---|
39 | if ((!(task1.getEventType() instanceof IInteraction)) || |
---|
40 | (!(task2.getEventType() instanceof IInteraction))) |
---|
41 | { |
---|
42 | return null; |
---|
43 | } |
---|
44 | |
---|
45 | if (node1 == node2) { |
---|
46 | return NodeEquality.IDENTICAL; |
---|
47 | } |
---|
48 | |
---|
49 | if (!task1.getEventTarget().equals(task2.getEventTarget())) { |
---|
50 | return NodeEquality.UNEQUAL; |
---|
51 | } |
---|
52 | |
---|
53 | IInteraction interaction1 = (IInteraction) task1.getEventType(); |
---|
54 | IInteraction interaction2 = (IInteraction) task2.getEventType(); |
---|
55 | |
---|
56 | return compareInteractions(interaction1, interaction2); |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * <p> |
---|
61 | * compares two interactions. The method delegates two |
---|
62 | * {@link #compareTextInputs(TextInput, TextInput)} and |
---|
63 | * {@link #compareValueSelections(ValueSelection, ValueSelection)} for text inputs and value |
---|
64 | * selections. Otherwise it uses the equal method of the interactions for comparison. In this |
---|
65 | * case, if the interactions equal method returns true, this method returns lexical equality. |
---|
66 | * </p> |
---|
67 | * |
---|
68 | * @param interaction1 the first interaction to compare |
---|
69 | * @param interaction2 the second interaction to compare |
---|
70 | * |
---|
71 | * @return as described |
---|
72 | */ |
---|
73 | private NodeEquality compareInteractions(IInteraction interaction1, IInteraction interaction2) { |
---|
74 | if (interaction1 == interaction2) { |
---|
75 | return NodeEquality.LEXICALLY_EQUAL; |
---|
76 | } |
---|
77 | else if ((interaction1 instanceof TextInput) && (interaction2 instanceof TextInput)) { |
---|
78 | return compareTextInputs((TextInput) interaction1, (TextInput) interaction2); |
---|
79 | } |
---|
80 | else if ((interaction1 instanceof ValueSelection) && |
---|
81 | (interaction2 instanceof ValueSelection)) |
---|
82 | { |
---|
83 | return compareValueSelections |
---|
84 | ((ValueSelection<?>) interaction1, (ValueSelection<?>) interaction2); |
---|
85 | } |
---|
86 | else if (interaction1.equals(interaction2)) { |
---|
87 | return NodeEquality.LEXICALLY_EQUAL; |
---|
88 | } |
---|
89 | else { |
---|
90 | return NodeEquality.UNEQUAL; |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * <p> |
---|
96 | * compares two text inputs. If both text inputs have the same entered text and text input |
---|
97 | * events, they are lexically equal. If they only have the same entered text, they are |
---|
98 | * syntactically equal. If they are only both text inputs, they are semantically equal. |
---|
99 | * (the equality of the event targets is checked beforehand). |
---|
100 | * </p> |
---|
101 | * |
---|
102 | * @param interaction1 the first text input to compare |
---|
103 | * @param interaction2 the second text input to compare |
---|
104 | * |
---|
105 | * @return as described |
---|
106 | */ |
---|
107 | private NodeEquality compareTextInputs(TextInput interaction1, TextInput interaction2) { |
---|
108 | if (interaction1.getEnteredText().equals(interaction2.getEnteredText())) { |
---|
109 | if (interaction1.getTextInputEvents().equals(interaction2.getTextInputEvents())) { |
---|
110 | return NodeEquality.LEXICALLY_EQUAL; |
---|
111 | } |
---|
112 | else { |
---|
113 | return NodeEquality.SYNTACTICALLY_EQUAL; |
---|
114 | } |
---|
115 | } |
---|
116 | else { |
---|
117 | return NodeEquality.SEMANTICALLY_EQUAL; |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * <p> |
---|
123 | * compares two value selections. If both value selections have the same selected value, they |
---|
124 | * are syntactically equal, otherwise they are semantically equal. |
---|
125 | * (the equality of the event targets is checked beforehand). |
---|
126 | * </p> |
---|
127 | * |
---|
128 | * @param interaction1 the first value selection to compare |
---|
129 | * @param interaction2 the second value selection to compare |
---|
130 | * |
---|
131 | * @return as described |
---|
132 | */ |
---|
133 | private NodeEquality compareValueSelections(ValueSelection<?> interaction1, |
---|
134 | ValueSelection<?> interaction2) |
---|
135 | { |
---|
136 | Object value1 = interaction1.getSelectedValue(); |
---|
137 | Object value2 = interaction2.getSelectedValue(); |
---|
138 | |
---|
139 | if ((value1 == value2) || ((value1 != null) && (value1.equals(value2)))) { |
---|
140 | return NodeEquality.LEXICALLY_EQUAL; |
---|
141 | } |
---|
142 | else { |
---|
143 | return NodeEquality.SEMANTICALLY_EQUAL; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | } |
---|