Ignore:
Timestamp:
03/18/13 11:46:47 (11 years ago)
Author:
pharms
Message:
  • refactoring of task tree node comparison to be able to optimize the comparisons for the different comparison levels lexically, syntactically, semantically
File:
1 edited

Legend:

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

    r1113 r1125  
    1515package de.ugoe.cs.autoquest.tasktrees.nodeequality; 
    1616 
     17import java.util.List; 
     18 
     19import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; 
    1720import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; 
    1821import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; 
     22import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; 
    1923import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; 
    2024 
     
    9599    } 
    96100 
    97     /* 
    98      * (non-Javadoc) 
    99      *  
    100      * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode) 
     101    /* (non-Javadoc) 
     102     * @see NodeComparisonRule#isApplicable(ITaskTreeNode, ITaskTreeNode) 
     103     */ 
     104    @Override 
     105    public boolean isApplicable(ITaskTreeNode node1, ITaskTreeNode node2) { 
     106        return (node1 instanceof IIteration) && (node2 instanceof IIteration); 
     107    } 
     108 
     109    /* (non-Javadoc) 
     110     * @see NodeComparisonRule#areLexicallyEqual(ITaskTreeNode, ITaskTreeNode) 
     111     */ 
     112    @Override 
     113    public boolean areLexicallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) { 
     114        List<ITaskTreeNode> children1 = node1.getChildren(); 
     115        List<ITaskTreeNode> children2 = node2.getChildren(); 
     116         
     117        if (children1.size() == children2.size()) { 
     118            if (children1.size() == 0) { 
     119                return true; 
     120            } 
     121            else { 
     122                ITaskTreeNode child1 = children1.get(0); 
     123                ITaskTreeNode child2 = children2.get(0); 
     124                 
     125                // iterations may have 3 different structures. 
     126                // 1. they have one child, which is the iterated one 
     127                // 2. they have a sequence of children, which is iterated 
     128                // 3. they have a selection of different iterated variants (usually the variants 
     129                //    are semantically equal) 
     130                // check if the type of children match. If not, return false. If they match, 
     131                // use the equality manager to perform further comparisons 
     132                 
     133                if (((child1 instanceof ISelection) && (child2 instanceof ISelection)) || 
     134                    ((child1 instanceof ISequence) && (child2 instanceof ISequence)) || 
     135                    ((child1 instanceof IEventTask) && (child2 instanceof IEventTask))) 
     136                { 
     137                    return getNodeEquality 
     138                        (child1, child2).isAtLeast(NodeEquality.LEXICALLY_EQUAL); 
     139                } 
     140            } 
     141        } 
     142         
     143        return false; 
     144    } 
     145 
     146    /* (non-Javadoc) 
     147     * @see NodeComparisonRule#areSyntacticallyEqual(ITaskTreeNode, ITaskTreeNode) 
     148     */ 
     149    @Override 
     150    public boolean areSyntacticallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) { 
     151        List<ITaskTreeNode> children1 = node1.getChildren(); 
     152        List<ITaskTreeNode> children2 = node2.getChildren(); 
     153         
     154        if (children1.size() == children2.size()) { 
     155            if (children1.size() == 0) { 
     156                return true; 
     157            } 
     158            else { 
     159                ITaskTreeNode child1 = children1.get(0); 
     160                ITaskTreeNode child2 = children2.get(0); 
     161                 
     162                // iterations may have 3 different structures. 
     163                // 1. they have one child, which is the iterated one 
     164                // 2. they have a sequence of children, which is iterated 
     165                // 3. they have a selection of different iterated variants (usually the variants 
     166                //    are semantically equal) 
     167                // ignore the type of the children but check them for equality. 
     168                 
     169                return getNodeEquality(child1, child2).isAtLeast(NodeEquality.SYNTACTICALLY_EQUAL); 
     170            } 
     171        } 
     172         
     173        return false; 
     174    } 
     175 
     176    /* (non-Javadoc) 
     177     * @see NodeComparisonRule#areSemanticallyEqual(ITaskTreeNode, ITaskTreeNode) 
     178     */ 
     179    @Override 
     180    public boolean areSemanticallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) { 
     181        return compare(node1, node2).isAtLeast(NodeEquality.SEMANTICALLY_EQUAL); 
     182    } 
     183 
     184    /* (non-Javadoc) 
     185     * @see NodeComparisonRule#compare(ITaskTreeNode, ITaskTreeNode) 
    101186     */ 
    102187    @Override 
    103188    public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) { 
    104         if ((!(node1 instanceof IIteration)) || (!(node2 instanceof IIteration))) { 
    105             return null; 
    106         } 
    107  
    108         if (node1 == node2) { 
    109             return NodeEquality.IDENTICAL; 
    110         } 
     189        List<ITaskTreeNode> children1 = node1.getChildren(); 
     190        List<ITaskTreeNode> children2 = node2.getChildren(); 
    111191 
    112192        // if both iterations do not have children, they are equal although this doesn't make sense 
    113         if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) { 
     193        if ((children1.size() == 0) && (children2.size() == 0)) { 
    114194            return NodeEquality.LEXICALLY_EQUAL; 
    115195        } 
    116         else if ((node1.getChildren().size() == 0) || (node2.getChildren().size() == 0)) { 
     196        else if ((children1.size() == 0) || (children2.size() == 0)) { 
    117197            return NodeEquality.UNEQUAL; 
    118198        } 
    119199 
    120         ITaskTreeNode child1 = node1.getChildren().get(0); 
    121         ITaskTreeNode child2 = node2.getChildren().get(0); 
     200        ITaskTreeNode child1 = children1.get(0); 
     201        ITaskTreeNode child2 = children2.get(0); 
    122202 
    123203        // iterations may have 3 different structures. 
     
    132212        // This condition matches, if both iterations are the same variants of iteration. I.e. three 
    133213        // combinations of the permutation are handled herewith. 
    134         NodeEquality nodeEquality = mRuleManager.applyRules(child1, child2); 
     214        NodeEquality nodeEquality = getNodeEquality(child1, child2); 
     215         
     216        if (nodeEquality != null) { 
     217            return nodeEquality; 
     218        } 
     219 
     220        // compare one iteration with a single node as a child and another one with a selection of 
     221        // semantically equal nodes 
     222        return selectionChildrenSemanticallyEqualNode(child1, child2); 
     223         
     224        // all other combinations (i.e. sequence with single child and sequence with selection) 
     225        // can not match 
     226    } 
     227 
     228    /** 
     229     * TODO update comment 
     230     */ 
     231    private NodeEquality getNodeEquality(ITaskTreeNode child1, ITaskTreeNode child2) { 
     232        NodeEquality nodeEquality = callRuleManager(child1, child2, null); 
    135233 
    136234        if (nodeEquality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)) { 
     
    144242            } 
    145243        } 
    146  
    147         // compare one iteration with a single node as a child and another one with a selection of 
    148         // semantically equal nodes 
    149         return selectionChildrenSemanticallyEqualNode(child1, child2); 
    150          
    151         // all other combinations (i.e. sequence with single child and sequence with selection) 
    152         // can not match 
     244         
     245        return NodeEquality.UNEQUAL; 
    153246    } 
    154247 
     
    189282 
    190283        for (ITaskTreeNode child : selection.getChildren()) { 
    191             NodeEquality nodeEquality = mRuleManager.applyRules(node, child); 
     284            NodeEquality nodeEquality = 
     285                  callRuleManager(node, child, commonDenominatorForAllComparisons); 
    192286 
    193287            if ((nodeEquality == null) || (nodeEquality == NodeEquality.UNEQUAL)) 
     
    203297    } 
    204298 
     299    /** 
     300     * <p> 
     301     * TODO: comment 
     302     * </p> 
     303     * 
     304     * @param child1 
     305     * @param child2 
     306     * @param requiredEqualityLevel 
     307     * @return 
     308     */ 
     309    private NodeEquality callRuleManager(ITaskTreeNode child1, 
     310                                         ITaskTreeNode child2, 
     311                                         NodeEquality  requiredEqualityLevel) 
     312    { 
     313        if (requiredEqualityLevel == null) { 
     314            return mRuleManager.compare(child1, child2); 
     315        } 
     316        else if (mRuleManager.areAtLeastEqual(child1, child2, requiredEqualityLevel)) { 
     317            return requiredEqualityLevel; 
     318        } 
     319        else { 
     320            return NodeEquality.UNEQUAL; 
     321        } 
     322    } 
    205323} 
Note: See TracChangeset for help on using the changeset viewer.