source: trunk/quest-core-tasktrees/src/main/java/de/ugoe/cs/quest/tasktrees/nodeequality/NodeEqualityRuleManager.java @ 725

Last change on this file since 725 was 725, checked in by pharms, 12 years ago
  • use console for logging instead of logger
  • Property svn:executable set to *
File size: 2.9 KB
Line 
1package de.ugoe.cs.quest.tasktrees.nodeequality;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode;
7
8/**
9 * <p>
10 * The node equality rule manager is capable of comparing task tree nodes based on its internal list
11 * of comparison rules. The current list of rules contains the {@link NodeIdentityRule}, the
12 * {@link IterationComparisonRule}, the {@link SequenceComparisonRule}, and
13 * {@link SelectionComparisonRule}. These rules are asked for comparing the two provided task tree
14 * nodes in the mentioned order. If a rule returns a node equality other than null, this equality is
15 * returned. Otherwise the next rule is asked.
16 * </p>
17 *
18 * @version $Revision: $ $Date: 19.02.2012$
19 * @author 2012, last modified by $Author: patrick$
20 */
21public class NodeEqualityRuleManager {
22
23    /** */
24    private List<NodeComparisonRule> mRuleIndex = null;
25
26    /**
27     * <p>
28     * initializes the node equality rule manager by filling the internal list of comparison rules.
29     * This method must be called before any other method is called on the rule manager.
30     * </p>
31     */
32    public void init() {
33        mRuleIndex = new ArrayList<NodeComparisonRule>();
34        mRuleIndex.add(new NodeIdentityRule());
35        mRuleIndex.add(new IterationComparisonRule(this));
36        mRuleIndex.add(new SequenceComparisonRule(this));
37        mRuleIndex.add(new SelectionComparisonRule(this));
38    }
39
40    /**
41     * <p>
42     * this method performs a comparison of the two provided task tree nodes. It iterates its
43     * internal comparison rules. If the first rule returns a node equality other than null,
44     * this equality is returned. Otherwise the next rule is tried. If no rule returns an equality
45     * <code>NodeEquality.UNEQUAL</code> is returned.
46     * </p>
47     *
48     * @param node1 the first task tree node to be compared
49     * @param node2 the second task tree node to be compared
50     *
51     * @return as described
52     *
53     * @throws IllegalStateException in the case, the {@link #init()} method was not called on the
54     *                               manager before a call to this method.
55     */
56    public NodeEquality applyRules(ITaskTreeNode node1, ITaskTreeNode node2)
57        throws IllegalStateException
58    {
59        if (mRuleIndex == null) {
60            throw new IllegalStateException("not initialized");
61        }
62       
63        // LOG.info("checking for equality of " + node1 + " and " + node2);
64        NodeEquality nodeEquality = null;
65
66        for (NodeComparisonRule rule : mRuleIndex) {
67            nodeEquality = rule.compare(node1, node2);
68
69            if (nodeEquality != null) {
70                // LOG.warning("used rule " + rule + " for equality check");
71                return nodeEquality;
72            }
73        }
74
75        // LOG.warning("no rule could be applied --> handling nodes as unequal");
76
77        return NodeEquality.UNEQUAL;
78    }
79
80}
Note: See TracBrowser for help on using the repository browser.