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

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