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

Last change on this file since 1190 was 1190, checked in by pharms, 11 years ago
  • remove a find bugs warning
  • Property svn:executable set to *
File size: 12.3 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.IEventTask;
18import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
19import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
20import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
21import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
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 NodeComparisonRule#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 NodeComparisonRule#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                // check if the type of children match. If not, return false. If they match,
110                // use the equality manager to perform further comparisons
111               
112                if (((child1 instanceof ISelection) && (child2 instanceof ISelection)) ||
113                    ((child1 instanceof ISequence) && (child2 instanceof ISequence)) ||
114                    ((child1 instanceof IEventTask) && (child2 instanceof IEventTask)))
115                {
116                    return getNodeEquality
117                        (child1, child2).isAtLeast(TaskEquality.LEXICALLY_EQUAL);
118                }
119            }
120        }
121        else if (child2 == null) {
122            return true;
123        }
124       
125        return false;
126    }
127
128    /* (non-Javadoc)
129     * @see NodeComparisonRule#areSyntacticallyEqual(ITask, ITask)
130     */
131    @Override
132    public boolean areSyntacticallyEqual(ITask task1, ITask task2) {
133        ITask child1 = ((IIteration) task1).getMarkedTask();
134        ITask child2 = ((IIteration) task2).getMarkedTask();
135       
136        if (child1 != null) {
137            if (child2 == null) {
138                return false;
139            }
140            else {
141                // iterations may have 3 different structures.
142                // 1. they have one child, which is the iterated one
143                // 2. they have a sequence of children, which is iterated
144                // 3. they have a selection of different iterated variants (usually the variants
145                //    are semantically equal)
146                // ignore the type of the children but check them for equality.
147               
148                return getNodeEquality(child1, child2).isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL);
149            }
150        }
151        else if (child2 == null) {
152            return true;
153        }
154       
155        return false;
156    }
157
158    /* (non-Javadoc)
159     * @see NodeComparisonRule#areSemanticallyEqual(ITask, ITask)
160     */
161    @Override
162    public boolean areSemanticallyEqual(ITask task1, ITask task2) {
163        return compare(task1, task2).isAtLeast(TaskEquality.SEMANTICALLY_EQUAL);
164    }
165
166    /* (non-Javadoc)
167     * @see NodeComparisonRule#compare(ITask, ITask)
168     */
169    @Override
170    public TaskEquality compare(ITask task1, ITask task2) {
171        ITask child1 = ((IIteration) task1).getMarkedTask();
172        ITask child2 = ((IIteration) task2).getMarkedTask();
173
174        // if both iterations do not have children, they are equal although this doesn't make sense
175        if ((child1 == null) && (child2 == null)) {
176            return TaskEquality.LEXICALLY_EQUAL;
177        }
178        else if ((child1 == null) || (child2 == null)) {
179            return TaskEquality.UNEQUAL;
180        }
181
182        // iterations may have 3 different structures.
183        // 1. they have one child, which is the iterated one
184        // 2. they have a sequence of children, which is iterated
185        // 3. they have a selection of different iterated variants (usually the variants are
186        // semantically equal)
187        //
188        // the permutations of the three variants in combination must be checked
189
190        // check if both tasks are the same variants of iterations and if their children are equal.
191        // This condition matches, if both iterations are the same variants of iteration. I.e. three
192        // combinations of the permutation are handled herewith.
193        TaskEquality taskEquality = getNodeEquality(child1, child2);
194       
195        if (taskEquality != null) {
196            return taskEquality;
197        }
198
199        // compare one iteration with a single task as a child and another one with a selection of
200        // semantically equal tasks
201        return selectionChildrenSemanticallyEqualNode(child1, child2);
202       
203        // all other combinations (i.e. sequence with single child and sequence with selection)
204        // can not match
205    }
206
207    /**
208     * <p>
209     * compares two tasks with each other by calling the rule manager. If the rule manager returns
210     * identity, then the returned equality is set to lexically equal. The reason is, that
211     * the children of the iterations are compared and that therefore the distinct iterations
212     * can be at most lexically equal.
213     * </p>
214     *
215     * @param child1 the first task to be compared
216     * @param child2 the second task to be compared
217     *
218     * @return the determined equality being at most lexical equality.
219     */
220    private TaskEquality getNodeEquality(ITask child1, ITask child2) {
221        TaskEquality taskEquality = callRuleManager(child1, child2, null);
222
223        if (taskEquality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)) {
224            // prevent, that identical is returned, because the iterations itself are not identical
225            // although the iterated tasks are
226            if (taskEquality == TaskEquality.IDENTICAL) {
227                return TaskEquality.LEXICALLY_EQUAL;
228            }
229            else {
230                return taskEquality;
231            }
232        }
233       
234        return TaskEquality.UNEQUAL;
235    }
236
237    /**
238     * <p>
239     * compares two tasks. One of them must be a selection, the other one can be any task.
240     * The method returns a task equality that is not <code>NodeEquality.UNEQUAL</code>
241     * if the other task is at least semantically equal to the children of the selection. It
242     * returns more concrete equalities, if the equality between the other task and the children
243     * of the selection is more concrete.
244     * </p>
245     *
246     * @param task1 the first task to compare
247     * @param task2 the second task to compare
248     *
249     * @return as described
250     */
251    private TaskEquality selectionChildrenSemanticallyEqualNode(ITask task1, ITask task2) {
252        ISelection selection = null;
253        ITask task = null;
254        if (task1 instanceof ISelection) {
255            selection = (ISelection) task1;
256            task = task2;
257        }
258        else if (task2 instanceof ISelection) {
259            selection = (ISelection) task2;
260            task = task1;
261        }
262        else {
263            return TaskEquality.UNEQUAL;
264        }
265
266        // Iterations, where one has a selection and the other one not can at most be syntactically
267        // equal but not identical
268        TaskEquality commonDenominatorForAllComparisons = TaskEquality.SYNTACTICALLY_EQUAL;
269
270        for (ITask child : selection.getChildren()) {
271            TaskEquality taskEquality =
272                  callRuleManager(task, child, commonDenominatorForAllComparisons);
273
274            if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL))
275            {
276                return TaskEquality.UNEQUAL;
277            }
278           
279            commonDenominatorForAllComparisons =
280                commonDenominatorForAllComparisons.getCommonDenominator(taskEquality);
281        }
282
283        return commonDenominatorForAllComparisons;
284    }
285
286    /**
287     * <p>
288     * used to to call the task equality rule manager for the comparison of the two provided
289     * children. If no required equality level is provided, than the most concrete equality is
290     * returned. Otherwise, the required equality is returned as long as the children are equal
291     * on that level.
292     * </p>
293     *
294     * @param child1                the first task to be compared
295     * @param child2                the second task to be compared
296     * @param requiredEqualityLevel the equality level to be checked for
297     *
298     * @return the determined equality
299     */
300    private TaskEquality callRuleManager(ITask        child1,
301                                         ITask        child2,
302                                         TaskEquality requiredEqualityLevel)
303    {
304        if (requiredEqualityLevel == null) {
305            return TaskEqualityRuleManager.getInstance().compare(child1, child2);
306        }
307        else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual
308                    (child1, child2, requiredEqualityLevel))
309        {
310            return requiredEqualityLevel;
311        }
312        else {
313            return TaskEquality.UNEQUAL;
314        }
315    }
316}
Note: See TracBrowser for help on using the repository browser.