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

Last change on this file since 557 was 557, checked in by pharms, 12 years ago
  • adapted task tree creation stuff to more general event handling
  • Property svn:executable set to *
File size: 6.5 KB
Line 
1// Module    : $RCSfile: NodeIdentityRule.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
6
7package de.ugoe.cs.quest.tasktrees.nodeequality;
8
9import de.ugoe.cs.quest.tasktrees.treeifc.IIteration;
10import de.ugoe.cs.quest.tasktrees.treeifc.ISelection;
11import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode;
12
13/**
14 * <p>
15 * This class is capable of comparing Iterations. Iterations equal at distinct levels
16 * in distinct situations. The following table shows the results of the comparison for the
17 * specific situations (the parameters are commutative). In any other situation, the comparison
18 * returns <code>NodeEquality.UNEQUAL</code>:
19 * </p>
20 *
21 * <table border="1">
22 *   <tr>
23 *     <th>iteration 1</th>
24 *     <th>iteration 2</th>
25 *     <th>comparison result</th>
26 *   </tr>
27 *   <tr>
28 *     <td>any iteration</td>
29 *     <td>any iteration with a child that is lexically equal to the child of iteration 1</td>
30 *     <td><code>NodeEquality.LEXICALLY_EQUAL</code></td>
31 *   </tr>
32 *   <tr>
33 *     <td>any iteration</td>
34 *     <td>any iteration with a child that is syntactically equal to the child of iteration 1</td>
35 *     <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td>
36 *   </tr>
37 *   <tr>
38 *     <td>any iteration</td>
39 *     <td>any iteration with a child that is semantically equal to the child of iteration 1</td>
40 *     <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td>
41 *   </tr>
42 *   <tr>
43 *     <td>an iteration with a selection of syntactically equal children</td>
44 *     <td>an iteration with a child that is syntactically equal to the children of the child
45 *     selection of iteration 1</td>
46 *     <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td>
47 *   </tr>
48 *   <tr>
49 *     <td>an iteration with a selection of semantically equal children</td>
50 *     <td>an iteration with a child that is semantically equal to the children of the child
51 *     selection of iteration 1</td>
52 *     <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td>
53 *   </tr>
54 * </table>
55 *
56 * @version $Revision: $ $Date: 19.02.2012$
57 * @author 2012, last modified by $Author: patrick$
58 */
59public class IterationComparisonRule implements NodeComparisonRule {
60   
61    /** the rule manager for internally comparing task tree nodes */
62    private NodeEqualityRuleManager mRuleManager;
63
64    /**
65     * <p>
66     * simple constructor to provide the rule with the node equality rule manager to be able
67     * to perform comparisons of the children of provided task tree nodes
68     * </p>
69     *
70     * @param ruleManager the rule manager for comparing task tree nodes
71     */
72    IterationComparisonRule(NodeEqualityRuleManager ruleManager) {
73        super();
74        mRuleManager = ruleManager;
75    }
76
77    /*
78     * (non-Javadoc)
79     *
80     * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode)
81     */
82    @Override
83    public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) {
84        if ((!(node1 instanceof IIteration)) || (!(node2 instanceof IIteration))) {
85            return null;
86        }
87
88        // if both iterations do not have children, they are equal although this doesn't make sense
89        if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) {
90            return NodeEquality.IDENTICAL;
91        }
92
93        ITaskTreeNode child1 = node1.getChildren().get(0);
94        ITaskTreeNode child2 = node2.getChildren().get(0);
95
96        // iterations may have 3 different structures.
97        // 1. they have one child, which is the iterated one
98        // 2. they have a sequence of children, which is iterated
99        // 3. they have a selection of different iterated variants (usually the variants are
100        // semantically equal)
101        //
102        // the permutations of the three variants in combination must be checked
103
104        // check if both nodes are the same variants of iterations and if their children are equal.
105        // This condition matches, if both iterations are the same variants of iteration. I.e. three
106        // combinations of the permutation are handled herewith.
107        NodeEquality nodeEquality = mRuleManager.applyRules(child1, child2);
108
109        if (nodeEquality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)) {
110            return nodeEquality;
111        }
112
113        // compare one iteration with a single node as a child and another one with a selection of
114        // semantically equal nodes
115        return selectionChildrenSemanticallyEqualNode(child1, child2);
116       
117        // all other combinations (i.e. sequence with single child and sequence with selection)
118        // can not match
119    }
120
121    /**
122     * <p>
123     * compares two task tree nodes. One of them must be a selection of at least semantically
124     * equal children. The other one can be any task tree node. The method returns a node equality
125     * that is not <code>NodeEquality.UNEQUAL</code> if the other node is at least semantically
126     * equal to the children of the selection. It returns more concrete equalities, if the
127     * equality between the other node and the children of the selection is more concrete.
128     * </p>
129     *
130     * @param taskTreeNode  the first task tree node to compare
131     * @param taskTreeNode2 the second task tree node to compare
132     *
133     * @return as described
134     */
135    private NodeEquality selectionChildrenSemanticallyEqualNode(ITaskTreeNode taskTreeNode,
136                                                                ITaskTreeNode taskTreeNode2)
137    {
138        ISelection selection = null;
139        ITaskTreeNode node = null;
140        if (taskTreeNode instanceof ISelection) {
141            selection = (ISelection) taskTreeNode;
142            node = taskTreeNode2;
143        }
144        else if (taskTreeNode2 instanceof ISelection) {
145            selection = (ISelection) taskTreeNode2;
146            node = taskTreeNode;
147        }
148        else {
149            return NodeEquality.UNEQUAL;
150        }
151
152        NodeEquality lessConcreteEqualityForAllComparisons = NodeEquality.IDENTICAL;
153
154        for (ITaskTreeNode child : selection.getChildren()) {
155            NodeEquality nodeEquality = mRuleManager.applyRules(node, child);
156
157            if (!nodeEquality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)) {
158                return NodeEquality.UNEQUAL;
159            }
160            else if (!lessConcreteEqualityForAllComparisons.isAtLeast(nodeEquality)) {
161                lessConcreteEqualityForAllComparisons = nodeEquality;
162            }
163        }
164
165        return lessConcreteEqualityForAllComparisons;
166    }
167
168}
Note: See TracBrowser for help on using the repository browser.