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

Last change on this file since 807 was 807, checked in by pharms, 12 years ago
  • improved node equality comparison to match the principle of lexical, syntactical and semantical node equality. As a result, more condensed task trees are created.
  • Property svn:executable set to *
File size: 3.0 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 GUIEventTaskComparisonRule());
36        mRuleIndex.add(new EventTaskComparisonRule());
37        mRuleIndex.add(new IterationComparisonRule(this));
38        mRuleIndex.add(new SequenceComparisonRule(this));
39        mRuleIndex.add(new SelectionComparisonRule(this));
40    }
41
42    /**
43     * <p>
44     * this method performs a comparison of the two provided task tree nodes. It iterates its
45     * internal comparison rules. If the first rule returns a node equality other than null,
46     * this equality is returned. Otherwise the next rule is tried. If no rule returns an equality
47     * <code>NodeEquality.UNEQUAL</code> is returned.
48     * </p>
49     *
50     * @param node1 the first task tree node to be compared
51     * @param node2 the second task tree node to be compared
52     *
53     * @return as described
54     *
55     * @throws IllegalStateException in the case, the {@link #init()} method was not called on the
56     *                               manager before a call to this method.
57     */
58    public NodeEquality applyRules(ITaskTreeNode node1, ITaskTreeNode node2)
59        throws IllegalStateException
60    {
61        if (mRuleIndex == null) {
62            throw new IllegalStateException("not initialized");
63        }
64       
65        // LOG.info("checking for equality of " + node1 + " and " + node2);
66        NodeEquality nodeEquality = null;
67
68        for (NodeComparisonRule rule : mRuleIndex) {
69            nodeEquality = rule.compare(node1, node2);
70
71            if (nodeEquality != null) {
72                // LOG.warning("used rule " + rule + " for equality check");
73                return nodeEquality;
74            }
75        }
76
77        // LOG.warning("no rule could be applied --> handling nodes as unequal");
78
79        return NodeEquality.UNEQUAL;
80    }
81
82}
Note: See TracBrowser for help on using the repository browser.