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/SelectionComparisonRule.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.Sequence; 
    11 import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeNode; 
     9import de.ugoe.cs.quest.tasktrees.treeifc.ISelection; 
     10import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode; 
    1211 
    13 //------------------------------------------------------------------------------------------------- 
    1412/** 
    15  * TODO comment 
     13 * <p> 
     14 * this node comparison rule is capable of comparing selections. If both selections do not have 
     15 * children, they are treated as lexically equal. If they have the same number of children other 
     16 * than 0 and all these children are lexically equal, then the selections are lexically equal. 
     17 * They are syntactically equal, if each child of both selections is syntactically equal to any 
     18 * other child. The rule can not compare semantical equality if the nodes are not at least 
     19 * syntactically equal and returns null, if it can not decide this. 
     20 * </p> 
    1621 *  
    1722 * @version $Revision: $ $Date: 19.02.2012$ 
    1823 * @author 2012, last modified by $Author: patrick$ 
    1924 */ 
    20 //------------------------------------------------------------------------------------------------- 
    21 public class SelectionComparisonRule implements NodeComparisonRule 
    22 { 
     25public class SelectionComparisonRule implements NodeComparisonRule { 
    2326 
    24   /** */ 
    25   private NodeEqualityRuleManager mRuleManager; 
     27    /** the rule manager for internally comparing task tree nodes */ 
     28    private NodeEqualityRuleManager mRuleManager; 
    2629 
    27   //----------------------------------------------------------------------------------------------- 
    28   /** 
    29    * TODO: comment 
    30    * 
    31    */ 
    32   SelectionComparisonRule(NodeEqualityRuleManager ruleManager) 
    33   { 
    34     super(); 
    35     mRuleManager = ruleManager; 
    36   } 
     30    /** 
     31     * <p> 
     32     * simple constructor to provide the rule with the node equality rule manager to be able 
     33     * to perform comparisons of the children of provided task tree nodes 
     34     * </p> 
     35     *  
     36     * @param ruleManager the rule manager for comparing task tree nodes 
     37     */ 
     38    SelectionComparisonRule(NodeEqualityRuleManager ruleManager) { 
     39        super(); 
     40        mRuleManager = ruleManager; 
     41    } 
    3742 
    38   //----------------------------------------------------------------------------------------------- 
    39   /* (non-Javadoc) 
    40    * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode) 
    41    */ 
    42   //----------------------------------------------------------------------------------------------- 
    43   @Override 
    44   public NodeEquality compare(TaskTreeNode node1, TaskTreeNode node2) 
    45   { 
    46     if ((!(node1 instanceof Sequence)) || (!(node2 instanceof Sequence))) 
    47     { 
    48       return null; 
     43    /* 
     44     * (non-Javadoc) 
     45     *  
     46     * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode) 
     47     */ 
     48    @Override 
     49    public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) { 
     50        if ((!(node1 instanceof ISelection)) || (!(node2 instanceof ISelection))) { 
     51            return null; 
     52        } 
     53 
     54        // if both sequences do not have children, they are equal although this doesn't make sense 
     55        if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) { 
     56            return NodeEquality.LEXICALLY_EQUAL; 
     57        } 
     58 
     59        // Selections are syntactically equal, if they have children, which are all syntactically 
     60        // equal. 
     61        // They are lexically equals, if they have the same number and order of lexically equal 
     62        // children 
     63        boolean lexicallyEqual = node1.getChildren().size() == node2.getChildren().size(); 
     64 
     65        for (int i = 0; i < node1.getChildren().size(); i++) { 
     66            ITaskTreeNode child1 = node1.getChildren().get(i); 
     67            boolean foundLexicallyEqualChild = false; 
     68 
     69            for (int j = 0; j < node2.getChildren().size(); j++) { 
     70                ITaskTreeNode child2 = node2.getChildren().get(j); 
     71 
     72                NodeEquality nodeEquality = mRuleManager.applyRules(child1, child2); 
     73 
     74                if (!nodeEquality.isAtLeast(NodeEquality.SYNTACTICALLY_EQUAL)) { 
     75                    return null; 
     76                } 
     77                else if (nodeEquality.isAtLeast(NodeEquality.LEXICALLY_EQUAL)) { 
     78                    foundLexicallyEqualChild = true; 
     79                } 
     80            } 
     81 
     82            // if we compare two children at the same position and if they are lexically equal 
     83            // then it can be further expected, that the selections are lexically equal 
     84            lexicallyEqual &= foundLexicallyEqualChild; 
     85        } 
     86 
     87        if (lexicallyEqual) { 
     88            return NodeEquality.LEXICALLY_EQUAL; 
     89        } 
     90        else { 
     91            return NodeEquality.SYNTACTICALLY_EQUAL; 
     92        } 
    4993    } 
    50      
    51     // if both sequences do not have children, they are equal although this doesn't make sense 
    52     if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) 
    53     { 
    54       return new NodesEqual(); 
    55     } 
    56      
    57     // Selections are semantically equal, if they have children, which are all semantically equal. 
    58     // they are structurally equals, if they have the same number and order of structurally equal 
    59     // children 
    60     boolean structurallyEqual = 
    61       node1.getChildren().size() == node2.getChildren().size(); 
    62      
    63     for (int i = 0; i < node1.getChildren().size(); i++) 
    64     { 
    65       TaskTreeNode child1 = node1.getChildren().get(i); 
    66        
    67       for (int j = 0; j < node2.getChildren().size(); j++) 
    68       { 
    69         TaskTreeNode child2 = node2.getChildren().get(j); 
    70        
    71         NodeEquality nodeEquality = mRuleManager.applyRules(child1, child2); 
    72          
    73         if (!nodeEquality.getSemanticalEquality()) 
    74         { 
    75           return null; 
    76         } 
    77         else if (structurallyEqual && (i == j)) 
    78         { 
    79           // if we compare two children at the same position and if they are structurally equal 
    80           // then it can be further expected, that the selections are structurally equal 
    81           structurallyEqual &= nodeEquality.getStructuralEquality(); 
    82         } 
    83       } 
    84     } 
    85      
    86     if (structurallyEqual) 
    87     { 
    88       return new NodesEqual(); 
    89     } 
    90     else 
    91     { 
    92       return new NodesSemanticallyEqual(); 
    93     } 
    94   } 
    95  
    9694} 
Note: See TracChangeset for help on using the changeset viewer.