Changeset 557 for trunk/quest-core-tasktrees/src/main/java/de/ugoe/cs/quest/tasktrees/nodeequality/SequenceComparisonRule.java
- Timestamp:
- 08/17/12 08:33:29 (12 years ago)
- 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 //-------------------------------------------------------------------------------------------------2 1 // Module : $RCSfile: NodeIdentityRule.java,v $ 3 2 // Version : $Revision: 0.0 $ $Author: patrick $ $Date: 19.02.2012 $ … … 5 4 // Creation : 2012 by patrick 6 5 // Copyright : Patrick Harms, 2012 7 //------------------------------------------------------------------------------------------------- 6 8 7 package de.ugoe.cs.quest.tasktrees.nodeequality; 9 8 10 import de.ugoe.cs.quest.tasktrees.treeifc. Sequence;11 import de.ugoe.cs.quest.tasktrees.treeifc. TaskTreeNode;9 import de.ugoe.cs.quest.tasktrees.treeifc.ISequence; 10 import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode; 12 11 13 //-------------------------------------------------------------------------------------------------14 12 /** 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> 16 19 * 17 20 * @version $Revision: $ $Date: 19.02.2012$ 18 21 * @author 2012, last modified by $Author: patrick$ 19 22 */ 20 //------------------------------------------------------------------------------------------------- 21 public class SequenceComparisonRule implements NodeComparisonRule 22 { 23 public class SequenceComparisonRule implements NodeComparisonRule { 23 24 25 /** the rule manager for internally comparing task tree nodes */ 26 private NodeEqualityRuleManager mRuleManager; 24 27 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 } 27 40 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 } 39 51 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; 51 74 } 52 53 // if both sequences do not have children, they are equal although this doesn't make sense54 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 the60 // children match structurally, then the sequences are structurally equal. Otherwise, if61 // at least one pair of children matches only semantically, then the sequences match only62 // 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 else89 {90 return new NodesSemanticallyEqual();91 }92 }93 75 94 76 }
Note: See TracChangeset
for help on using the changeset viewer.