[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] | 15 | package de.ugoe.cs.autoquest.tasktrees.nodeequality; |
---|
[439] | 16 | |
---|
| 17 | import java.util.ArrayList; |
---|
| 18 | import java.util.List; |
---|
| 19 | |
---|
[922] | 20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
[439] | 21 | |
---|
| 22 | /** |
---|
[557] | 23 | * <p> |
---|
| 24 | * The node equality rule manager is capable of comparing task tree nodes based on its internal list |
---|
| 25 | * of comparison rules. The current list of rules contains the {@link NodeIdentityRule}, the |
---|
| 26 | * {@link IterationComparisonRule}, the {@link SequenceComparisonRule}, and |
---|
| 27 | * {@link SelectionComparisonRule}. These rules are asked for comparing the two provided task tree |
---|
| 28 | * nodes in the mentioned order. If a rule returns a node equality other than null, this equality is |
---|
| 29 | * returned. Otherwise the next rule is asked. |
---|
| 30 | * </p> |
---|
[439] | 31 | * |
---|
| 32 | * @version $Revision: $ $Date: 19.02.2012$ |
---|
| 33 | * @author 2012, last modified by $Author: patrick$ |
---|
| 34 | */ |
---|
[557] | 35 | public class NodeEqualityRuleManager { |
---|
[439] | 36 | |
---|
[557] | 37 | /** */ |
---|
| 38 | private List<NodeComparisonRule> mRuleIndex = null; |
---|
[439] | 39 | |
---|
[557] | 40 | /** |
---|
| 41 | * <p> |
---|
| 42 | * initializes the node equality rule manager by filling the internal list of comparison rules. |
---|
| 43 | * This method must be called before any other method is called on the rule manager. |
---|
| 44 | * </p> |
---|
| 45 | */ |
---|
| 46 | public void init() { |
---|
| 47 | mRuleIndex = new ArrayList<NodeComparisonRule>(); |
---|
| 48 | mRuleIndex.add(new NodeIdentityRule()); |
---|
[807] | 49 | mRuleIndex.add(new GUIEventTaskComparisonRule()); |
---|
| 50 | mRuleIndex.add(new EventTaskComparisonRule()); |
---|
[557] | 51 | mRuleIndex.add(new IterationComparisonRule(this)); |
---|
| 52 | mRuleIndex.add(new SequenceComparisonRule(this)); |
---|
| 53 | mRuleIndex.add(new SelectionComparisonRule(this)); |
---|
[816] | 54 | mRuleIndex.add(new NodeAndIterationComparisonRule(this)); |
---|
| 55 | mRuleIndex.add(new NodeAndSelectionComparisonRule(this)); |
---|
[557] | 56 | } |
---|
[439] | 57 | |
---|
[557] | 58 | /** |
---|
| 59 | * <p> |
---|
| 60 | * this method performs a comparison of the two provided task tree nodes. It iterates its |
---|
| 61 | * internal comparison rules. If the first rule returns a node equality other than null, |
---|
| 62 | * this equality is returned. Otherwise the next rule is tried. If no rule returns an equality |
---|
| 63 | * <code>NodeEquality.UNEQUAL</code> is returned. |
---|
| 64 | * </p> |
---|
| 65 | * |
---|
| 66 | * @param node1 the first task tree node to be compared |
---|
| 67 | * @param node2 the second task tree node to be compared |
---|
| 68 | * |
---|
| 69 | * @return as described |
---|
| 70 | * |
---|
| 71 | * @throws IllegalStateException in the case, the {@link #init()} method was not called on the |
---|
| 72 | * manager before a call to this method. |
---|
| 73 | */ |
---|
[1125] | 74 | public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) |
---|
[557] | 75 | throws IllegalStateException |
---|
[439] | 76 | { |
---|
[557] | 77 | if (mRuleIndex == null) { |
---|
| 78 | throw new IllegalStateException("not initialized"); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | // LOG.info("checking for equality of " + node1 + " and " + node2); |
---|
| 82 | NodeEquality nodeEquality = null; |
---|
| 83 | |
---|
| 84 | for (NodeComparisonRule rule : mRuleIndex) { |
---|
[1125] | 85 | if (rule.isApplicable(node1, node2)) { |
---|
| 86 | nodeEquality = rule.compare(node1, node2); |
---|
| 87 | if (nodeEquality != null) { |
---|
| 88 | // LOG.warning("used rule " + rule + " for equality check"); |
---|
| 89 | return nodeEquality; |
---|
| 90 | } |
---|
[557] | 91 | } |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | // LOG.warning("no rule could be applied --> handling nodes as unequal"); |
---|
| 95 | |
---|
| 96 | return NodeEquality.UNEQUAL; |
---|
[439] | 97 | } |
---|
| 98 | |
---|
[1125] | 99 | /** |
---|
| 100 | * <p> |
---|
| 101 | * TODO: comment |
---|
| 102 | * </p> |
---|
| 103 | * |
---|
| 104 | * @param child1 |
---|
| 105 | * @param child2 |
---|
| 106 | * @param equalityLevel |
---|
| 107 | * @return |
---|
| 108 | */ |
---|
| 109 | public boolean areAtLeastEqual(ITaskTreeNode node1, |
---|
| 110 | ITaskTreeNode node2, |
---|
| 111 | NodeEquality equalityLevel) |
---|
| 112 | { |
---|
| 113 | if (equalityLevel == null) { |
---|
| 114 | throw new IllegalArgumentException("required equality level must not be null"); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | switch (equalityLevel) { |
---|
| 118 | case IDENTICAL: |
---|
| 119 | return areIdentical(node1, node2); |
---|
| 120 | case LEXICALLY_EQUAL: |
---|
| 121 | return areLexicallyEqual(node1, node2); |
---|
| 122 | case SYNTACTICALLY_EQUAL: |
---|
| 123 | return areSyntacticallyEqual(node1, node2); |
---|
| 124 | case SEMANTICALLY_EQUAL: |
---|
| 125 | return areSemanticallyEqual(node1, node2); |
---|
| 126 | case UNEQUAL: |
---|
| 127 | return !areSemanticallyEqual(node1, node2); |
---|
| 128 | default: |
---|
| 129 | throw new IllegalArgumentException("unknown required equality: " + equalityLevel); |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | /** |
---|
| 134 | * <p> |
---|
| 135 | * TODO: comment |
---|
| 136 | * </p> |
---|
| 137 | * |
---|
| 138 | * @param child1 |
---|
| 139 | * @param child2 |
---|
| 140 | * @return |
---|
| 141 | */ |
---|
| 142 | public boolean areIdentical(ITaskTreeNode node1, ITaskTreeNode node2) { |
---|
| 143 | if (mRuleIndex == null) { |
---|
| 144 | throw new IllegalStateException("not initialized"); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | for (NodeComparisonRule rule : mRuleIndex) { |
---|
| 148 | if (rule.isApplicable(node1, node2) && rule.areLexicallyEqual(node1, node2)) { |
---|
| 149 | return true; |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | return false; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | /** |
---|
| 157 | * <p> |
---|
| 158 | * TODO: comment |
---|
| 159 | * </p> |
---|
| 160 | * |
---|
| 161 | * @param child1 |
---|
| 162 | * @param child2 |
---|
| 163 | * @return |
---|
| 164 | */ |
---|
| 165 | public boolean areLexicallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) { |
---|
| 166 | if (mRuleIndex == null) { |
---|
| 167 | throw new IllegalStateException("not initialized"); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | for (NodeComparisonRule rule : mRuleIndex) { |
---|
| 171 | if (rule.isApplicable(node1, node2) && rule.areLexicallyEqual(node1, node2)) { |
---|
| 172 | return true; |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | return false; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /** |
---|
| 180 | * <p> |
---|
| 181 | * TODO: comment |
---|
| 182 | * </p> |
---|
| 183 | * |
---|
| 184 | * @param child1 |
---|
| 185 | * @param child2 |
---|
| 186 | * @return |
---|
| 187 | */ |
---|
| 188 | public boolean areSyntacticallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) { |
---|
| 189 | if (mRuleIndex == null) { |
---|
| 190 | throw new IllegalStateException("not initialized"); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | for (NodeComparisonRule rule : mRuleIndex) { |
---|
| 194 | if (rule.isApplicable(node1, node2) && rule.areSyntacticallyEqual(node1, node2)) { |
---|
| 195 | return true; |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | return false; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | /** |
---|
| 203 | * <p> |
---|
| 204 | * TODO: comment |
---|
| 205 | * </p> |
---|
| 206 | * |
---|
| 207 | * @param child1 |
---|
| 208 | * @param child2 |
---|
| 209 | * @return |
---|
| 210 | */ |
---|
| 211 | public boolean areSemanticallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) { |
---|
| 212 | if (mRuleIndex == null) { |
---|
| 213 | throw new IllegalStateException("not initialized"); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | for (NodeComparisonRule rule : mRuleIndex) { |
---|
| 217 | if (rule.isApplicable(node1, node2) && rule.areSemanticallyEqual(node1, node2)) { |
---|
| 218 | return true; |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | return false; |
---|
| 223 | } |
---|
| 224 | |
---|
[439] | 225 | } |
---|