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

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