source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/OptionalComparisonRule.java @ 2255

Last change on this file since 2255 was 1887, checked in by pharms, 9 years ago
  • extended and corrected task comparison
File size: 14.4 KB
Line 
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
15package de.ugoe.cs.autoquest.tasktrees.taskequality;
16
17import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
18import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance;
19import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
20import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
21import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
22
23/**
24 * <p>
25 * This class is capable of comparing Optionals. Optionals equal at distinct levels
26 * in distinct situations. The following table shows the results of the comparison for the
27 * specific situations (the parameters are commutative). In any other situation, the comparison
28 * returns <code>NodeEquality.UNEQUAL</code>:
29 * </p>
30 *
31 * <table border="1">
32 *   <tr>
33 *     <th>optional 1</th>
34 *     <th>optional 2</th>
35 *     <th>comparison result</th>
36 *   </tr>
37 *   <tr>
38 *     <td>any optional</td>
39 *     <td>any optional with a child that is lexically equal to the child of optional 1</td>
40 *     <td><code>NodeEquality.LEXICALLY_EQUAL</code></td>
41 *   </tr>
42 *   <tr>
43 *     <td>any optional</td>
44 *     <td>any optional with a child that is syntactically equal to the child of optional 1</td>
45 *     <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td>
46 *   </tr>
47 *   <tr>
48 *     <td>any optional</td>
49 *     <td>any optional with a child that is semantically equal to the child of optional 1</td>
50 *     <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td>
51 *   </tr>
52 *   <tr>
53 *     <td>an optional with a selection of syntactically equal children</td>
54 *     <td>an optional with a child that is syntactically equal to the children of the child
55 *     selection of optional 1</td>
56 *     <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td>
57 *   </tr>
58 *   <tr>
59 *     <td>an optional with a selection of syntactically equal children</td>
60 *     <td>an optional with a selection of syntactically equal children that are all syntactically
61 *     equal to the selection of children of optional 1</td>
62 *     <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td>
63 *   </tr>
64 *   <tr>
65 *     <td>an optional with a selection of semantically equal children</td>
66 *     <td>an optional with a child that is semantically equal to the children of the child
67 *     selection of optional 1</td>
68 *     <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td>
69 *   </tr>
70 *   <tr>
71 *     <td>an optional with a selection of semantically equal children</td>
72 *     <td>an optional with a selection of semantically equal children that are all semantically
73 *     equal to the selection of children of optional 1</td>
74 *     <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td>
75 *   </tr>
76 * </table>
77 *
78 * @version $Revision: $ $Date: 19.02.2012$
79 * @author 2012, last modified by $Author: patrick$
80 */
81public class OptionalComparisonRule implements TaskComparisonRule {
82   
83    /* (non-Javadoc)
84     * @see TaskComparisonRule#isApplicable(ITask, ITask)
85     */
86    @Override
87    public boolean isApplicable(ITask task1, ITask task2) {
88        return (task1 instanceof IOptional) && (task2 instanceof IOptional);
89    }
90
91    /* (non-Javadoc)
92     * @see TaskComparisonRule#areLexicallyEqual(ITask, ITask)
93     */
94    @Override
95    public boolean areLexicallyEqual(ITask task1, ITask task2) {
96        ITask child1 = ((IOptional) task1).getMarkedTask();
97        ITask child2 = ((IOptional) task2).getMarkedTask();
98       
99        if (child1 != null) {
100            if (child2 == null) {
101                return false;
102            }
103            else {
104                // optionals may have 3 different structures.
105                // 1. they have one child, which is the optional one
106                // 2. they have a sequence of children, which is optional
107                // 3. they have a selection of different optional variants (usually the variants
108                //    are semantically equal)
109                // ignore the type of the children but check them for equality.
110               
111                return getNodeEquality(child1, child2).isAtLeast(TaskEquality.LEXICALLY_EQUAL);
112            }
113        }
114        else if (child2 == null) {
115            return true;
116        }
117       
118        return false;
119    }
120
121    /* (non-Javadoc)
122     * @see TaskComparisonRule#areSyntacticallyEqual(ITask, ITask)
123     */
124    @Override
125    public boolean areSyntacticallyEqual(ITask task1, ITask task2) {
126        return areLexicallyEqual(task1, task2);
127    }
128
129    /* (non-Javadoc)
130     * @see TaskComparisonRule#areSemanticallyEqual(ITask, ITask)
131     */
132    @Override
133    public boolean areSemanticallyEqual(ITask task1, ITask task2) {
134        return compare(task1, task2).isAtLeast(TaskEquality.SEMANTICALLY_EQUAL);
135    }
136
137    /* (non-Javadoc)
138     * @see TaskComparisonRule#compare(ITask, ITask)
139     */
140    @Override
141    public TaskEquality compare(ITask task1, ITask task2) {
142        ITask child1 = ((IOptional) task1).getMarkedTask();
143        ITask child2 = ((IOptional) task2).getMarkedTask();
144
145        // if both optionals do not have a child, they are equal although this doesn't make sense
146        if (child1 == child2) {
147            return TaskEquality.LEXICALLY_EQUAL;
148        }
149        else if ((child1 == null) || (child2 == null)) {
150            return TaskEquality.UNEQUAL;
151        }
152
153        // optionals may have 3 different structures.
154        // 1. they have one child, which is the optional one
155        // 2. they have a sequence of children, which is optional
156        // 3. they have a selection of different optional variants (usually the variants are
157        // semantically equal)
158        //
159        // the permutations of the three variants in combination must be checked
160
161        // check if both tasks are the same variants of optional and if their children are equal.
162        // This condition matches, if both optionals are the same variants of optional. I.e. three
163        // combinations of the permutation are handled herewith.
164        TaskEquality taskEquality = getNodeEquality(child1, child2);
165       
166        if (taskEquality != null) {
167            return taskEquality;
168        }
169
170        // compare one optional with a single task as a child and another one with a selection of
171        // semantically equal tasks
172        return selectionChildrenSemanticallyEqualNode(child1, child2);
173       
174        // all other combinations (i.e. sequence with single child and sequence with selection)
175        // can not match
176    }
177
178    /* (non-Javadoc)
179     * @see TaskComparisonRule#isApplicable(ITaskInstance, ITaskInstance)
180     */
181    @Override
182    public boolean isApplicable(ITaskInstance instance1, ITaskInstance instance2) {
183        return isApplicable(instance1.getTask(), instance2.getTask());
184    }
185
186    /* (non-Javadoc)
187     * @see TaskComparisonRule#areLexicallyEqual(ITaskInstance, ITaskInstance)
188     */
189    @Override
190    public boolean areLexicallyEqual(ITaskInstance instance1, ITaskInstance instance2) {
191        IOptionalInstance optional1 = (IOptionalInstance) instance1;
192        IOptionalInstance optional2 = (IOptionalInstance) instance2;
193
194        // if both optionals do not have a child, they are equal although this doesn't make sense
195        if (optional1.getChild() == optional2.getChild()) {
196            return true;
197        }
198        else if ((optional1.getChild() == null) || (optional2.getChild() == null)) {
199            return false;
200        }
201
202        TaskEquality taskEquality = callRuleManager
203            (optional1.getChild(), optional2.getChild(), TaskEquality.LEXICALLY_EQUAL);
204
205        if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) {
206            return false;
207        }
208
209        return true;
210    }
211
212    /* (non-Javadoc)
213     * @see TaskComparisonRule#areSyntacticallyEqual(ITaskInstance, ITaskInstance)
214     */
215    @Override
216    public boolean areSyntacticallyEqual(ITaskInstance instance1, ITaskInstance instance2) {
217        return areLexicallyEqual(instance1, instance2);
218    }
219
220    /* (non-Javadoc)
221     * @see TaskComparisonRule#areSemanticallyEqual(ITaskInstance, ITaskInstance)
222     */
223    @Override
224    public boolean areSemanticallyEqual(ITaskInstance instance1, ITaskInstance instance2) {
225        return areLexicallyEqual(instance1, instance2);
226    }
227
228    /* (non-Javadoc)
229     * @see TaskComparisonRule#compare(ITaskInstance, ITaskInstance)
230     */
231    @Override
232    public TaskEquality compare(ITaskInstance instance1, ITaskInstance instance2) {
233        if (areLexicallyEqual(instance1, instance2)) {
234            return TaskEquality.LEXICALLY_EQUAL;
235        }
236        else {
237            return TaskEquality.UNEQUAL;
238        }
239    }
240
241    /**
242     * <p>
243     * compares two tasks with each other by calling the rule manager. If the rule manager returns
244     * identity, then the returned equality is set to lexically equal. The reason is, that
245     * the children of the optionals are compared and that therefore the distinct optionals
246     * can be at most lexically equal.
247     * </p>
248     *
249     * @param child1 the first task to be compared
250     * @param child2 the second task to be compared
251     *
252     * @return the determined equality being at most lexical equality.
253     */
254    private TaskEquality getNodeEquality(ITask child1, ITask child2) {
255        TaskEquality taskEquality = callRuleManager(child1, child2, null);
256
257        if (taskEquality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)) {
258            // prevent, that identical is returned, because the optionals itself are not identical
259            // although the optional tasks are
260            if (taskEquality == TaskEquality.IDENTICAL) {
261                return TaskEquality.LEXICALLY_EQUAL;
262            }
263            else {
264                return taskEquality;
265            }
266        }
267       
268        return TaskEquality.UNEQUAL;
269    }
270
271    /**
272     * <p>
273     * compares two tasks. One of them must be a selection, the other one can be any task.
274     * The method returns a task equality that is not <code>NodeEquality.UNEQUAL</code>
275     * if the other task is at least semantically equal to the children of the selection. It
276     * returns more concrete equalities, if the equality between the other task and the children
277     * of the selection is more concrete.
278     * </p>
279     *
280     * @param task1 the first task to compare
281     * @param task2 the second task to compare
282     *
283     * @return as described
284     */
285    private TaskEquality selectionChildrenSemanticallyEqualNode(ITask task1, ITask task2) {
286        ISelection selection = null;
287        ITask task = null;
288        if (task1 instanceof ISelection) {
289            selection = (ISelection) task1;
290            task = task2;
291        }
292        else if (task2 instanceof ISelection) {
293            selection = (ISelection) task2;
294            task = task1;
295        }
296        else {
297            return TaskEquality.UNEQUAL;
298        }
299
300        // Iterations, where one has a selection and the other one not can at most be syntactically
301        // equal but not identical
302        TaskEquality commonDenominatorForAllComparisons = TaskEquality.SYNTACTICALLY_EQUAL;
303
304        for (ITask child : selection.getChildren()) {
305            TaskEquality taskEquality =
306                  callRuleManager(task, child, commonDenominatorForAllComparisons);
307
308            if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) {
309                return TaskEquality.UNEQUAL;
310            }
311           
312            commonDenominatorForAllComparisons =
313                commonDenominatorForAllComparisons.getCommonDenominator(taskEquality);
314        }
315
316        return commonDenominatorForAllComparisons;
317    }
318
319    /**
320     * <p>
321     * used to to call the task equality rule manager for the comparison of the two provided
322     * children. If no required equality level is provided, than the most concrete equality is
323     * returned. Otherwise, the required equality is returned as long as the children are equal
324     * on that level.
325     * </p>
326     *
327     * @param child1                the first task to be compared
328     * @param child2                the second task to be compared
329     * @param requiredEqualityLevel the equality level to be checked for
330     *
331     * @return the determined equality
332     */
333    private TaskEquality callRuleManager(ITask        child1,
334                                         ITask        child2,
335                                         TaskEquality requiredEqualityLevel)
336    {
337        if (requiredEqualityLevel == null) {
338            return TaskEqualityRuleManager.getInstance().compare(child1, child2);
339        }
340        else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual
341                    (child1, child2, requiredEqualityLevel))
342        {
343            return requiredEqualityLevel;
344        }
345        else {
346            return TaskEquality.UNEQUAL;
347        }
348    }
349   
350    /**
351     * <p>
352     * used to to call the task equality rule manager for the comparison of the two provided
353     * children. If no required equality level is provided, than the most concrete equality is
354     * returned. Otherwise, the required equality is returned as long as the children are equal
355     * on that level.
356     * </p>
357     *
358     * @param taskInstance1         the first task instance to be compared
359     * @param taskInstance2         the second task instance to be compared
360     * @param requiredEqualityLevel the equality level to be checked for
361     *
362     * @return the determined equality
363     */
364    private TaskEquality callRuleManager(ITaskInstance taskInstance1,
365                                         ITaskInstance taskInstance2,
366                                         TaskEquality  requiredEqualityLevel)
367    {
368        if (requiredEqualityLevel == null) {
369            return TaskEqualityRuleManager.getInstance().compare(taskInstance1, taskInstance2);
370        }
371        else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual
372                     (taskInstance1, taskInstance2, requiredEqualityLevel))
373        {
374            return requiredEqualityLevel;
375        }
376        else {
377            return TaskEquality.UNEQUAL;
378        }
379    }
380}
Note: See TracBrowser for help on using the repository browser.