Ignore:
Timestamp:
09/05/14 19:33:12 (10 years ago)
Author:
rkrimmel
Message:

Used Eclipse code cleanup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/IterationComparisonRule.java

    r1294 r1733  
    2323/** 
    2424 * <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>: 
     25 * This class is capable of comparing Iterations. Iterations equal at distinct 
     26 * levels in distinct situations. The following table shows the results of the 
     27 * comparison for the specific situations (the parameters are commutative). In 
     28 * any other situation, the comparison returns <code>NodeEquality.UNEQUAL</code> 
     29 * : 
    2930 * </p> 
    3031 *  
    3132 * <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> 
     33 * <tr> 
     34 * <th>iteration 1</th> 
     35 * <th>iteration 2</th> 
     36 * <th>comparison result</th> 
     37 * </tr> 
     38 * <tr> 
     39 * <td>any iteration</td> 
     40 * <td>any iteration with a child that is lexically equal to the child of 
     41 * iteration 1</td> 
     42 * <td><code>NodeEquality.LEXICALLY_EQUAL</code></td> 
     43 * </tr> 
     44 * <tr> 
     45 * <td>any iteration</td> 
     46 * <td>any iteration with a child that is syntactically equal to the child of 
     47 * iteration 1</td> 
     48 * <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td> 
     49 * </tr> 
     50 * <tr> 
     51 * <td>any iteration</td> 
     52 * <td>any iteration with a child that is semantically equal to the child of 
     53 * iteration 1</td> 
     54 * <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td> 
     55 * </tr> 
     56 * <tr> 
     57 * <td>an iteration with a selection of syntactically equal children</td> 
     58 * <td>an iteration with a child that is syntactically equal to the children of 
     59 * the child selection of iteration 1</td> 
     60 * <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td> 
     61 * </tr> 
     62 * <tr> 
     63 * <td>an iteration with a selection of syntactically equal children</td> 
     64 * <td>an iteration with a selection of syntactically equal children that are 
     65 * all syntactically equal to the selection of children of iteration 1</td> 
     66 * <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td> 
     67 * </tr> 
     68 * <tr> 
     69 * <td>an iteration with a selection of semantically equal children</td> 
     70 * <td>an iteration with a child that is semantically equal to the children of 
     71 * the child selection of iteration 1</td> 
     72 * <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td> 
     73 * </tr> 
     74 * <tr> 
     75 * <td>an iteration with a selection of semantically equal children</td> 
     76 * <td>an iteration with a selection of semantically equal children that are all 
     77 * semantically equal to the selection of children of iteration 1</td> 
     78 * <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td> 
     79 * </tr> 
    7680 * </table> 
    7781 *  
     
    8084 */ 
    8185public 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     } 
     86 
     87        /* 
     88         * (non-Javadoc) 
     89         *  
     90         * @see TaskComparisonRule#areLexicallyEqual(ITask, ITask) 
     91         */ 
     92        @Override 
     93        public boolean areLexicallyEqual(ITask task1, ITask task2) { 
     94                final ITask child1 = ((IIteration) task1).getMarkedTask(); 
     95                final ITask child2 = ((IIteration) task2).getMarkedTask(); 
     96 
     97                if (child1 != null) { 
     98                        if (child2 == null) { 
     99                                return false; 
     100                        } else { 
     101                                // iterations may have 3 different structures. 
     102                                // 1. they have one child, which is the iterated one 
     103                                // 2. they have a sequence of children, which is iterated 
     104                                // 3. they have a selection of different iterated variants 
     105                                // (usually the variants 
     106                                // are semantically equal) 
     107                                // ignore the type of the children but check them for equality. 
     108 
     109                                return getNodeEquality(child1, child2).isAtLeast( 
     110                                                TaskEquality.LEXICALLY_EQUAL); 
     111                        } 
     112                } else if (child2 == null) { 
     113                        return true; 
     114                } 
     115 
     116                return false; 
     117        } 
     118 
     119        /* 
     120         * (non-Javadoc) 
     121         *  
     122         * @see TaskComparisonRule#areLexicallyEqual(ITaskInstance, ITaskInstance) 
     123         */ 
     124        @Override 
     125        public boolean areLexicallyEqual(ITaskInstance instance1, 
     126                        ITaskInstance instance2) { 
     127                final IIterationInstance iteration1 = (IIterationInstance) instance1; 
     128                final IIterationInstance iteration2 = (IIterationInstance) instance2; 
     129 
     130                // if both sequences do not have children, they are equal although this 
     131                // doesn't make sense 
     132                if ((iteration1.size() == 0) && (iteration2.size() == 0)) { 
     133                        return true; 
     134                } 
     135 
     136                if (iteration1.size() != iteration2.size()) { 
     137                        return false; 
     138                } 
     139 
     140                for (int i = 0; i < iteration1.size(); i++) { 
     141                        final ITaskInstance child1 = iteration1.get(i); 
     142                        final ITaskInstance child2 = iteration2.get(i); 
     143 
     144                        final TaskEquality taskEquality = callRuleManager(child1, child2, 
     145                                        TaskEquality.LEXICALLY_EQUAL); 
     146 
     147                        if ((taskEquality == null) 
     148                                        || (taskEquality == TaskEquality.UNEQUAL)) { 
     149                                return false; 
     150                        } 
     151                } 
     152 
     153                return true; 
     154        } 
     155 
     156        /* 
     157         * (non-Javadoc) 
     158         *  
     159         * @see TaskComparisonRule#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        /* 
     167         * (non-Javadoc) 
     168         *  
     169         * @see TaskComparisonRule#areSemanticallyEqual(ITaskInstance, 
     170         * ITaskInstance) 
     171         */ 
     172        @Override 
     173        public boolean areSemanticallyEqual(ITaskInstance instance1, 
     174                        ITaskInstance instance2) { 
     175                return areLexicallyEqual(instance1, instance2); 
     176        } 
     177 
     178        /* 
     179         * (non-Javadoc) 
     180         *  
     181         * @see TaskComparisonRule#areSyntacticallyEqual(ITask, ITask) 
     182         */ 
     183        @Override 
     184        public boolean areSyntacticallyEqual(ITask task1, ITask task2) { 
     185                return areLexicallyEqual(task1, task2); 
     186        } 
     187 
     188        /* 
     189         * (non-Javadoc) 
     190         *  
     191         * @see TaskComparisonRule#areSyntacticallyEqual(ITaskInstance, 
     192         * ITaskInstance) 
     193         */ 
     194        @Override 
     195        public boolean areSyntacticallyEqual(ITaskInstance instance1, 
     196                        ITaskInstance instance2) { 
     197                return areLexicallyEqual(instance1, instance2); 
     198        } 
     199 
     200        /** 
     201         * <p> 
     202         * used to to call the task equality rule manager for the comparison of the 
     203         * two provided children. If no required equality level is provided, than 
     204         * the most concrete equality is returned. Otherwise, the required equality 
     205         * is returned as long as the children are equal on that level. 
     206         * </p> 
     207         *  
     208         * @param child1 
     209         *            the first task to be compared 
     210         * @param child2 
     211         *            the second task to be compared 
     212         * @param requiredEqualityLevel 
     213         *            the equality level to be checked for 
     214         *  
     215         * @return the determined equality 
     216         */ 
     217        private TaskEquality callRuleManager(ITask child1, ITask child2, 
     218                        TaskEquality requiredEqualityLevel) { 
     219                if (requiredEqualityLevel == null) { 
     220                        return TaskEqualityRuleManager.getInstance() 
     221                                        .compare(child1, child2); 
     222                } else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual( 
     223                                child1, child2, requiredEqualityLevel)) { 
     224                        return requiredEqualityLevel; 
     225                } else { 
     226                        return TaskEquality.UNEQUAL; 
     227                } 
     228        } 
     229 
     230        /** 
     231         * <p> 
     232         * used to to call the task equality rule manager for the comparison of the 
     233         * two provided children. If no required equality level is provided, than 
     234         * the most concrete equality is returned. Otherwise, the required equality 
     235         * is returned as long as the children are equal on that level. 
     236         * </p> 
     237         *  
     238         * @param taskInstance1 
     239         *            the first task instance to be compared 
     240         * @param taskInstance2 
     241         *            the second task instance to be compared 
     242         * @param requiredEqualityLevel 
     243         *            the equality level to be checked for 
     244         *  
     245         * @return the determined equality 
     246         */ 
     247        private TaskEquality callRuleManager(ITaskInstance taskInstance1, 
     248                        ITaskInstance taskInstance2, TaskEquality requiredEqualityLevel) { 
     249                if (requiredEqualityLevel == null) { 
     250                        return TaskEqualityRuleManager.getInstance().compare(taskInstance1, 
     251                                        taskInstance2); 
     252                } else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual( 
     253                                taskInstance1, taskInstance2, requiredEqualityLevel)) { 
     254                        return requiredEqualityLevel; 
     255                } else { 
     256                        return TaskEquality.UNEQUAL; 
     257                } 
     258        } 
     259 
     260        /* 
     261         * (non-Javadoc) 
     262         *  
     263         * @see TaskComparisonRule#compare(ITask, ITask) 
     264         */ 
     265        @Override 
     266        public TaskEquality compare(ITask task1, ITask task2) { 
     267                final ITask child1 = ((IIteration) task1).getMarkedTask(); 
     268                final ITask child2 = ((IIteration) task2).getMarkedTask(); 
     269 
     270                // if both iterations do not have children, they are equal although this 
     271                // doesn't make sense 
     272                if ((child1 == null) && (child2 == null)) { 
     273                        return TaskEquality.LEXICALLY_EQUAL; 
     274                } else if ((child1 == null) || (child2 == null)) { 
     275                        return TaskEquality.UNEQUAL; 
     276                } 
     277 
     278                // iterations may have 3 different structures. 
     279                // 1. they have one child, which is the iterated one 
     280                // 2. they have a sequence of children, which is iterated 
     281                // 3. they have a selection of different iterated variants (usually the 
     282                // variants are 
     283                // semantically equal) 
     284                // 
     285                // the permutations of the three variants in combination must be checked 
     286 
     287                // check if both tasks are the same variants of iterations and if their 
     288                // children are equal. 
     289                // This condition matches, if both iterations are the same variants of 
     290                // iteration. I.e. three 
     291                // combinations of the permutation are handled herewith. 
     292                final TaskEquality taskEquality = getNodeEquality(child1, child2); 
     293 
     294                if (taskEquality != null) { 
     295                        return taskEquality; 
     296                } 
     297 
     298                // compare one iteration with a single task as a child and another one 
     299                // with a selection of 
     300                // semantically equal tasks 
     301                return selectionChildrenSemanticallyEqualNode(child1, child2); 
     302 
     303                // all other combinations (i.e. sequence with single child and sequence 
     304                // with selection) 
     305                // can not match 
     306        } 
     307 
     308        /* 
     309         * (non-Javadoc) 
     310         *  
     311         * @see TaskComparisonRule#compare(ITaskInstance, ITaskInstance) 
     312         */ 
     313        @Override 
     314        public TaskEquality compare(ITaskInstance instance1, ITaskInstance instance2) { 
     315                if (areLexicallyEqual(instance1, instance2)) { 
     316                        return TaskEquality.LEXICALLY_EQUAL; 
     317                } else { 
     318                        return TaskEquality.UNEQUAL; 
     319                } 
     320        } 
     321 
     322        /** 
     323         * <p> 
     324         * compares two tasks with each other by calling the rule manager. If the 
     325         * rule manager returns identity, then the returned equality is set to 
     326         * lexically equal. The reason is, that the children of the iterations are 
     327         * compared and that therefore the distinct iterations can be at most 
     328         * lexically equal. 
     329         * </p> 
     330         *  
     331         * @param child1 
     332         *            the first task to be compared 
     333         * @param child2 
     334         *            the second task to be compared 
     335         *  
     336         * @return the determined equality being at most lexical equality. 
     337         */ 
     338        private TaskEquality getNodeEquality(ITask child1, ITask child2) { 
     339                final TaskEquality taskEquality = callRuleManager(child1, child2, null); 
     340 
     341                if (taskEquality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)) { 
     342                        // prevent, that identical is returned, because the iterations 
     343                        // itself are not identical 
     344                        // although the iterated tasks are 
     345                        if (taskEquality == TaskEquality.IDENTICAL) { 
     346                                return TaskEquality.LEXICALLY_EQUAL; 
     347                        } else { 
     348                                return taskEquality; 
     349                        } 
     350                } 
     351 
     352                return TaskEquality.UNEQUAL; 
     353        } 
     354 
     355        /* 
     356         * (non-Javadoc) 
     357         *  
     358         * @see TaskComparisonRule#isApplicable(ITask, ITask) 
     359         */ 
     360        @Override 
     361        public boolean isApplicable(ITask task1, ITask task2) { 
     362                return (task1 instanceof IIteration) && (task2 instanceof IIteration); 
     363        } 
     364 
     365        /* 
     366         * (non-Javadoc) 
     367         *  
     368         * @see TaskComparisonRule#isApplicable(ITaskInstance, ITaskInstance) 
     369         */ 
     370        @Override 
     371        public boolean isApplicable(ITaskInstance instance1, ITaskInstance instance2) { 
     372                return isApplicable(instance1.getTask(), instance2.getTask()); 
     373        } 
     374 
     375        /** 
     376         * <p> 
     377         * compares two tasks. One of them must be a selection, the other one can be 
     378         * any task. The method returns a task equality that is not 
     379         * <code>NodeEquality.UNEQUAL</code> if the other task is at least 
     380         * semantically equal to the children of the selection. It returns more 
     381         * concrete equalities, if the equality between the other task and the 
     382         * children of the selection is more concrete. 
     383         * </p> 
     384         *  
     385         * @param task1 
     386         *            the first task to compare 
     387         * @param task2 
     388         *            the second task to compare 
     389         *  
     390         * @return as described 
     391         */ 
     392        private TaskEquality selectionChildrenSemanticallyEqualNode(ITask task1, 
     393                        ITask task2) { 
     394                ISelection selection = null; 
     395                ITask task = null; 
     396                if (task1 instanceof ISelection) { 
     397                        selection = (ISelection) task1; 
     398                        task = task2; 
     399                } else if (task2 instanceof ISelection) { 
     400                        selection = (ISelection) task2; 
     401                        task = task1; 
     402                } else { 
     403                        return TaskEquality.UNEQUAL; 
     404                } 
     405 
     406                // Iterations, where one has a selection and the other one not can at 
     407                // most be syntactically 
     408                // equal but not identical 
     409                TaskEquality commonDenominatorForAllComparisons = TaskEquality.SYNTACTICALLY_EQUAL; 
     410 
     411                for (final ITask child : selection.getChildren()) { 
     412                        final TaskEquality taskEquality = callRuleManager(task, child, 
     413                                        commonDenominatorForAllComparisons); 
     414 
     415                        if ((taskEquality == null) 
     416                                        || (taskEquality == TaskEquality.UNEQUAL)) { 
     417                                return TaskEquality.UNEQUAL; 
     418                        } 
     419 
     420                        commonDenominatorForAllComparisons = commonDenominatorForAllComparisons 
     421                                        .getCommonDenominator(taskEquality); 
     422                } 
     423 
     424                return commonDenominatorForAllComparisons; 
     425        } 
    386426} 
Note: See TracChangeset for help on using the changeset viewer.