source: branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/IterationComparisonRule.java @ 1588

Last change on this file since 1588 was 1294, checked in by pharms, 11 years ago
  • rework of task model to move event instance stuff to task instances
  • introduction of sequence, selection, iteration and optional instances
  • Property svn:executable set to *
File size: 14.6 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.IIteration;
18import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance;
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 Iterations. Iterations 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>iteration 1</th>
34 *     <th>iteration 2</th>
35 *     <th>comparison result</th>
36 *   </tr>
37 *   <tr>
38 *     <td>any iteration</td>
39 *     <td>any iteration with a child that is lexically equal to the child of iteration 1</td>
40 *     <td><code>NodeEquality.LEXICALLY_EQUAL</code></td>
41 *   </tr>
42 *   <tr>
43 *     <td>any iteration</td>
44 *     <td>any iteration with a child that is syntactically equal to the child of iteration 1</td>
45 *     <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td>
46 *   </tr>
47 *   <tr>
48 *     <td>any iteration</td>
49 *     <td>any iteration with a child that is semantically equal to the child of iteration 1</td>
50 *     <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td>
51 *   </tr>
52 *   <tr>
53 *     <td>an iteration with a selection of syntactically equal children</td>
54 *     <td>an iteration with a child that is syntactically equal to the children of the child
55 *     selection of iteration 1</td>
56 *     <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td>
57 *   </tr>
58 *   <tr>
59 *     <td>an iteration with a selection of syntactically equal children</td>
60 *     <td>an iteration with a selection of syntactically equal children that are all syntactically
61 *     equal to the selection of children of iteration 1</td>
62 *     <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td>
63 *   </tr>
64 *   <tr>
65 *     <td>an iteration with a selection of semantically equal children</td>
66 *     <td>an iteration with a child that is semantically equal to the children of the child
67 *     selection of iteration 1</td>
68 *     <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td>
69 *   </tr>
70 *   <tr>
71 *     <td>an iteration with a selection of semantically equal children</td>
72 *     <td>an iteration with a selection of semantically equal children that are all semantically
73 *     equal to the selection of children of iteration 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 IterationComparisonRule 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 IIteration) && (task2 instanceof IIteration);
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 = ((IIteration) task1).getMarkedTask();
97        ITask child2 = ((IIteration) task2).getMarkedTask();
98       
99        if (child1 != null) {
100            if (child2 == null) {
101                return false;
102            }
103            else {
104                // iterations may have 3 different structures.
105                // 1. they have one child, which is the iterated one
106                // 2. they have a sequence of children, which is iterated
107                // 3. they have a selection of different iterated 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 = ((IIteration) task1).getMarkedTask();
143        ITask child2 = ((IIteration) task2).getMarkedTask();
144
145        // if both iterations do not have children, they are equal although this doesn't make sense
146        if ((child1 == null) && (child2 == null)) {
147            return TaskEquality.LEXICALLY_EQUAL;
148        }
149        else if ((child1 == null) || (child2 == null)) {
150            return TaskEquality.UNEQUAL;
151        }
152
153        // iterations may have 3 different structures.
154        // 1. they have one child, which is the iterated one
155        // 2. they have a sequence of children, which is iterated
156        // 3. they have a selection of different iterated 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 iterations and if their children are equal.
162        // This condition matches, if both iterations are the same variants of iteration. 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 iteration 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        IIterationInstance iteration1 = (IIterationInstance) instance1;
192        IIterationInstance iteration2 = (IIterationInstance) instance2;
193
194        // if both sequences do not have children, they are equal although this doesn't make sense
195        if ((iteration1.size() == 0) && (iteration2.size() == 0)) {
196            return true;
197        }
198
199        if (iteration1.size() != iteration2.size()) {
200            return false;
201        }
202
203        for (int i = 0; i < iteration1.size(); i++) {
204            ITaskInstance child1 = iteration1.get(i);
205            ITaskInstance child2 = iteration2.get(i);
206
207            TaskEquality taskEquality =
208                callRuleManager(child1, child2, TaskEquality.LEXICALLY_EQUAL);
209
210            if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) {
211                return false;
212            }
213        }
214
215        return true;
216    }
217
218    /* (non-Javadoc)
219     * @see TaskComparisonRule#areSyntacticallyEqual(ITaskInstance, ITaskInstance)
220     */
221    @Override
222    public boolean areSyntacticallyEqual(ITaskInstance instance1, ITaskInstance instance2) {
223        return areLexicallyEqual(instance1, instance2);
224    }
225
226    /* (non-Javadoc)
227     * @see TaskComparisonRule#areSemanticallyEqual(ITaskInstance, ITaskInstance)
228     */
229    @Override
230    public boolean areSemanticallyEqual(ITaskInstance instance1, ITaskInstance instance2) {
231        return areLexicallyEqual(instance1, instance2);
232    }
233
234    /* (non-Javadoc)
235     * @see TaskComparisonRule#compare(ITaskInstance, ITaskInstance)
236     */
237    @Override
238    public TaskEquality compare(ITaskInstance instance1, ITaskInstance instance2) {
239        if (areLexicallyEqual(instance1, instance2)) {
240            return TaskEquality.LEXICALLY_EQUAL;
241        }
242        else {
243            return TaskEquality.UNEQUAL;
244        }
245    }
246
247    /**
248     * <p>
249     * compares two tasks with each other by calling the rule manager. If the rule manager returns
250     * identity, then the returned equality is set to lexically equal. The reason is, that
251     * the children of the iterations are compared and that therefore the distinct iterations
252     * can be at most lexically equal.
253     * </p>
254     *
255     * @param child1 the first task to be compared
256     * @param child2 the second task to be compared
257     *
258     * @return the determined equality being at most lexical equality.
259     */
260    private TaskEquality getNodeEquality(ITask child1, ITask child2) {
261        TaskEquality taskEquality = callRuleManager(child1, child2, null);
262
263        if (taskEquality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)) {
264            // prevent, that identical is returned, because the iterations itself are not identical
265            // although the iterated tasks are
266            if (taskEquality == TaskEquality.IDENTICAL) {
267                return TaskEquality.LEXICALLY_EQUAL;
268            }
269            else {
270                return taskEquality;
271            }
272        }
273       
274        return TaskEquality.UNEQUAL;
275    }
276
277    /**
278     * <p>
279     * compares two tasks. One of them must be a selection, the other one can be any task.
280     * The method returns a task equality that is not <code>NodeEquality.UNEQUAL</code>
281     * if the other task is at least semantically equal to the children of the selection. It
282     * returns more concrete equalities, if the equality between the other task and the children
283     * of the selection is more concrete.
284     * </p>
285     *
286     * @param task1 the first task to compare
287     * @param task2 the second task to compare
288     *
289     * @return as described
290     */
291    private TaskEquality selectionChildrenSemanticallyEqualNode(ITask task1, ITask task2) {
292        ISelection selection = null;
293        ITask task = null;
294        if (task1 instanceof ISelection) {
295            selection = (ISelection) task1;
296            task = task2;
297        }
298        else if (task2 instanceof ISelection) {
299            selection = (ISelection) task2;
300            task = task1;
301        }
302        else {
303            return TaskEquality.UNEQUAL;
304        }
305
306        // Iterations, where one has a selection and the other one not can at most be syntactically
307        // equal but not identical
308        TaskEquality commonDenominatorForAllComparisons = TaskEquality.SYNTACTICALLY_EQUAL;
309
310        for (ITask child : selection.getChildren()) {
311            TaskEquality taskEquality =
312                  callRuleManager(task, child, commonDenominatorForAllComparisons);
313
314            if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) {
315                return TaskEquality.UNEQUAL;
316            }
317           
318            commonDenominatorForAllComparisons =
319                commonDenominatorForAllComparisons.getCommonDenominator(taskEquality);
320        }
321
322        return commonDenominatorForAllComparisons;
323    }
324
325    /**
326     * <p>
327     * used to to call the task equality rule manager for the comparison of the two provided
328     * children. If no required equality level is provided, than the most concrete equality is
329     * returned. Otherwise, the required equality is returned as long as the children are equal
330     * on that level.
331     * </p>
332     *
333     * @param child1                the first task to be compared
334     * @param child2                the second task to be compared
335     * @param requiredEqualityLevel the equality level to be checked for
336     *
337     * @return the determined equality
338     */
339    private TaskEquality callRuleManager(ITask        child1,
340                                         ITask        child2,
341                                         TaskEquality requiredEqualityLevel)
342    {
343        if (requiredEqualityLevel == null) {
344            return TaskEqualityRuleManager.getInstance().compare(child1, child2);
345        }
346        else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual
347                    (child1, child2, requiredEqualityLevel))
348        {
349            return requiredEqualityLevel;
350        }
351        else {
352            return TaskEquality.UNEQUAL;
353        }
354    }
355   
356    /**
357     * <p>
358     * used to to call the task equality rule manager for the comparison of the two provided
359     * children. If no required equality level is provided, than the most concrete equality is
360     * returned. Otherwise, the required equality is returned as long as the children are equal
361     * on that level.
362     * </p>
363     *
364     * @param taskInstance1         the first task instance to be compared
365     * @param taskInstance2         the second task instance to be compared
366     * @param requiredEqualityLevel the equality level to be checked for
367     *
368     * @return the determined equality
369     */
370    private TaskEquality callRuleManager(ITaskInstance taskInstance1,
371                                         ITaskInstance taskInstance2,
372                                         TaskEquality  requiredEqualityLevel)
373    {
374        if (requiredEqualityLevel == null) {
375            return TaskEqualityRuleManager.getInstance().compare(taskInstance1, taskInstance2);
376        }
377        else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual
378                     (taskInstance1, taskInstance2, requiredEqualityLevel))
379        {
380            return requiredEqualityLevel;
381        }
382        else {
383            return TaskEquality.UNEQUAL;
384        }
385    }
386}
Note: See TracBrowser for help on using the repository browser.