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

Last change on this file since 1146 was 1146, checked in by pharms, 11 years ago
  • complete refactoring of task tree model with a separation of task models and task instances
  • appropriate adaptation of task tree generation process
  • appropriate adaptation of commands and task tree visualization
  • Property svn:executable set to *
File size: 11.9 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     * TODO update comment
225     */
226    private TaskEquality getNodeEquality(ITask child1, ITask child2) {
227        TaskEquality taskEquality = callRuleManager(child1, child2, null);
228
229        if (taskEquality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)) {
230            // prevent, that identical is returned, because the iterations itself are not identical
231            // although the iterated tasks are
232            if (taskEquality == TaskEquality.IDENTICAL) {
233                return TaskEquality.LEXICALLY_EQUAL;
234            }
235            else {
236                return taskEquality;
237            }
238        }
239       
240        return TaskEquality.UNEQUAL;
241    }
242
243    /**
244     * <p>
245     * compares two tasks. One of them must be a selection, the other one can be any task
246     * tree task. The method returns a task equality that is not <code>NodeEquality.UNEQUAL</code>
247     * if the other task is at least semantically equal to the children of the selection. It
248     * returns more concrete equalities, if the equality between the other task and the children
249     * of the selection is more concrete.
250     * </p>
251     *
252     * @param taskTreeNode  the first task to compare
253     * @param taskTreeNode2 the second task to compare
254     *
255     * @return as described
256     */
257    private TaskEquality selectionChildrenSemanticallyEqualNode(ITask task1, ITask task2) {
258        ISelection selection = null;
259        ITask task = null;
260        if (task1 instanceof ISelection) {
261            selection = (ISelection) task1;
262            task = task2;
263        }
264        else if (task2 instanceof ISelection) {
265            selection = (ISelection) task2;
266            task = task1;
267        }
268        else {
269            return TaskEquality.UNEQUAL;
270        }
271
272        // Iterations, where one has a selection and the other one not can at most be syntactically
273        // equal but not identical
274        TaskEquality commonDenominatorForAllComparisons = TaskEquality.SYNTACTICALLY_EQUAL;
275
276        for (ITask child : selection.getChildren()) {
277            TaskEquality taskEquality =
278                  callRuleManager(task, child, commonDenominatorForAllComparisons);
279
280            if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL))
281            {
282                return TaskEquality.UNEQUAL;
283            }
284           
285            commonDenominatorForAllComparisons =
286                commonDenominatorForAllComparisons.getCommonDenominator(taskEquality);
287        }
288
289        return commonDenominatorForAllComparisons;
290    }
291
292    /**
293     * <p>
294     * TODO: comment
295     * </p>
296     *
297     * @param child1
298     * @param child2
299     * @param requiredEqualityLevel
300     * @return
301     */
302    private TaskEquality callRuleManager(ITask        child1,
303                                         ITask        child2,
304                                         TaskEquality requiredEqualityLevel)
305    {
306        if (requiredEqualityLevel == null) {
307            return mRuleManager.compare(child1, child2);
308        }
309        else if (mRuleManager.areAtLeastEqual(child1, child2, requiredEqualityLevel)) {
310            return requiredEqualityLevel;
311        }
312        else {
313            return TaskEquality.UNEQUAL;
314        }
315    }
316}
Note: See TracBrowser for help on using the repository browser.