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/NodeAndSelectionComparisonRule.java

    r1113 r1125  
    1414 
    1515package de.ugoe.cs.autoquest.tasktrees.nodeequality; 
     16 
     17import java.util.List; 
    1618 
    1719import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; 
     
    3335    /** the rule manager for internally comparing task tree nodes */ 
    3436    private NodeEqualityRuleManager mRuleManager; 
    35  
     37     
    3638    /** 
    3739     * <p> 
     
    4749    } 
    4850 
    49     /* 
    50      * (non-Javadoc) 
    51      *  
    52      * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode) 
     51    /* (non-Javadoc) 
     52     * @see NodeComparisonRule#isApplicable(ITaskTreeNode, ITaskTreeNode) 
     53     */ 
     54    @Override 
     55    public boolean isApplicable(ITaskTreeNode node1, ITaskTreeNode node2) { 
     56        return ((node1 instanceof ISelection) && (!(node2 instanceof ISelection))) || 
     57               ((node2 instanceof ISelection) && (!(node1 instanceof ISelection))); 
     58    } 
     59 
     60    /* (non-Javadoc) 
     61     * @see NodeComparisonRule#areLexicallyEqual(ITaskTreeNode, ITaskTreeNode) 
     62     */ 
     63    @Override 
     64    public boolean areLexicallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) { 
     65        NodeEquality equality = getEquality(node1, node2, NodeEquality.LEXICALLY_EQUAL); 
     66        return (equality != null) && (equality.isAtLeast(NodeEquality.LEXICALLY_EQUAL)); 
     67    } 
     68 
     69    /* (non-Javadoc) 
     70     * @see NodeComparisonRule#areSyntacticallyEqual(ITaskTreeNode, ITaskTreeNode) 
     71     */ 
     72    @Override 
     73    public boolean areSyntacticallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) { 
     74        NodeEquality equality = getEquality(node1, node2, NodeEquality.SYNTACTICALLY_EQUAL); 
     75        return (equality != null) && (equality.isAtLeast(NodeEquality.SYNTACTICALLY_EQUAL)); 
     76    } 
     77 
     78    /* (non-Javadoc) 
     79     * @see NodeComparisonRule#areSemanticallyEqual(ITaskTreeNode, ITaskTreeNode) 
     80     */ 
     81    @Override 
     82    public boolean areSemanticallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) { 
     83        NodeEquality equality = getEquality(node1, node2, NodeEquality.SEMANTICALLY_EQUAL); 
     84        return (equality != null) && (equality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)); 
     85    } 
     86 
     87    /* (non-Javadoc) 
     88     * @see NodeComparisonRule#compare(ITaskTreeNode, ITaskTreeNode) 
    5389     */ 
    5490    @Override 
    5591    public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) { 
     92        return getEquality(node1, node2, null); 
     93    } 
     94     
     95    /** 
     96     *  
     97     */ 
     98    private NodeEquality getEquality(ITaskTreeNode node1, 
     99                                     ITaskTreeNode node2, 
     100                                     NodeEquality  requiredEqualityLevel) 
     101    { 
    56102        ISelection selection = null; 
    57103        ITaskTreeNode node = null; 
     
    59105        if (node1 instanceof ISelection) { 
    60106            if (node2 instanceof ISelection) { 
    61                 // the rule is not responsible for two iterations 
     107                // the rule is not responsible for two selections 
    62108                return null; 
    63109            } 
     
    68114        else if (node2 instanceof ISelection) { 
    69115            if (node1 instanceof ISelection) { 
    70                 // the rule is not responsible for two iterations 
     116                // the rule is not responsible for two selections 
    71117                return null; 
    72118            } 
     
    79125        } 
    80126 
    81         // now, that we found the iteration and the node, lets compare the child of the iteration 
     127        // now, that we found the selection and the node, lets compare the children of the selection 
    82128        // with the node. 
    83         if (selection.getChildren().size() < 1) { 
     129        List<ITaskTreeNode> children = selection.getChildren(); 
     130         
     131        if (children.size() < 1) { 
    84132            return null; 
    85133        } 
     
    87135        NodeEquality mostConcreteNodeEquality = null; 
    88136         
    89         for (ITaskTreeNode child : selection.getChildren()) { 
    90             NodeEquality nodeEquality = mRuleManager.applyRules(child, node); 
     137        for (ITaskTreeNode child : children) { 
     138            NodeEquality nodeEquality = callRuleManager(child, node, requiredEqualityLevel); 
    91139             
    92140            if (nodeEquality != NodeEquality.UNEQUAL) { 
     
    94142                    mostConcreteNodeEquality = nodeEquality; 
    95143                } 
    96                 else { 
    97                     mostConcreteNodeEquality = 
    98                         mostConcreteNodeEquality.getCommonDenominator(nodeEquality); 
     144                else if (mostConcreteNodeEquality.isAtLeast(nodeEquality)) { 
     145                    mostConcreteNodeEquality = nodeEquality; 
     146                     
     147                } 
     148                 
     149                if ((requiredEqualityLevel != null) && 
     150                    (mostConcreteNodeEquality.isAtLeast(requiredEqualityLevel))) 
     151                { 
     152                    // if we found one child of the selection that is as equal as required, then 
     153                    // we can consider the selection to be sufficiently equal to the other node. 
     154                    // So we break up checking further children. 
     155                    break; 
    99156                } 
    100157            } 
     
    111168 
    112169    } 
     170     
     171    /** 
     172     * <p> 
     173     * TODO: comment 
     174     * </p> 
     175     * 
     176     * @param child1 
     177     * @param child2 
     178     * @param requiredEqualityLevel 
     179     * @return 
     180     */ 
     181    private NodeEquality callRuleManager(ITaskTreeNode child1, 
     182                                         ITaskTreeNode child2, 
     183                                         NodeEquality  requiredEqualityLevel) 
     184    { 
     185        if (requiredEqualityLevel == null) { 
     186            return mRuleManager.compare(child1, child2); 
     187        } 
     188        else if (mRuleManager.areAtLeastEqual(child1, child2, requiredEqualityLevel)) { 
     189            return requiredEqualityLevel; 
     190        } 
     191        else { 
     192            return NodeEquality.UNEQUAL; 
     193        } 
     194    } 
    113195} 
Note: See TracChangeset for help on using the changeset viewer.