1 | //------------------------------------------------------------------------------------------------- |
---|
2 | // Module : $RCSfile: NodeIdentityRule.java,v $ |
---|
3 | // Version : $Revision: 0.0 $ $Author: patrick $ $Date: 19.02.2012 $ |
---|
4 | // Project : TaskTreeCreator |
---|
5 | // Creation : 2012 by patrick |
---|
6 | // Copyright : Patrick Harms, 2012 |
---|
7 | //------------------------------------------------------------------------------------------------- |
---|
8 | package de.ugoe.cs.quest.tasktrees.nodeequality; |
---|
9 | |
---|
10 | import de.ugoe.cs.quest.tasktrees.treeifc.Iteration; |
---|
11 | import de.ugoe.cs.quest.tasktrees.treeifc.Selection; |
---|
12 | import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeNode; |
---|
13 | |
---|
14 | //------------------------------------------------------------------------------------------------- |
---|
15 | /** |
---|
16 | * TODO comment |
---|
17 | * |
---|
18 | * @version $Revision: $ $Date: 19.02.2012$ |
---|
19 | * @author 2012, last modified by $Author: patrick$ |
---|
20 | */ |
---|
21 | //------------------------------------------------------------------------------------------------- |
---|
22 | public class IterationComparisonRule implements NodeComparisonRule |
---|
23 | { |
---|
24 | /** */ |
---|
25 | private NodeEqualityRuleManager mRuleManager; |
---|
26 | |
---|
27 | //----------------------------------------------------------------------------------------------- |
---|
28 | /** |
---|
29 | * TODO: comment |
---|
30 | * |
---|
31 | */ |
---|
32 | //----------------------------------------------------------------------------------------------- |
---|
33 | IterationComparisonRule(NodeEqualityRuleManager ruleManager) |
---|
34 | { |
---|
35 | super(); |
---|
36 | mRuleManager = ruleManager; |
---|
37 | } |
---|
38 | |
---|
39 | //----------------------------------------------------------------------------------------------- |
---|
40 | /* (non-Javadoc) |
---|
41 | * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode) |
---|
42 | */ |
---|
43 | //----------------------------------------------------------------------------------------------- |
---|
44 | @Override |
---|
45 | public NodeEquality compare(TaskTreeNode node1, TaskTreeNode node2) |
---|
46 | { |
---|
47 | if ((!(node1 instanceof Iteration)) || (!(node2 instanceof Iteration))) |
---|
48 | { |
---|
49 | return null; |
---|
50 | } |
---|
51 | |
---|
52 | // if both iterations do not have children, they are equal although this doesn't make sense |
---|
53 | if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) |
---|
54 | { |
---|
55 | return new NodesIdentical(); |
---|
56 | } |
---|
57 | |
---|
58 | TaskTreeNode child1 = node1.getChildren().get(0); |
---|
59 | TaskTreeNode child2 = node2.getChildren().get(0); |
---|
60 | |
---|
61 | // iterations may have 3 different structures. |
---|
62 | // 1. they have one child, which is the iterated one |
---|
63 | // 2. they have a sequence of children, which is iterated |
---|
64 | // 3. they have a selection of different iterated variants (usually the variants are |
---|
65 | // semantically equal) |
---|
66 | // |
---|
67 | // the permutations of the three variants in combination must be checked |
---|
68 | |
---|
69 | // check if both nodes are the same variants of iterations and if their children are equal. |
---|
70 | // This condition matches, if both iterations are the same variants of iteration. I.e. three |
---|
71 | // combinations of the permutation are handled herewith. |
---|
72 | NodeEquality nodeEquality = mRuleManager.applyRules(child1, child2); |
---|
73 | |
---|
74 | if (nodeEquality.getStructuralEquality() || nodeEquality.getSemanticalEquality()) |
---|
75 | { |
---|
76 | return nodeEquality; |
---|
77 | } |
---|
78 | |
---|
79 | // compare one iteration with a single node as a child and another one with a selection of |
---|
80 | // semantically equal nodes |
---|
81 | if (selectionChildrenSemanticallyEqualNode(child1, child2)) |
---|
82 | { |
---|
83 | return new NodesSemanticallyEqual(); |
---|
84 | } |
---|
85 | |
---|
86 | // all other combinations (i.e. sequence with single child and sequence with selection) |
---|
87 | // can not match |
---|
88 | return null; |
---|
89 | } |
---|
90 | |
---|
91 | //----------------------------------------------------------------------------------------------- |
---|
92 | /** |
---|
93 | * TODO: comment |
---|
94 | * |
---|
95 | * @param taskTreeNode |
---|
96 | * @param taskTreeNode2 |
---|
97 | * @return |
---|
98 | */ |
---|
99 | //----------------------------------------------------------------------------------------------- |
---|
100 | private boolean selectionChildrenSemanticallyEqualNode(TaskTreeNode taskTreeNode, |
---|
101 | TaskTreeNode taskTreeNode2) |
---|
102 | { |
---|
103 | Selection selection = null; |
---|
104 | TaskTreeNode node = null; |
---|
105 | if (taskTreeNode instanceof Selection) |
---|
106 | { |
---|
107 | selection = (Selection) taskTreeNode; |
---|
108 | node = taskTreeNode2; |
---|
109 | } |
---|
110 | else if (taskTreeNode2 instanceof Selection) |
---|
111 | { |
---|
112 | selection = (Selection) taskTreeNode2; |
---|
113 | node = taskTreeNode; |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | return false; |
---|
118 | } |
---|
119 | |
---|
120 | for (TaskTreeNode child : selection.getChildren()) |
---|
121 | { |
---|
122 | NodeEquality nodeEquality = mRuleManager.applyRules(node, child); |
---|
123 | |
---|
124 | if (!nodeEquality.getStructuralEquality() && !nodeEquality.getSemanticalEquality()) |
---|
125 | { |
---|
126 | return false; |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | return true; |
---|
131 | } |
---|
132 | |
---|
133 | } |
---|