[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 | */ |
---|
| 74 | public NodeEquality applyRules(ITaskTreeNode node1, ITaskTreeNode node2) |
---|
| 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) { |
---|
| 85 | nodeEquality = rule.compare(node1, node2); |
---|
| 86 | |
---|
| 87 | if (nodeEquality != null) { |
---|
| 88 | // LOG.warning("used rule " + rule + " for equality check"); |
---|
| 89 | return nodeEquality; |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | // LOG.warning("no rule could be applied --> handling nodes as unequal"); |
---|
| 94 | |
---|
| 95 | return NodeEquality.UNEQUAL; |
---|
[439] | 96 | } |
---|
| 97 | |
---|
| 98 | } |
---|