Ignore:
Timestamp:
08/17/12 08:33:29 (12 years ago)
Author:
pharms
Message:
  • adapted task tree creation stuff to more general event handling
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/quest-core-tasktrees/src/main/java/de/ugoe/cs/quest/tasktrees/nodeequality/IterationComparisonRule.java

    r439 r557  
    1 //------------------------------------------------------------------------------------------------- 
    21// Module    : $RCSfile: NodeIdentityRule.java,v $ 
    32// Version   : $Revision: 0.0 $  $Author: patrick $  $Date: 19.02.2012 $ 
     
    54// Creation  : 2012 by patrick 
    65// Copyright : Patrick Harms, 2012 
    7 //------------------------------------------------------------------------------------------------- 
     6 
    87package de.ugoe.cs.quest.tasktrees.nodeequality; 
    98 
    10 import de.ugoe.cs.quest.tasktrees.treeifc.Iteration; 
    11 import de.ugoe.cs.quest.tasktrees.treeifc.Selection; 
    12 import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeNode; 
     9import de.ugoe.cs.quest.tasktrees.treeifc.IIteration; 
     10import de.ugoe.cs.quest.tasktrees.treeifc.ISelection; 
     11import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode; 
    1312 
    14 //------------------------------------------------------------------------------------------------- 
    1513/** 
    16  * TODO comment 
     14 * <p> 
     15 * This class is capable of comparing Iterations. Iterations equal at distinct levels 
     16 * in distinct situations. The following table shows the results of the comparison for the 
     17 * specific situations (the parameters are commutative). In any other situation, the comparison 
     18 * returns <code>NodeEquality.UNEQUAL</code>: 
     19 * </p> 
     20 *  
     21 * <table border="1"> 
     22 *   <tr> 
     23 *     <th>iteration 1</th> 
     24 *     <th>iteration 2</th> 
     25 *     <th>comparison result</th> 
     26 *   </tr> 
     27 *   <tr> 
     28 *     <td>any iteration</td> 
     29 *     <td>any iteration with a child that is lexically equal to the child of iteration 1</td> 
     30 *     <td><code>NodeEquality.LEXICALLY_EQUAL</code></td> 
     31 *   </tr> 
     32 *   <tr> 
     33 *     <td>any iteration</td> 
     34 *     <td>any iteration with a child that is syntactically equal to the child of iteration 1</td> 
     35 *     <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td> 
     36 *   </tr> 
     37 *   <tr> 
     38 *     <td>any iteration</td> 
     39 *     <td>any iteration with a child that is semantically equal to the child of iteration 1</td> 
     40 *     <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td> 
     41 *   </tr> 
     42 *   <tr> 
     43 *     <td>an iteration with a selection of syntactically equal children</td> 
     44 *     <td>an iteration with a child that is syntactically equal to the children of the child 
     45 *     selection of iteration 1</td> 
     46 *     <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td> 
     47 *   </tr> 
     48 *   <tr> 
     49 *     <td>an iteration with a selection of semantically equal children</td> 
     50 *     <td>an iteration with a child that is semantically equal to the children of the child 
     51 *     selection of iteration 1</td> 
     52 *     <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td> 
     53 *   </tr> 
     54 * </table> 
    1755 *  
    1856 * @version $Revision: $ $Date: 19.02.2012$ 
    1957 * @author 2012, last modified by $Author: patrick$ 
    2058 */ 
    21 //------------------------------------------------------------------------------------------------- 
    22 public class IterationComparisonRule implements NodeComparisonRule 
    23 { 
    24   /** */ 
    25   private NodeEqualityRuleManager mRuleManager; 
     59public class IterationComparisonRule implements NodeComparisonRule { 
     60     
     61    /** the rule manager for internally comparing task tree nodes */ 
     62    private NodeEqualityRuleManager mRuleManager; 
    2663 
    27   //----------------------------------------------------------------------------------------------- 
    28   /** 
    29    * TODO: comment 
    30    * 
    31    */ 
    32   //----------------------------------------------------------------------------------------------- 
    33   IterationComparisonRule(NodeEqualityRuleManager ruleManager) 
    34   { 
    35     super(); 
    36     mRuleManager = ruleManager; 
    37   } 
    38  
    39   //----------------------------------------------------------------------------------------------- 
    40   /* (non-Javadoc) 
    41    * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode) 
    42    */ 
    43   //----------------------------------------------------------------------------------------------- 
    44   @Override 
    45   public NodeEquality compare(TaskTreeNode node1, TaskTreeNode node2) 
    46   { 
    47     if ((!(node1 instanceof Iteration)) || (!(node2 instanceof Iteration))) 
    48     { 
    49       return null; 
    50     } 
    51      
    52     // if both iterations do not have children, they are equal although this doesn't make sense 
    53     if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) 
    54     { 
    55       return new NodesIdentical(); 
    56     } 
    57      
    58     TaskTreeNode child1 = node1.getChildren().get(0); 
    59     TaskTreeNode child2 = node2.getChildren().get(0); 
    60      
    61     // iterations may have 3 different structures. 
    62     //   1. they have one child, which is the iterated one 
    63     //   2. they have a sequence of children, which is iterated 
    64     //   3. they have a selection of different iterated variants (usually the variants are 
    65     //      semantically equal) 
    66     // 
    67     // the permutations of the three variants in combination must be checked 
    68      
    69     // check if both nodes are the same variants of iterations and if their children are equal. 
    70     // This condition matches, if both iterations are the same variants of iteration. I.e. three 
    71     // combinations of the permutation are handled herewith. 
    72     NodeEquality nodeEquality = mRuleManager.applyRules(child1, child2); 
    73        
    74     if (nodeEquality.getStructuralEquality() || nodeEquality.getSemanticalEquality()) 
    75     { 
    76       return nodeEquality; 
    77     } 
    78      
    79     // compare one iteration with a single node as a child and another one with a selection of 
    80     // semantically equal nodes 
    81     if (selectionChildrenSemanticallyEqualNode(child1, child2)) 
    82     { 
    83       return new NodesSemanticallyEqual(); 
     64    /** 
     65     * <p> 
     66     * simple constructor to provide the rule with the node equality rule manager to be able 
     67     * to perform comparisons of the children of provided task tree nodes 
     68     * </p> 
     69     *  
     70     * @param ruleManager the rule manager for comparing task tree nodes 
     71     */ 
     72    IterationComparisonRule(NodeEqualityRuleManager ruleManager) { 
     73        super(); 
     74        mRuleManager = ruleManager; 
    8475    } 
    8576 
    86     // all other combinations (i.e. sequence with single child and sequence with selection) 
    87     // can not match 
    88     return null; 
    89   } 
     77    /* 
     78     * (non-Javadoc) 
     79     *  
     80     * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode) 
     81     */ 
     82    @Override 
     83    public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) { 
     84        if ((!(node1 instanceof IIteration)) || (!(node2 instanceof IIteration))) { 
     85            return null; 
     86        } 
    9087 
    91   //----------------------------------------------------------------------------------------------- 
    92   /** 
    93    * TODO: comment 
    94    * 
    95    * @param taskTreeNode 
    96    * @param taskTreeNode2 
    97    * @return 
    98    */ 
    99   //----------------------------------------------------------------------------------------------- 
    100   private boolean selectionChildrenSemanticallyEqualNode(TaskTreeNode taskTreeNode, 
    101                                                          TaskTreeNode taskTreeNode2) 
    102   { 
    103     Selection selection = null; 
    104     TaskTreeNode node = null; 
    105     if (taskTreeNode instanceof Selection) 
     88        // if both iterations do not have children, they are equal although this doesn't make sense 
     89        if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) { 
     90            return NodeEquality.IDENTICAL; 
     91        } 
     92 
     93        ITaskTreeNode child1 = node1.getChildren().get(0); 
     94        ITaskTreeNode child2 = node2.getChildren().get(0); 
     95 
     96        // iterations may have 3 different structures. 
     97        // 1. they have one child, which is the iterated one 
     98        // 2. they have a sequence of children, which is iterated 
     99        // 3. they have a selection of different iterated variants (usually the variants are 
     100        // semantically equal) 
     101        // 
     102        // the permutations of the three variants in combination must be checked 
     103 
     104        // check if both nodes are the same variants of iterations and if their children are equal. 
     105        // This condition matches, if both iterations are the same variants of iteration. I.e. three 
     106        // combinations of the permutation are handled herewith. 
     107        NodeEquality nodeEquality = mRuleManager.applyRules(child1, child2); 
     108 
     109        if (nodeEquality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)) { 
     110            return nodeEquality; 
     111        } 
     112 
     113        // compare one iteration with a single node as a child and another one with a selection of 
     114        // semantically equal nodes 
     115        return selectionChildrenSemanticallyEqualNode(child1, child2); 
     116         
     117        // all other combinations (i.e. sequence with single child and sequence with selection) 
     118        // can not match 
     119    } 
     120 
     121    /** 
     122     * <p> 
     123     * compares two task tree nodes. One of them must be a selection of at least semantically 
     124     * equal children. The other one can be any task tree node. The method returns a node equality 
     125     * that is not <code>NodeEquality.UNEQUAL</code> if the other node is at least semantically 
     126     * equal to the children of the selection. It returns more concrete equalities, if the 
     127     * equality between the other node and the children of the selection is more concrete. 
     128     * </p>  
     129     *  
     130     * @param taskTreeNode  the first task tree node to compare 
     131     * @param taskTreeNode2 the second task tree node to compare 
     132     *  
     133     * @return as described 
     134     */ 
     135    private NodeEquality selectionChildrenSemanticallyEqualNode(ITaskTreeNode taskTreeNode, 
     136                                                                ITaskTreeNode taskTreeNode2) 
    106137    { 
    107       selection = (Selection) taskTreeNode; 
    108       node = taskTreeNode2; 
     138        ISelection selection = null; 
     139        ITaskTreeNode node = null; 
     140        if (taskTreeNode instanceof ISelection) { 
     141            selection = (ISelection) taskTreeNode; 
     142            node = taskTreeNode2; 
     143        } 
     144        else if (taskTreeNode2 instanceof ISelection) { 
     145            selection = (ISelection) taskTreeNode2; 
     146            node = taskTreeNode; 
     147        } 
     148        else { 
     149            return NodeEquality.UNEQUAL; 
     150        } 
     151 
     152        NodeEquality lessConcreteEqualityForAllComparisons = NodeEquality.IDENTICAL; 
     153 
     154        for (ITaskTreeNode child : selection.getChildren()) { 
     155            NodeEquality nodeEquality = mRuleManager.applyRules(node, child); 
     156 
     157            if (!nodeEquality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)) { 
     158                return NodeEquality.UNEQUAL; 
     159            } 
     160            else if (!lessConcreteEqualityForAllComparisons.isAtLeast(nodeEquality)) { 
     161                lessConcreteEqualityForAllComparisons = nodeEquality; 
     162            } 
     163        } 
     164 
     165        return lessConcreteEqualityForAllComparisons; 
    109166    } 
    110     else if (taskTreeNode2 instanceof Selection) 
    111     { 
    112       selection = (Selection) taskTreeNode2; 
    113       node = taskTreeNode; 
    114     } 
    115     else 
    116     { 
    117       return false; 
    118     } 
    119      
    120     for (TaskTreeNode child : selection.getChildren()) 
    121     { 
    122       NodeEquality nodeEquality = mRuleManager.applyRules(node, child); 
    123            
    124       if (!nodeEquality.getStructuralEquality() && !nodeEquality.getSemanticalEquality()) 
    125       { 
    126         return false; 
    127       } 
    128     } 
    129      
    130     return true; 
    131   } 
    132167 
    133168} 
Note: See TracChangeset for help on using the changeset viewer.