[1113] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
| 2 | // |
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | // you may not use this file except in compliance with the License. |
---|
| 5 | // You may obtain a copy of the License at |
---|
| 6 | // |
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | // |
---|
| 9 | // Unless required by applicable law or agreed to in writing, software |
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 12 | // See the License for the specific language governing permissions and |
---|
| 13 | // limitations under the License. |
---|
| 14 | |
---|
[922] | 15 | package de.ugoe.cs.autoquest.tasktrees.nodeequality; |
---|
[439] | 16 | |
---|
[922] | 17 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
| 18 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
[439] | 19 | |
---|
| 20 | /** |
---|
[557] | 21 | * <p> |
---|
| 22 | * This rule is capable of comparing sequences. If both sequences do not have children, they are |
---|
| 23 | * treated as lexically equal. Sequences are lexically equal, if they have the same number and |
---|
| 24 | * order of lexically equal children. The rule can not decide, if two sequences are syntactically |
---|
| 25 | * or semantically equal. |
---|
| 26 | * </p> |
---|
[439] | 27 | * |
---|
| 28 | * @version $Revision: $ $Date: 19.02.2012$ |
---|
| 29 | * @author 2012, last modified by $Author: patrick$ |
---|
| 30 | */ |
---|
[557] | 31 | public class SequenceComparisonRule implements NodeComparisonRule { |
---|
[439] | 32 | |
---|
[557] | 33 | /** the rule manager for internally comparing task tree nodes */ |
---|
| 34 | private NodeEqualityRuleManager mRuleManager; |
---|
[439] | 35 | |
---|
[557] | 36 | /** |
---|
| 37 | * <p> |
---|
| 38 | * simple constructor to provide the rule with the node equality rule manager to be able |
---|
| 39 | * to perform comparisons of the children of provided task tree nodes |
---|
| 40 | * </p> |
---|
| 41 | * |
---|
| 42 | * @param ruleManager the rule manager for comparing task tree nodes |
---|
| 43 | */ |
---|
| 44 | SequenceComparisonRule(NodeEqualityRuleManager ruleManager) { |
---|
| 45 | super(); |
---|
| 46 | mRuleManager = ruleManager; |
---|
| 47 | } |
---|
[439] | 48 | |
---|
[557] | 49 | /* |
---|
| 50 | * (non-Javadoc) |
---|
| 51 | * |
---|
| 52 | * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode) |
---|
| 53 | */ |
---|
| 54 | @Override |
---|
| 55 | public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) { |
---|
| 56 | if ((!(node1 instanceof ISequence)) || (!(node2 instanceof ISequence))) { |
---|
| 57 | return null; |
---|
| 58 | } |
---|
[439] | 59 | |
---|
[807] | 60 | if (node1 == node2) { |
---|
| 61 | return NodeEquality.IDENTICAL; |
---|
| 62 | } |
---|
| 63 | |
---|
[557] | 64 | // if both sequences do not have children, they are equal although this doesn't make sense |
---|
| 65 | if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) { |
---|
| 66 | return NodeEquality.LEXICALLY_EQUAL; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | if (node1.getChildren().size() != node2.getChildren().size()) { |
---|
[807] | 70 | return NodeEquality.UNEQUAL; |
---|
[557] | 71 | } |
---|
| 72 | |
---|
[807] | 73 | NodeEquality resultingEquality = NodeEquality.LEXICALLY_EQUAL; |
---|
[557] | 74 | for (int i = 0; i < node1.getChildren().size(); i++) { |
---|
| 75 | ITaskTreeNode child1 = node1.getChildren().get(i); |
---|
| 76 | ITaskTreeNode child2 = node2.getChildren().get(i); |
---|
| 77 | |
---|
| 78 | NodeEquality nodeEquality = mRuleManager.applyRules(child1, child2); |
---|
| 79 | |
---|
[807] | 80 | if ((nodeEquality == null) || (nodeEquality == NodeEquality.UNEQUAL)) { |
---|
| 81 | return NodeEquality.UNEQUAL; |
---|
[557] | 82 | } |
---|
[807] | 83 | |
---|
| 84 | resultingEquality = resultingEquality.getCommonDenominator(nodeEquality); |
---|
[557] | 85 | } |
---|
| 86 | |
---|
[807] | 87 | return resultingEquality; |
---|
[439] | 88 | } |
---|
| 89 | |
---|
| 90 | } |
---|