[439] | 1 | // Module : $RCSfile: NodeEqualityRuleManager.java,v $ |
---|
| 2 | // Version : $Revision: 0.0 $ $Author: patrick $ $Date: 19.02.2012 $ |
---|
| 3 | // Project : TaskTreeCreator |
---|
| 4 | // Creation : 2012 by patrick |
---|
| 5 | // Copyright : Patrick Harms, 2012 |
---|
[557] | 6 | |
---|
[439] | 7 | package de.ugoe.cs.quest.tasktrees.nodeequality; |
---|
| 8 | |
---|
| 9 | import java.util.ArrayList; |
---|
| 10 | import java.util.List; |
---|
| 11 | |
---|
[557] | 12 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode; |
---|
[439] | 13 | |
---|
| 14 | /** |
---|
[557] | 15 | * <p> |
---|
| 16 | * The node equality rule manager is capable of comparing task tree nodes based on its internal list |
---|
| 17 | * of comparison rules. The current list of rules contains the {@link NodeIdentityRule}, the |
---|
| 18 | * {@link IterationComparisonRule}, the {@link SequenceComparisonRule}, and |
---|
| 19 | * {@link SelectionComparisonRule}. These rules are asked for comparing the two provided task tree |
---|
| 20 | * nodes in the mentioned order. If a rule returns a node equality other than null, this equality is |
---|
| 21 | * returned. Otherwise the next rule is asked. |
---|
| 22 | * </p> |
---|
[439] | 23 | * |
---|
| 24 | * @version $Revision: $ $Date: 19.02.2012$ |
---|
| 25 | * @author 2012, last modified by $Author: patrick$ |
---|
| 26 | */ |
---|
[557] | 27 | public class NodeEqualityRuleManager { |
---|
[439] | 28 | |
---|
[557] | 29 | /** */ |
---|
| 30 | // private static Logger LOG = Logger.getLogger(NodeEqualityRuleManager.class.getName()); |
---|
[439] | 31 | |
---|
[557] | 32 | /** */ |
---|
| 33 | private List<NodeComparisonRule> mRuleIndex = null; |
---|
[439] | 34 | |
---|
[557] | 35 | /** |
---|
| 36 | * <p> |
---|
| 37 | * initializes the node equality rule manager by filling the internal list of comparison rules. |
---|
| 38 | * This method must be called before any other method is called on the rule manager. |
---|
| 39 | * </p> |
---|
| 40 | */ |
---|
| 41 | public void init() { |
---|
| 42 | mRuleIndex = new ArrayList<NodeComparisonRule>(); |
---|
| 43 | mRuleIndex.add(new NodeIdentityRule()); |
---|
| 44 | mRuleIndex.add(new IterationComparisonRule(this)); |
---|
| 45 | mRuleIndex.add(new SequenceComparisonRule(this)); |
---|
| 46 | mRuleIndex.add(new SelectionComparisonRule(this)); |
---|
| 47 | } |
---|
[439] | 48 | |
---|
[557] | 49 | /** |
---|
| 50 | * <p> |
---|
| 51 | * this method performs a comparison of the two provided task tree nodes. It iterates its |
---|
| 52 | * internal comparison rules. If the first rule returns a node equality other than null, |
---|
| 53 | * this equality is returned. Otherwise the next rule is tried. If no rule returns an equality |
---|
| 54 | * <code>NodeEquality.UNEQUAL</code> is returned. |
---|
| 55 | * </p> |
---|
| 56 | * |
---|
| 57 | * @param node1 the first task tree node to be compared |
---|
| 58 | * @param node2 the second task tree node to be compared |
---|
| 59 | * |
---|
| 60 | * @return as described |
---|
| 61 | * |
---|
| 62 | * @throws IllegalStateException in the case, the {@link #init()} method was not called on the |
---|
| 63 | * manager before a call to this method. |
---|
| 64 | */ |
---|
| 65 | public NodeEquality applyRules(ITaskTreeNode node1, ITaskTreeNode node2) |
---|
| 66 | throws IllegalStateException |
---|
[439] | 67 | { |
---|
[557] | 68 | if (mRuleIndex == null) { |
---|
| 69 | throw new IllegalStateException("not initialized"); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | // LOG.info("checking for equality of " + node1 + " and " + node2); |
---|
| 73 | NodeEquality nodeEquality = null; |
---|
| 74 | |
---|
| 75 | for (NodeComparisonRule rule : mRuleIndex) { |
---|
| 76 | nodeEquality = rule.compare(node1, node2); |
---|
| 77 | |
---|
| 78 | if (nodeEquality != null) { |
---|
| 79 | // LOG.warning("used rule " + rule + " for equality check"); |
---|
| 80 | return nodeEquality; |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | // LOG.warning("no rule could be applied --> handling nodes as unequal"); |
---|
| 85 | |
---|
| 86 | return NodeEquality.UNEQUAL; |
---|
[439] | 87 | } |
---|
| 88 | |
---|
| 89 | } |
---|