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

Last change on this file since 1618 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
RevLine 
[1113]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
[1146]15package de.ugoe.cs.autoquest.tasktrees.taskequality;
[439]16
[922]17import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
[1294]18import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance;
[922]19import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
[1146]20import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
[1294]21import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
[439]22
23/**
[557]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>
[439]30 *
[557]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>
[807]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>
[557]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>
[807]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>
[557]76 * </table>
77 *
[439]78 * @version $Revision: $ $Date: 19.02.2012$
79 * @author 2012, last modified by $Author: patrick$
80 */
[1146]81public class IterationComparisonRule implements TaskComparisonRule {
[557]82   
[1125]83    /* (non-Javadoc)
[1294]84     * @see TaskComparisonRule#isApplicable(ITask, ITask)
[557]85     */
86    @Override
[1146]87    public boolean isApplicable(ITask task1, ITask task2) {
88        return (task1 instanceof IIteration) && (task2 instanceof IIteration);
[1125]89    }
90
91    /* (non-Javadoc)
[1294]92     * @see TaskComparisonRule#areLexicallyEqual(ITask, ITask)
[1125]93     */
94    @Override
[1146]95    public boolean areLexicallyEqual(ITask task1, ITask task2) {
96        ITask child1 = ((IIteration) task1).getMarkedTask();
97        ITask child2 = ((IIteration) task2).getMarkedTask();
[1125]98       
[1146]99        if (child1 != null) {
100            if (child2 == null) {
101                return false;
[1125]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)
[1294]109                // ignore the type of the children but check them for equality.
[1125]110               
[1294]111                return getNodeEquality(child1, child2).isAtLeast(TaskEquality.LEXICALLY_EQUAL);
[1125]112            }
[557]113        }
[1146]114        else if (child2 == null) {
115            return true;
116        }
[1125]117       
118        return false;
119    }
[557]120
[1125]121    /* (non-Javadoc)
[1294]122     * @see TaskComparisonRule#areSyntacticallyEqual(ITask, ITask)
[1125]123     */
124    @Override
[1146]125    public boolean areSyntacticallyEqual(ITask task1, ITask task2) {
[1294]126        return areLexicallyEqual(task1, task2);
[1125]127    }
[807]128
[1125]129    /* (non-Javadoc)
[1294]130     * @see TaskComparisonRule#areSemanticallyEqual(ITask, ITask)
[1125]131     */
132    @Override
[1146]133    public boolean areSemanticallyEqual(ITask task1, ITask task2) {
134        return compare(task1, task2).isAtLeast(TaskEquality.SEMANTICALLY_EQUAL);
[1125]135    }
136
137    /* (non-Javadoc)
[1294]138     * @see TaskComparisonRule#compare(ITask, ITask)
[1125]139     */
140    @Override
[1146]141    public TaskEquality compare(ITask task1, ITask task2) {
142        ITask child1 = ((IIteration) task1).getMarkedTask();
143        ITask child2 = ((IIteration) task2).getMarkedTask();
[1125]144
[557]145        // if both iterations do not have children, they are equal although this doesn't make sense
[1146]146        if ((child1 == null) && (child2 == null)) {
147            return TaskEquality.LEXICALLY_EQUAL;
[557]148        }
[1146]149        else if ((child1 == null) || (child2 == null)) {
150            return TaskEquality.UNEQUAL;
[807]151        }
[557]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
[1146]161        // check if both tasks are the same variants of iterations and if their children are equal.
[557]162        // This condition matches, if both iterations are the same variants of iteration. I.e. three
163        // combinations of the permutation are handled herewith.
[1146]164        TaskEquality taskEquality = getNodeEquality(child1, child2);
[1125]165       
[1146]166        if (taskEquality != null) {
167            return taskEquality;
[1125]168        }
[557]169
[1146]170        // compare one iteration with a single task as a child and another one with a selection of
171        // semantically equal tasks
[1125]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
[1294]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
[1125]247    /**
[1154]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.
[1125]259     */
[1146]260    private TaskEquality getNodeEquality(ITask child1, ITask child2) {
261        TaskEquality taskEquality = callRuleManager(child1, child2, null);
[1125]262
[1146]263        if (taskEquality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)) {
[807]264            // prevent, that identical is returned, because the iterations itself are not identical
265            // although the iterated tasks are
[1146]266            if (taskEquality == TaskEquality.IDENTICAL) {
267                return TaskEquality.LEXICALLY_EQUAL;
[807]268            }
269            else {
[1146]270                return taskEquality;
[807]271            }
[557]272        }
273       
[1146]274        return TaskEquality.UNEQUAL;
[439]275    }
[557]276
277    /**
278     * <p>
[1154]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>
[1146]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
[807]283     * of the selection is more concrete.
[557]284     * </p>
285     *
[1154]286     * @param task1 the first task to compare
287     * @param task2 the second task to compare
[557]288     *
289     * @return as described
290     */
[1146]291    private TaskEquality selectionChildrenSemanticallyEqualNode(ITask task1, ITask task2) {
[557]292        ISelection selection = null;
[1146]293        ITask task = null;
294        if (task1 instanceof ISelection) {
295            selection = (ISelection) task1;
296            task = task2;
[557]297        }
[1146]298        else if (task2 instanceof ISelection) {
299            selection = (ISelection) task2;
300            task = task1;
[557]301        }
302        else {
[1146]303            return TaskEquality.UNEQUAL;
[557]304        }
[439]305
[807]306        // Iterations, where one has a selection and the other one not can at most be syntactically
307        // equal but not identical
[1146]308        TaskEquality commonDenominatorForAllComparisons = TaskEquality.SYNTACTICALLY_EQUAL;
[439]309
[1146]310        for (ITask child : selection.getChildren()) {
311            TaskEquality taskEquality =
312                  callRuleManager(task, child, commonDenominatorForAllComparisons);
[557]313
[1294]314            if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) {
[1146]315                return TaskEquality.UNEQUAL;
[557]316            }
[807]317           
318            commonDenominatorForAllComparisons =
[1146]319                commonDenominatorForAllComparisons.getCommonDenominator(taskEquality);
[557]320        }
321
[807]322        return commonDenominatorForAllComparisons;
[439]323    }
324
[1125]325    /**
326     * <p>
[1154]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
[1125]338     */
[1146]339    private TaskEquality callRuleManager(ITask        child1,
340                                         ITask        child2,
341                                         TaskEquality requiredEqualityLevel)
[1125]342    {
343        if (requiredEqualityLevel == null) {
[1190]344            return TaskEqualityRuleManager.getInstance().compare(child1, child2);
[1125]345        }
[1190]346        else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual
347                    (child1, child2, requiredEqualityLevel))
348        {
[1125]349            return requiredEqualityLevel;
350        }
351        else {
[1146]352            return TaskEquality.UNEQUAL;
[1125]353        }
354    }
[1294]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    }
[439]386}
Note: See TracBrowser for help on using the repository browser.