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 | |
---|
15 | package de.ugoe.cs.autoquest.tasktrees.taskequality; |
---|
16 | |
---|
17 | import java.util.List; |
---|
18 | |
---|
19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance; |
---|
21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
23 | |
---|
24 | /** |
---|
25 | * <p> |
---|
26 | * This rule is capable of comparing sequences. If both sequences do not have children, they are |
---|
27 | * treated as lexically equal. Sequences are lexically equal, if they have the same number and |
---|
28 | * order of lexically equal children. The rule can not decide, if two sequences are syntactically |
---|
29 | * or semantically equal. |
---|
30 | * </p> |
---|
31 | * |
---|
32 | * @version $Revision: $ $Date: 19.02.2012$ |
---|
33 | * @author 2012, last modified by $Author: patrick$ |
---|
34 | */ |
---|
35 | public class SequenceComparisonRule implements TaskComparisonRule { |
---|
36 | |
---|
37 | /* (non-Javadoc) |
---|
38 | * @see TaskComparisonRule#isApplicable(ITask, ITask) |
---|
39 | */ |
---|
40 | @Override |
---|
41 | public boolean isApplicable(ITask task1, ITask task2) { |
---|
42 | return (task1 instanceof ISequence) && (task2 instanceof ISequence); |
---|
43 | } |
---|
44 | |
---|
45 | /* (non-Javadoc) |
---|
46 | * @see TaskComparisonRule#areLexicallyEqual(ITask, ITask) |
---|
47 | */ |
---|
48 | @Override |
---|
49 | public boolean areLexicallyEqual(ITask task1, ITask task2) { |
---|
50 | TaskEquality equality = getEquality(task1, task2, TaskEquality.LEXICALLY_EQUAL); |
---|
51 | return (equality != null) && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL)); |
---|
52 | } |
---|
53 | |
---|
54 | /* (non-Javadoc) |
---|
55 | * @see TaskComparisonRule#areSyntacticallyEqual(ITask, ITask) |
---|
56 | */ |
---|
57 | @Override |
---|
58 | public boolean areSyntacticallyEqual(ITask task1, ITask task2) { |
---|
59 | TaskEquality equality = getEquality(task1, task2, TaskEquality.SYNTACTICALLY_EQUAL); |
---|
60 | return (equality != null) && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL)); |
---|
61 | } |
---|
62 | |
---|
63 | /* (non-Javadoc) |
---|
64 | * @see TaskComparisonRule#areSemanticallyEqual(ITask, ITask) |
---|
65 | */ |
---|
66 | @Override |
---|
67 | public boolean areSemanticallyEqual(ITask task1, ITask task2) { |
---|
68 | TaskEquality equality = getEquality(task1, task2, TaskEquality.SEMANTICALLY_EQUAL); |
---|
69 | return (equality != null) && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)); |
---|
70 | } |
---|
71 | |
---|
72 | /* (non-Javadoc) |
---|
73 | * @see TaskComparisonRule#compare(ITask, ITask) |
---|
74 | */ |
---|
75 | @Override |
---|
76 | public TaskEquality compare(ITask task1, ITask task2) { |
---|
77 | return getEquality(task1, task2, null); |
---|
78 | } |
---|
79 | |
---|
80 | /* (non-Javadoc) |
---|
81 | * @see TaskComparisonRule#isApplicable(ITaskInstance, ITaskInstance) |
---|
82 | */ |
---|
83 | @Override |
---|
84 | public boolean isApplicable(ITaskInstance instance1, ITaskInstance instance2) { |
---|
85 | return isApplicable(instance1.getTask(), instance2.getTask()); |
---|
86 | } |
---|
87 | |
---|
88 | /* (non-Javadoc) |
---|
89 | * @see TaskComparisonRule#areLexicallyEqual(ITaskInstance, ITaskInstance) |
---|
90 | */ |
---|
91 | @Override |
---|
92 | public boolean areLexicallyEqual(ITaskInstance instance1, ITaskInstance instance2) { |
---|
93 | TaskEquality equality = getEquality(instance1, instance2, TaskEquality.LEXICALLY_EQUAL); |
---|
94 | return (equality != null) && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL)); |
---|
95 | } |
---|
96 | |
---|
97 | /* (non-Javadoc) |
---|
98 | * @see TaskComparisonRule#areSyntacticallyEqual(ITaskInstance, ITaskInstance) |
---|
99 | */ |
---|
100 | @Override |
---|
101 | public boolean areSyntacticallyEqual(ITaskInstance instance1, ITaskInstance instance2) { |
---|
102 | TaskEquality equality = getEquality(instance1, instance2, TaskEquality.SYNTACTICALLY_EQUAL); |
---|
103 | return (equality != null) && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL)); |
---|
104 | } |
---|
105 | |
---|
106 | /* (non-Javadoc) |
---|
107 | * @see TaskComparisonRule#areSemanticallyEqual(ITaskInstance, ITaskInstance) |
---|
108 | */ |
---|
109 | @Override |
---|
110 | public boolean areSemanticallyEqual(ITaskInstance instance1, ITaskInstance instance2) { |
---|
111 | TaskEquality equality = getEquality(instance1, instance2, TaskEquality.SEMANTICALLY_EQUAL); |
---|
112 | return (equality != null) && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)); |
---|
113 | } |
---|
114 | |
---|
115 | /* (non-Javadoc) |
---|
116 | * @see TaskComparisonRule#compare(ITaskInstance, ITaskInstance) |
---|
117 | */ |
---|
118 | @Override |
---|
119 | public TaskEquality compare(ITaskInstance instance1, ITaskInstance instance2) { |
---|
120 | return getEquality(instance1, instance2, null); |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * <p> |
---|
125 | * compares two sequences with each other checking for the provided required level of |
---|
126 | * equality. If this level is ensured, the method immediately returns. The more concrete |
---|
127 | * the required equality level, the more checks this method performs. |
---|
128 | * </p> |
---|
129 | * |
---|
130 | * @param task1 the first task to be compared |
---|
131 | * @param task2 the second task to be compared |
---|
132 | * @param requiredEqualityLevel the equality level to be checked for |
---|
133 | * |
---|
134 | * @return the determined equality. |
---|
135 | */ |
---|
136 | private TaskEquality getEquality(ITask task1, ITask task2, TaskEquality requiredEqualityLevel) { |
---|
137 | List<ITask> children1 = ((ISequence) task1).getChildren(); |
---|
138 | List<ITask> children2 = ((ISequence) task2).getChildren(); |
---|
139 | |
---|
140 | // if both sequences do not have children, they are equal although this doesn't make sense |
---|
141 | if ((children1.size() == 0) && (children2.size() == 0)) { |
---|
142 | return TaskEquality.LEXICALLY_EQUAL; |
---|
143 | } |
---|
144 | |
---|
145 | if (children1.size() != children2.size()) { |
---|
146 | return TaskEquality.UNEQUAL; |
---|
147 | } |
---|
148 | |
---|
149 | TaskEquality resultingEquality = TaskEquality.LEXICALLY_EQUAL; |
---|
150 | for (int i = 0; i < children1.size(); i++) { |
---|
151 | ITask child1 = children1.get(i); |
---|
152 | ITask child2 = children2.get(i); |
---|
153 | |
---|
154 | TaskEquality taskEquality = callRuleManager(child1, child2, requiredEqualityLevel); |
---|
155 | |
---|
156 | if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) { |
---|
157 | return TaskEquality.UNEQUAL; |
---|
158 | } |
---|
159 | |
---|
160 | resultingEquality = resultingEquality.getCommonDenominator(taskEquality); |
---|
161 | } |
---|
162 | |
---|
163 | return resultingEquality; |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * <p> |
---|
168 | * used to to call the task equality rule manager for the comparison of the two provided |
---|
169 | * children. If no required equality level is provided, than the most concrete equality is |
---|
170 | * returned. Otherwise, the required equality is returned as long as the children are equal |
---|
171 | * on that level. |
---|
172 | * </p> |
---|
173 | * |
---|
174 | * @param child1 the first task to be compared |
---|
175 | * @param child2 the second task to be compared |
---|
176 | * @param requiredEqualityLevel the equality level to be checked for |
---|
177 | * |
---|
178 | * @return the determined equality |
---|
179 | */ |
---|
180 | private TaskEquality callRuleManager(ITask child1, |
---|
181 | ITask child2, |
---|
182 | TaskEquality requiredEqualityLevel) |
---|
183 | { |
---|
184 | if (requiredEqualityLevel == null) { |
---|
185 | return TaskEqualityRuleManager.getInstance().compare(child1, child2); |
---|
186 | } |
---|
187 | else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual |
---|
188 | (child1, child2, requiredEqualityLevel)) |
---|
189 | { |
---|
190 | return requiredEqualityLevel; |
---|
191 | } |
---|
192 | else { |
---|
193 | return TaskEquality.UNEQUAL; |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | * <p> |
---|
199 | * compares two sequence instances with each other checking for the provided required level of |
---|
200 | * equality. If this level is ensured, the method immediately returns. The more concrete |
---|
201 | * the required equality level, the more checks this method performs. |
---|
202 | * </p> |
---|
203 | * |
---|
204 | * @param taskInstance1 the first task instance to be compared |
---|
205 | * @param taskInstance2 the second task instance to be compared |
---|
206 | * @param requiredEqualityLevel the equality level to be checked for |
---|
207 | * |
---|
208 | * @return the determined equality. |
---|
209 | */ |
---|
210 | private TaskEquality getEquality(ITaskInstance taskInstance1, |
---|
211 | ITaskInstance taskInstance2, |
---|
212 | TaskEquality requiredEqualityLevel) |
---|
213 | { |
---|
214 | ISequenceInstance sequence1 = (ISequenceInstance) taskInstance1; |
---|
215 | ISequenceInstance sequence2 = (ISequenceInstance) taskInstance2; |
---|
216 | |
---|
217 | // if both sequences do not have children, they are equal although this doesn't make sense |
---|
218 | if ((sequence1.size() == 0) && (sequence2.size() == 0)) { |
---|
219 | return TaskEquality.LEXICALLY_EQUAL; |
---|
220 | } |
---|
221 | |
---|
222 | if (sequence1.size() != sequence2.size()) { |
---|
223 | return TaskEquality.UNEQUAL; |
---|
224 | } |
---|
225 | |
---|
226 | TaskEquality resultingEquality = TaskEquality.LEXICALLY_EQUAL; |
---|
227 | for (int i = 0; i < sequence1.size(); i++) { |
---|
228 | ITaskInstance child1 = sequence1.get(i); |
---|
229 | ITaskInstance child2 = sequence2.get(i); |
---|
230 | |
---|
231 | TaskEquality taskEquality = callRuleManager(child1, child2, requiredEqualityLevel); |
---|
232 | |
---|
233 | if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) { |
---|
234 | return TaskEquality.UNEQUAL; |
---|
235 | } |
---|
236 | |
---|
237 | resultingEquality = resultingEquality.getCommonDenominator(taskEquality); |
---|
238 | } |
---|
239 | |
---|
240 | return resultingEquality; |
---|
241 | } |
---|
242 | |
---|
243 | /** |
---|
244 | * <p> |
---|
245 | * used to to call the task equality rule manager for the comparison of the two provided |
---|
246 | * children. If no required equality level is provided, than the most concrete equality is |
---|
247 | * returned. Otherwise, the required equality is returned as long as the children are equal |
---|
248 | * on that level. |
---|
249 | * </p> |
---|
250 | * |
---|
251 | * @param taskInstance1 the first task instance to be compared |
---|
252 | * @param taskInstance2 the second task instance to be compared |
---|
253 | * @param requiredEqualityLevel the equality level to be checked for |
---|
254 | * |
---|
255 | * @return the determined equality |
---|
256 | */ |
---|
257 | private TaskEquality callRuleManager(ITaskInstance taskInstance1, |
---|
258 | ITaskInstance taskInstance2, |
---|
259 | TaskEquality requiredEqualityLevel) |
---|
260 | { |
---|
261 | if (requiredEqualityLevel == null) { |
---|
262 | return TaskEqualityRuleManager.getInstance().compare(taskInstance1, taskInstance2); |
---|
263 | } |
---|
264 | else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual |
---|
265 | (taskInstance1, taskInstance2, requiredEqualityLevel)) |
---|
266 | { |
---|
267 | return requiredEqualityLevel; |
---|
268 | } |
---|
269 | else { |
---|
270 | return TaskEquality.UNEQUAL; |
---|
271 | } |
---|
272 | } |
---|
273 | } |
---|