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

Last change on this file since 1113 was 1113, checked in by pharms, 11 years ago
  • added license statement
File size: 4.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 de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
18import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
19
20/**
21 * <p>
22 * This class is capable of comparing any task tree node which is not a selection with a
23 * selection. This is needed, because selections may contain exactly that node. Therefore, if
24 * this node is selected out of a selection the selection is equal to the node itself.
25 * The rule returns lexically equal, it the selection contains a lexically equal node. The same
26 * applies for syntactical and semantical equality.
27 * </p>
28
29 * @author Patrick Harms
30 */
31public class NodeAndSelectionComparisonRule implements NodeComparisonRule {
32   
33    /** the rule manager for internally comparing task tree nodes */
34    private NodeEqualityRuleManager mRuleManager;
35
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    NodeAndSelectionComparisonRule(NodeEqualityRuleManager ruleManager) {
45        super();
46        mRuleManager = ruleManager;
47    }
48
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        ISelection selection = null;
57        ITaskTreeNode node = null;
58       
59        if (node1 instanceof ISelection) {
60            if (node2 instanceof ISelection) {
61                // the rule is not responsible for two iterations
62                return null;
63            }
64           
65            selection = (ISelection) node1;
66            node = node2;
67        }
68        else if (node2 instanceof ISelection) {
69            if (node1 instanceof ISelection) {
70                // the rule is not responsible for two iterations
71                return null;
72            }
73           
74            selection = (ISelection) node2;
75            node = node1;
76        }
77        else {
78            return null;
79        }
80
81        // now, that we found the iteration and the node, lets compare the child of the iteration
82        // with the node.
83        if (selection.getChildren().size() < 1) {
84            return null;
85        }
86
87        NodeEquality mostConcreteNodeEquality = null;
88       
89        for (ITaskTreeNode child : selection.getChildren()) {
90            NodeEquality nodeEquality = mRuleManager.applyRules(child, node);
91           
92            if (nodeEquality != NodeEquality.UNEQUAL) {
93                if (mostConcreteNodeEquality == null) {
94                    mostConcreteNodeEquality = nodeEquality;
95                }
96                else {
97                    mostConcreteNodeEquality =
98                        mostConcreteNodeEquality.getCommonDenominator(nodeEquality);
99                }
100            }
101        }
102       
103        // although the subtask may be identical to the node, we can not return identical, as
104        // the selection is not identical to the node, but at most lexically equal
105        if (mostConcreteNodeEquality == NodeEquality.IDENTICAL) {
106            return NodeEquality.LEXICALLY_EQUAL;
107        }
108        else {
109            return mostConcreteNodeEquality;
110        }
111
112    }
113}
Note: See TracBrowser for help on using the repository browser.