source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/nodeequality/NodeAndIterationComparisonRule.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
File size: 6.1 KB
Line 
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
15package de.ugoe.cs.autoquest.tasktrees.nodeequality;
16
17import java.util.List;
18
19import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
20import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
21
22/**
23 * <p>
24 * This class is capable of comparing any task tree node which is not an iteration with an
25 * iteration. This is needed, because iterations may iterate exactly that node. In this
26 * case, the iteration would be equal to that node if it was executed exactly once. The rule
27 * returns lexically equal, it the child of the iteration is lexically equal to the node
28 * or if the child of the iteration is a selection and this selections contains a lexically equal
29 * node. The same applies for syntactical and semantical equality.
30 * </p>
31
32 * @author Patrick Harms
33 */
34public class NodeAndIterationComparisonRule implements NodeComparisonRule {
35   
36    /** the rule manager for internally comparing task tree nodes */
37    private NodeEqualityRuleManager mRuleManager;
38
39    /**
40     * <p>
41     * simple constructor to provide the rule with the node equality rule manager to be able
42     * to perform comparisons of the children of provided task tree nodes
43     * </p>
44     *
45     * @param ruleManager the rule manager for comparing task tree nodes
46     */
47    NodeAndIterationComparisonRule(NodeEqualityRuleManager ruleManager) {
48        super();
49        mRuleManager = ruleManager;
50    }
51
52    /* (non-Javadoc)
53     * @see NodeComparisonRule#isApplicable(ITaskTreeNode, ITaskTreeNode)
54     */
55    @Override
56    public boolean isApplicable(ITaskTreeNode node1, ITaskTreeNode node2) {
57        return ((node1 instanceof IIteration) && (!(node2 instanceof IIteration))) ||
58               ((node2 instanceof IIteration) && (!(node1 instanceof IIteration)));
59    }
60
61    /* (non-Javadoc)
62     * @see NodeComparisonRule#areLexicallyEqual(ITaskTreeNode, ITaskTreeNode)
63     */
64    @Override
65    public boolean areLexicallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) {
66        NodeEquality equality = getEquality(node1, node2, NodeEquality.LEXICALLY_EQUAL);
67        return (equality != null) && (equality.isAtLeast(NodeEquality.LEXICALLY_EQUAL));
68    }
69
70    /* (non-Javadoc)
71     * @see NodeComparisonRule#areSyntacticallyEqual(ITaskTreeNode, ITaskTreeNode)
72     */
73    @Override
74    public boolean areSyntacticallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) {
75        NodeEquality equality = getEquality(node1, node2, NodeEquality.SYNTACTICALLY_EQUAL);
76        return (equality != null) && (equality.isAtLeast(NodeEquality.SYNTACTICALLY_EQUAL));
77    }
78
79    /* (non-Javadoc)
80     * @see NodeComparisonRule#areSemanticallyEqual(ITaskTreeNode, ITaskTreeNode)
81     */
82    @Override
83    public boolean areSemanticallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) {
84        NodeEquality equality = getEquality(node1, node2, NodeEquality.SEMANTICALLY_EQUAL);
85        return (equality != null) && (equality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL));
86    }
87
88    /* (non-Javadoc)
89     * @see NodeComparisonRule#compare(ITaskTreeNode, ITaskTreeNode)
90     */
91    @Override
92    public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) {
93        return getEquality(node1, node2, null);
94    }
95
96    /**
97     *
98     */
99    private NodeEquality getEquality(ITaskTreeNode node1,
100                                     ITaskTreeNode node2,
101                                     NodeEquality  requiredEqualityLevel)
102    {
103        IIteration iteration = null;
104        ITaskTreeNode node = null;
105       
106        if (node1 instanceof IIteration) {
107            if (node2 instanceof IIteration) {
108                // the rule is not responsible for two iterations
109                return null;
110            }
111           
112            iteration = (IIteration) node1;
113            node = node2;
114        }
115        else if (node2 instanceof IIteration) {
116            if (node1 instanceof IIteration) {
117                // the rule is not responsible for two iterations
118                return null;
119            }
120           
121            iteration = (IIteration) node2;
122            node = node1;
123        }
124        else {
125            return null;
126        }
127
128        List<ITaskTreeNode> children = iteration.getChildren();
129       
130        // now, that we found the iteration and the node, lets compare the child of the iteration
131        // with the node.
132        if (children.size() < 1) {
133            return null;
134        }
135
136        NodeEquality nodeEquality = callRuleManager(children.get(0), node, requiredEqualityLevel);
137
138        // although the subtask may be identical to the node, we can not return identical, as
139        // the iteration is not identical to the node, but at most lexically equal
140        if (nodeEquality == NodeEquality.IDENTICAL) {
141            return NodeEquality.LEXICALLY_EQUAL;
142        }
143        else {
144            return nodeEquality;
145        }
146
147    }
148   
149    /**
150     * <p>
151     * TODO: comment
152     * </p>
153     *
154     * @param child1
155     * @param child2
156     * @param requiredEqualityLevel
157     * @return
158     */
159    private NodeEquality callRuleManager(ITaskTreeNode child1,
160                                         ITaskTreeNode child2,
161                                         NodeEquality  requiredEqualityLevel)
162    {
163        if (requiredEqualityLevel == null) {
164            return mRuleManager.compare(child1, child2);
165        }
166        else if (mRuleManager.areAtLeastEqual(child1, child2, requiredEqualityLevel)) {
167            return requiredEqualityLevel;
168        }
169        else {
170            return NodeEquality.UNEQUAL;
171        }
172    }
173}
Note: See TracBrowser for help on using the repository browser.