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

Last change on this file since 1113 was 1113, checked in by pharms, 11 years ago
  • added license statement
  • Property svn:executable set to *
File size: 5.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 de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
18import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
19
20/**
21 * <p>
22 * this node comparison rule is capable of comparing selections. If both selections do not have
23 * children, they are treated as identical. If they have children, each child of both selections
24 * is compared to each child of the respective other selection. The resulting equality is the most
25 * concrete one of all these comparisons. I.e. if all children are at least lexically equal, then
26 * the selections are lexically equal. If all children are at least syntactically equal, then the
27 * selections are syntactically equal. If all children are at least semantically equal, then the
28 * selections are semantically equal. If only one of the selections has children, then the
29 * selections are unequal.
30 * </p>
31 *
32 * @version $Revision: $ $Date: 19.02.2012$
33 * @author 2012, last modified by $Author: patrick$
34 */
35public class SelectionComparisonRule implements NodeComparisonRule {
36
37    /** the rule manager for internally comparing task tree nodes */
38    private NodeEqualityRuleManager mRuleManager;
39
40    /**
41     * <p>
42     * simple constructor to provide the rule with the node equality rule manager to be able
43     * to perform comparisons of the children of provided task tree nodes
44     * </p>
45     *
46     * @param ruleManager the rule manager for comparing task tree nodes
47     */
48    SelectionComparisonRule(NodeEqualityRuleManager ruleManager) {
49        super();
50        mRuleManager = ruleManager;
51    }
52
53    /*
54     * (non-Javadoc)
55     *
56     * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode)
57     */
58    @Override
59    public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) {
60        if ((!(node1 instanceof ISelection)) || (!(node2 instanceof ISelection))) {
61            return null;
62        }
63
64        if (node1 == node2) {
65            return NodeEquality.IDENTICAL;
66        }
67
68        // if both sequences do not have children, they are identical. If only one of them has
69        // children, they are unequal.
70        if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) {
71            return NodeEquality.LEXICALLY_EQUAL;
72        }
73        else if ((node1.getChildren().size() == 0) || (node2.getChildren().size() == 0)) {
74            return NodeEquality.UNEQUAL;
75        }
76
77        NodeEquality selectionEquality = NodeEquality.LEXICALLY_EQUAL;
78
79        // compare each child of selection one with each child of selection two
80        NodeEquality childEquality;
81        NodeEquality currentEquality;
82        for (ITaskTreeNode child1 : node1.getChildren()) {
83            childEquality = null;
84            for (ITaskTreeNode child2 : node2.getChildren()) {
85                currentEquality = mRuleManager.applyRules(child1, child2);
86                if ((currentEquality != null) && (currentEquality != NodeEquality.UNEQUAL)) {
87                    if (childEquality == null) {
88                        childEquality = currentEquality;
89                    }
90                    else {
91                        childEquality = childEquality.getCommonDenominator(currentEquality);
92                    }
93                }
94            }
95           
96            if (childEquality != null) {
97                selectionEquality = selectionEquality.getCommonDenominator(childEquality);
98            }
99            else {
100                return NodeEquality.UNEQUAL;
101            }
102        }
103
104        // compare each child of selection two with each child of selection one
105        for (ITaskTreeNode child2 : node2.getChildren()) {
106            childEquality = null;
107            for (ITaskTreeNode child1 : node1.getChildren()) {
108                currentEquality = mRuleManager.applyRules(child1, child2);
109                if ((currentEquality != null) && (currentEquality != NodeEquality.UNEQUAL)) {
110                    if (childEquality == null) {
111                        childEquality = currentEquality;
112                    }
113                    else {
114                        childEquality = childEquality.getCommonDenominator(currentEquality);
115                    }
116                }
117            }
118           
119            if (childEquality != null) {
120                selectionEquality = selectionEquality.getCommonDenominator(childEquality);
121            }
122            else {
123                return NodeEquality.UNEQUAL;
124            }
125        }
126
127        return selectionEquality;
128    }
129
130}
Note: See TracBrowser for help on using the repository browser.