source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/nodeequality/NodeAndSelectionComparisonRule.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: 7.0 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.ISelection;
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 a selection with a
25 * selection. This is needed, because selections may contain exactly that node. Therefore, if
26 * this node is selected out of a selection the selection is equal to the node itself.
27 * The rule returns lexically equal, it the selection contains a lexically equal node. The same
28 * applies for syntactical and semantical equality.
29 * </p>
30
31 * @author Patrick Harms
32 */
33public class NodeAndSelectionComparisonRule implements NodeComparisonRule {
34   
35    /** the rule manager for internally comparing task tree nodes */
36    private NodeEqualityRuleManager mRuleManager;
37   
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    NodeAndSelectionComparisonRule(NodeEqualityRuleManager ruleManager) {
47        super();
48        mRuleManager = ruleManager;
49    }
50
51    /* (non-Javadoc)
52     * @see NodeComparisonRule#isApplicable(ITaskTreeNode, ITaskTreeNode)
53     */
54    @Override
55    public boolean isApplicable(ITaskTreeNode node1, ITaskTreeNode node2) {
56        return ((node1 instanceof ISelection) && (!(node2 instanceof ISelection))) ||
57               ((node2 instanceof ISelection) && (!(node1 instanceof ISelection)));
58    }
59
60    /* (non-Javadoc)
61     * @see NodeComparisonRule#areLexicallyEqual(ITaskTreeNode, ITaskTreeNode)
62     */
63    @Override
64    public boolean areLexicallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) {
65        NodeEquality equality = getEquality(node1, node2, NodeEquality.LEXICALLY_EQUAL);
66        return (equality != null) && (equality.isAtLeast(NodeEquality.LEXICALLY_EQUAL));
67    }
68
69    /* (non-Javadoc)
70     * @see NodeComparisonRule#areSyntacticallyEqual(ITaskTreeNode, ITaskTreeNode)
71     */
72    @Override
73    public boolean areSyntacticallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) {
74        NodeEquality equality = getEquality(node1, node2, NodeEquality.SYNTACTICALLY_EQUAL);
75        return (equality != null) && (equality.isAtLeast(NodeEquality.SYNTACTICALLY_EQUAL));
76    }
77
78    /* (non-Javadoc)
79     * @see NodeComparisonRule#areSemanticallyEqual(ITaskTreeNode, ITaskTreeNode)
80     */
81    @Override
82    public boolean areSemanticallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) {
83        NodeEquality equality = getEquality(node1, node2, NodeEquality.SEMANTICALLY_EQUAL);
84        return (equality != null) && (equality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL));
85    }
86
87    /* (non-Javadoc)
88     * @see NodeComparisonRule#compare(ITaskTreeNode, ITaskTreeNode)
89     */
90    @Override
91    public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) {
92        return getEquality(node1, node2, null);
93    }
94   
95    /**
96     *
97     */
98    private NodeEquality getEquality(ITaskTreeNode node1,
99                                     ITaskTreeNode node2,
100                                     NodeEquality  requiredEqualityLevel)
101    {
102        ISelection selection = null;
103        ITaskTreeNode node = null;
104       
105        if (node1 instanceof ISelection) {
106            if (node2 instanceof ISelection) {
107                // the rule is not responsible for two selections
108                return null;
109            }
110           
111            selection = (ISelection) node1;
112            node = node2;
113        }
114        else if (node2 instanceof ISelection) {
115            if (node1 instanceof ISelection) {
116                // the rule is not responsible for two selections
117                return null;
118            }
119           
120            selection = (ISelection) node2;
121            node = node1;
122        }
123        else {
124            return null;
125        }
126
127        // now, that we found the selection and the node, lets compare the children of the selection
128        // with the node.
129        List<ITaskTreeNode> children = selection.getChildren();
130       
131        if (children.size() < 1) {
132            return null;
133        }
134
135        NodeEquality mostConcreteNodeEquality = null;
136       
137        for (ITaskTreeNode child : children) {
138            NodeEquality nodeEquality = callRuleManager(child, node, requiredEqualityLevel);
139           
140            if (nodeEquality != NodeEquality.UNEQUAL) {
141                if (mostConcreteNodeEquality == null) {
142                    mostConcreteNodeEquality = nodeEquality;
143                }
144                else if (mostConcreteNodeEquality.isAtLeast(nodeEquality)) {
145                    mostConcreteNodeEquality = nodeEquality;
146                   
147                }
148               
149                if ((requiredEqualityLevel != null) &&
150                    (mostConcreteNodeEquality.isAtLeast(requiredEqualityLevel)))
151                {
152                    // if we found one child of the selection that is as equal as required, then
153                    // we can consider the selection to be sufficiently equal to the other node.
154                    // So we break up checking further children.
155                    break;
156                }
157            }
158        }
159       
160        // although the subtask may be identical to the node, we can not return identical, as
161        // the selection is not identical to the node, but at most lexically equal
162        if (mostConcreteNodeEquality == NodeEquality.IDENTICAL) {
163            return NodeEquality.LEXICALLY_EQUAL;
164        }
165        else {
166            return mostConcreteNodeEquality;
167        }
168
169    }
170   
171    /**
172     * <p>
173     * TODO: comment
174     * </p>
175     *
176     * @param child1
177     * @param child2
178     * @param requiredEqualityLevel
179     * @return
180     */
181    private NodeEquality callRuleManager(ITaskTreeNode child1,
182                                         ITaskTreeNode child2,
183                                         NodeEquality  requiredEqualityLevel)
184    {
185        if (requiredEqualityLevel == null) {
186            return mRuleManager.compare(child1, child2);
187        }
188        else if (mRuleManager.areAtLeastEqual(child1, child2, requiredEqualityLevel)) {
189            return requiredEqualityLevel;
190        }
191        else {
192            return NodeEquality.UNEQUAL;
193        }
194    }
195}
Note: See TracBrowser for help on using the repository browser.