source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/nodeequality/SequenceComparisonRule.java @ 1125

Last change on this file since 1125 was 1125, checked in by pharms, 11 years ago
  • refactoring of task tree node comparison to be able to optimize the comparisons for the different comparison levels lexically, syntactically, semantically
  • Property svn:executable set to *
File size: 5.5 KB
RevLine 
[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]15package de.ugoe.cs.autoquest.tasktrees.nodeequality;
[439]16
[1125]17import java.util.List;
18
[922]19import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
20import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
[439]21
22/**
[557]23 * <p>
24 * This rule is capable of comparing sequences. If both sequences do not have children, they are
25 * treated as lexically equal. Sequences are lexically equal, if they have the same number and
26 * order of lexically equal children. The rule can not decide, if two sequences are syntactically
27 * or semantically equal.
28 * </p>
[439]29 *
30 * @version $Revision: $ $Date: 19.02.2012$
31 * @author 2012, last modified by $Author: patrick$
32 */
[557]33public class SequenceComparisonRule implements NodeComparisonRule {
[439]34
[557]35    /** the rule manager for internally comparing task tree nodes */
36    private NodeEqualityRuleManager mRuleManager;
[439]37
[557]38    /**
39     * <p>
40     * simple constructor to provide the rule with the node equality rule manager to be able
41     * to perform comparisons of the children of provided task tree nodes
42     * </p>
43     *
44     * @param ruleManager the rule manager for comparing task tree nodes
45     */
46    SequenceComparisonRule(NodeEqualityRuleManager ruleManager) {
47        super();
48        mRuleManager = ruleManager;
49    }
[439]50
[1125]51    /* (non-Javadoc)
52     * @see NodeComparisonRule#isApplicable(ITaskTreeNode, ITaskTreeNode)
[557]53     */
54    @Override
[1125]55    public boolean isApplicable(ITaskTreeNode node1, ITaskTreeNode node2) {
56        return (node1 instanceof ISequence) && (node2 instanceof ISequence);
57    }
58
59    /* (non-Javadoc)
60     * @see NodeComparisonRule#areLexicallyEqual(ITaskTreeNode, ITaskTreeNode)
61     */
62    @Override
63    public boolean areLexicallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) {
64        NodeEquality equality = getEquality(node1, node2, NodeEquality.LEXICALLY_EQUAL);
65        return (equality != null) && (equality.isAtLeast(NodeEquality.LEXICALLY_EQUAL));
66    }
67
68    /* (non-Javadoc)
69     * @see NodeComparisonRule#areSyntacticallyEqual(ITaskTreeNode, ITaskTreeNode)
70     */
71    @Override
72    public boolean areSyntacticallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) {
73        NodeEquality equality = getEquality(node1, node2, NodeEquality.SYNTACTICALLY_EQUAL);
74        return (equality != null) && (equality.isAtLeast(NodeEquality.SYNTACTICALLY_EQUAL));
75    }
76
77    /* (non-Javadoc)
78     * @see NodeComparisonRule#areSemanticallyEqual(ITaskTreeNode, ITaskTreeNode)
79     */
80    @Override
81    public boolean areSemanticallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) {
82        NodeEquality equality = getEquality(node1, node2, NodeEquality.SEMANTICALLY_EQUAL);
83        return (equality != null) && (equality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL));
84    }
85
86    /* (non-Javadoc)
87     * @see NodeComparisonRule#compare(ITaskTreeNode, ITaskTreeNode)
88     */
89    @Override
[557]90    public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) {
[1125]91        return getEquality(node1, node2, null);
92    }
[439]93
[1125]94    /**
95     *
96     */
97    private NodeEquality getEquality(ITaskTreeNode node1,
98                                     ITaskTreeNode node2,
99                                     NodeEquality  requiredEqualityLevel)
100    {
101        List<ITaskTreeNode> children1 = node1.getChildren();
102        List<ITaskTreeNode> children2 = node2.getChildren();
[807]103
[557]104        // if both sequences do not have children, they are equal although this doesn't make sense
[1125]105        if ((children1.size() == 0) && (children2.size() == 0)) {
[557]106            return NodeEquality.LEXICALLY_EQUAL;
107        }
108
[1125]109        if (children1.size() != children2.size()) {
[807]110            return NodeEquality.UNEQUAL;
[557]111        }
112
[807]113        NodeEquality resultingEquality = NodeEquality.LEXICALLY_EQUAL;
[1125]114        for (int i = 0; i < children1.size(); i++) {
115            ITaskTreeNode child1 = children1.get(i);
116            ITaskTreeNode child2 = children2.get(i);
[557]117
[1125]118            NodeEquality nodeEquality = callRuleManager(child1, child2, requiredEqualityLevel);
[557]119
[807]120            if ((nodeEquality == null) || (nodeEquality == NodeEquality.UNEQUAL)) {
121                return NodeEquality.UNEQUAL;
[557]122            }
[807]123           
124            resultingEquality = resultingEquality.getCommonDenominator(nodeEquality);
[557]125        }
126
[807]127        return resultingEquality;
[439]128    }
129
[1125]130    /**
131     * <p>
132     * TODO: comment
133     * </p>
134     *
135     * @param child1
136     * @param child2
137     * @param requiredEqualityLevel
138     * @return
139     */
140    private NodeEquality callRuleManager(ITaskTreeNode child1,
141                                         ITaskTreeNode child2,
142                                         NodeEquality  requiredEqualityLevel)
143    {
144        if (requiredEqualityLevel == null) {
145            return mRuleManager.compare(child1, child2);
146        }
147        else if (mRuleManager.areAtLeastEqual(child1, child2, requiredEqualityLevel)) {
148            return requiredEqualityLevel;
149        }
150        else {
151            return NodeEquality.UNEQUAL;
152        }
153    }
[439]154}
Note: See TracBrowser for help on using the repository browser.