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/SequenceComparisonRule.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.ISequence; 
     10import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode; 
    1211 
    13 //------------------------------------------------------------------------------------------------- 
    1412/** 
    15  * TODO comment 
     13 * <p> 
     14 * This rule is capable of comparing sequences. If both sequences do not have children, they are 
     15 * treated as lexically equal. Sequences are lexically equal, if they have the same number and 
     16 * order of lexically equal children. The rule can not decide, if two sequences are syntactically 
     17 * or semantically equal. 
     18 * </p> 
    1619 *  
    1720 * @version $Revision: $ $Date: 19.02.2012$ 
    1821 * @author 2012, last modified by $Author: patrick$ 
    1922 */ 
    20 //------------------------------------------------------------------------------------------------- 
    21 public class SequenceComparisonRule implements NodeComparisonRule 
    22 { 
     23public class SequenceComparisonRule implements NodeComparisonRule { 
    2324 
     25    /** the rule manager for internally comparing task tree nodes */ 
     26    private NodeEqualityRuleManager mRuleManager; 
    2427 
    25   /** */ 
    26   private NodeEqualityRuleManager mRuleManager; 
     28    /** 
     29     * <p> 
     30     * simple constructor to provide the rule with the node equality rule manager to be able 
     31     * to perform comparisons of the children of provided task tree nodes 
     32     * </p> 
     33     *  
     34     * @param ruleManager the rule manager for comparing task tree nodes 
     35     */ 
     36    SequenceComparisonRule(NodeEqualityRuleManager ruleManager) { 
     37        super(); 
     38        mRuleManager = ruleManager; 
     39    } 
    2740 
    28   //----------------------------------------------------------------------------------------------- 
    29   /** 
    30    * TODO: comment 
    31    * 
    32    */ 
    33   //----------------------------------------------------------------------------------------------- 
    34   SequenceComparisonRule(NodeEqualityRuleManager ruleManager) 
    35   { 
    36     super(); 
    37     mRuleManager = ruleManager; 
    38   } 
     41    /* 
     42     * (non-Javadoc) 
     43     *  
     44     * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode) 
     45     */ 
     46    @Override 
     47    public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) { 
     48        if ((!(node1 instanceof ISequence)) || (!(node2 instanceof ISequence))) { 
     49            return null; 
     50        } 
    3951 
    40   //----------------------------------------------------------------------------------------------- 
    41   /* (non-Javadoc) 
    42    * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode) 
    43    */ 
    44   //----------------------------------------------------------------------------------------------- 
    45   @Override 
    46   public NodeEquality compare(TaskTreeNode node1, TaskTreeNode node2) 
    47   { 
    48     if ((!(node1 instanceof Sequence)) || (!(node2 instanceof Sequence))) 
    49     { 
    50       return null; 
     52        // if both sequences do not have children, they are equal although this doesn't make sense 
     53        if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) { 
     54            return NodeEquality.LEXICALLY_EQUAL; 
     55        } 
     56 
     57        //  
     58        if (node1.getChildren().size() != node2.getChildren().size()) { 
     59            return null; 
     60        } 
     61 
     62        for (int i = 0; i < node1.getChildren().size(); i++) { 
     63            ITaskTreeNode child1 = node1.getChildren().get(i); 
     64            ITaskTreeNode child2 = node2.getChildren().get(i); 
     65 
     66            NodeEquality nodeEquality = mRuleManager.applyRules(child1, child2); 
     67 
     68            if (!nodeEquality.isAtLeast(NodeEquality.LEXICALLY_EQUAL)) { 
     69                return null; 
     70            } 
     71        } 
     72 
     73        return NodeEquality.LEXICALLY_EQUAL; 
    5174    } 
    52      
    53     // if both sequences do not have children, they are equal although this doesn't make sense 
    54     if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) 
    55     { 
    56       return new NodesEqual(); 
    57     } 
    58      
    59     // Sequences are equal, if they have the same number and order of children. If all the 
    60     // children match structurally, then the sequences are structurally equal. Otherwise, if 
    61     // at least one pair of children matches only semantically, then the sequences match only 
    62     // semantically. 
    63     if (node1.getChildren().size() != node2.getChildren().size()) 
    64     { 
    65       return null; 
    66     } 
    67      
    68     boolean structurallyEqual = true; 
    69     for (int i = 0; i < node1.getChildren().size(); i++) 
    70     { 
    71       TaskTreeNode child1 = node1.getChildren().get(i); 
    72       TaskTreeNode child2 = node2.getChildren().get(i); 
    73        
    74       NodeEquality nodeEquality = mRuleManager.applyRules(child1, child2); 
    75        
    76       structurallyEqual &= nodeEquality.getStructuralEquality(); 
    77        
    78       if (!structurallyEqual && !nodeEquality.getSemanticalEquality()) 
    79       { 
    80         return null; 
    81       } 
    82     } 
    83      
    84     if (structurallyEqual) 
    85     { 
    86       return new NodesEqual(); 
    87     } 
    88     else 
    89     { 
    90       return new NodesSemanticallyEqual(); 
    91     } 
    92   } 
    9375 
    9476} 
Note: See TracChangeset for help on using the changeset viewer.