1 | package de.ugoe.cs.quest.tasktrees.temporalrelation; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.List; |
---|
5 | |
---|
6 | import de.ugoe.cs.quest.tasktrees.nodeequality.NodeEquality; |
---|
7 | import de.ugoe.cs.quest.tasktrees.nodeequality.NodeEqualityRuleManager; |
---|
8 | import de.ugoe.cs.quest.tasktrees.treeifc.IIteration; |
---|
9 | import de.ugoe.cs.quest.tasktrees.treeifc.ISelection; |
---|
10 | import de.ugoe.cs.quest.tasktrees.treeifc.ISequence; |
---|
11 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeBuilder; |
---|
12 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode; |
---|
13 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNodeFactory; |
---|
14 | |
---|
15 | /** |
---|
16 | * TODO comment |
---|
17 | * |
---|
18 | * @version $Revision: $ $Date: 19.02.2012$ |
---|
19 | * @author 2012, last modified by $Author: patrick$ |
---|
20 | */ |
---|
21 | public class DefaultIterationDetectionRule implements TemporalRelationshipRule { |
---|
22 | |
---|
23 | /** */ |
---|
24 | private NodeEqualityRuleManager nodeEqualityRuleManager; |
---|
25 | |
---|
26 | /** |
---|
27 | * TODO: comment |
---|
28 | * |
---|
29 | */ |
---|
30 | DefaultIterationDetectionRule(NodeEqualityRuleManager nodeEqualityRuleManager) { |
---|
31 | super(); |
---|
32 | this.nodeEqualityRuleManager = nodeEqualityRuleManager; |
---|
33 | } |
---|
34 | |
---|
35 | /* |
---|
36 | * (non-Javadoc) |
---|
37 | * |
---|
38 | * @see TemporalRelationshipRule#apply(TaskTreeNode, TaskTreeBuilder, TaskTreeNodeFactory) |
---|
39 | */ |
---|
40 | @Override |
---|
41 | public RuleApplicationResult apply(ITaskTreeNode parent, |
---|
42 | ITaskTreeBuilder treeBuilder, |
---|
43 | ITaskTreeNodeFactory nodeFactory, |
---|
44 | boolean finalize) |
---|
45 | { |
---|
46 | if (!(parent instanceof ISequence)) { |
---|
47 | return null; |
---|
48 | } |
---|
49 | |
---|
50 | // parent must already have at least 2 children |
---|
51 | if ((parent.getChildren() == null) || (parent.getChildren().size() < 2)) { |
---|
52 | return null; |
---|
53 | } |
---|
54 | |
---|
55 | // iterations represent as a list of nodes that splits up in several equal sublists. If |
---|
56 | // the remaining nodes also start an equal sublist, then the iteration may not be completed |
---|
57 | // yet. So wait for further events to only identify completed iterations. |
---|
58 | |
---|
59 | // to find longer iterations first, start with long sequences |
---|
60 | for (int end = parent.getChildren().size() - 1; end > 0; end--) { |
---|
61 | for (int start = 0; start < end; start++) { |
---|
62 | List<ITaskTreeNode[]> equalVariants = |
---|
63 | getEqualSublistVariantsInBoundaries(parent, start, end); |
---|
64 | |
---|
65 | if (equalVariants != null) { |
---|
66 | if (!finalize) { |
---|
67 | // check, if the iteration may go on. This may be the case, if the detected |
---|
68 | // iteration finishes with the last child of the parent, or if the |
---|
69 | // remaining children, which were not identified as part of the iteration, |
---|
70 | // start a further occurrence of the iteration |
---|
71 | if (end == (parent.getChildren().size() - 1)) { |
---|
72 | RuleApplicationResult result = new RuleApplicationResult(); |
---|
73 | result.setRuleApplicationStatus |
---|
74 | (RuleApplicationStatus.RULE_APPLICATION_FEASIBLE); |
---|
75 | return result; |
---|
76 | } |
---|
77 | |
---|
78 | boolean allNodesEqual = true; |
---|
79 | for (int i = 0; ((allNodesEqual) && (i < equalVariants.get(0).length)); i++) |
---|
80 | { |
---|
81 | if ((end + i + 1) >= parent.getChildren().size()) { |
---|
82 | break; |
---|
83 | } |
---|
84 | |
---|
85 | NodeEquality nodeEquality = nodeEqualityRuleManager.applyRules |
---|
86 | (equalVariants.get(0)[i], parent.getChildren().get(end + i + 1)); |
---|
87 | |
---|
88 | allNodesEqual &= |
---|
89 | nodeEquality.isAtLeast(NodeEquality.SYNTACTICALLY_EQUAL); |
---|
90 | } |
---|
91 | |
---|
92 | if (allNodesEqual) { |
---|
93 | RuleApplicationResult result = new RuleApplicationResult(); |
---|
94 | result.setRuleApplicationStatus |
---|
95 | (RuleApplicationStatus.RULE_APPLICATION_FEASIBLE); |
---|
96 | return result; |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | RuleApplicationResult result = new RuleApplicationResult(); |
---|
101 | IIteration newIteration = nodeFactory.createNewIteration(); |
---|
102 | result.addNewlyCreatedParentNode(newIteration); |
---|
103 | |
---|
104 | if (equalVariants.size() == 1) { |
---|
105 | // all children are the same. Create an iteration of this child |
---|
106 | if (equalVariants.get(0).length == 1) { |
---|
107 | // all children are the same. Create an iteration of this child |
---|
108 | treeBuilder.setChild(newIteration, equalVariants.get(0)[0]); |
---|
109 | } |
---|
110 | else { |
---|
111 | // there was an iteration of structurally equal sequences |
---|
112 | ISequence sequence = nodeFactory.createNewSequence(); |
---|
113 | result.addNewlyCreatedParentNode(sequence); |
---|
114 | |
---|
115 | for (ITaskTreeNode node : equalVariants.get(0)) { |
---|
116 | treeBuilder.addChild(sequence, node); |
---|
117 | } |
---|
118 | |
---|
119 | treeBuilder.setChild(newIteration, sequence); |
---|
120 | } |
---|
121 | } |
---|
122 | else { |
---|
123 | // there are distinct variants of semantically equal subsequences or |
---|
124 | // children --> |
---|
125 | // create an iterated selection |
---|
126 | ISelection selection = nodeFactory.createNewSelection(); |
---|
127 | result.addNewlyCreatedParentNode(selection); |
---|
128 | |
---|
129 | for (ITaskTreeNode[] variant : equalVariants) { |
---|
130 | if (variant.length == 1) { |
---|
131 | treeBuilder.addChild(selection, variant[0]); |
---|
132 | } |
---|
133 | else { |
---|
134 | ISequence sequence = nodeFactory.createNewSequence(); |
---|
135 | result.addNewlyCreatedParentNode(sequence); |
---|
136 | |
---|
137 | for (ITaskTreeNode node : variant) { |
---|
138 | treeBuilder.addChild(sequence, node); |
---|
139 | } |
---|
140 | |
---|
141 | treeBuilder.addChild(selection, sequence); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | treeBuilder.setChild(newIteration, selection); |
---|
146 | } |
---|
147 | |
---|
148 | // remove iterated children |
---|
149 | for (int j = end; j >= start; j--) { |
---|
150 | treeBuilder.removeChild((ISequence) parent, j); |
---|
151 | } |
---|
152 | |
---|
153 | // add the new iteration instead |
---|
154 | treeBuilder.addChild((ISequence) parent, start, newIteration); |
---|
155 | |
---|
156 | result.setRuleApplicationStatus |
---|
157 | (RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
158 | return result; |
---|
159 | } |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | return null; |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * TODO: comment |
---|
168 | * |
---|
169 | * @return |
---|
170 | */ |
---|
171 | private List<ITaskTreeNode[]> getEqualSublistVariantsInBoundaries(ITaskTreeNode parent, |
---|
172 | int start, |
---|
173 | int end) |
---|
174 | { |
---|
175 | List<ITaskTreeNode[]> equalVariants = null; |
---|
176 | |
---|
177 | int noOfChildrenInBoundaries = end - start + 1; |
---|
178 | |
---|
179 | for (int subListLen = 1; subListLen <= (noOfChildrenInBoundaries / 2); subListLen++) |
---|
180 | { |
---|
181 | if ((noOfChildrenInBoundaries % subListLen) == 0) { |
---|
182 | equalVariants = |
---|
183 | getEqualSublistVariantsForSubListLength(parent, start, end, subListLen); |
---|
184 | |
---|
185 | if (equalVariants != null) { |
---|
186 | return equalVariants; |
---|
187 | } |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | return null; |
---|
192 | } |
---|
193 | |
---|
194 | /** |
---|
195 | * |
---|
196 | */ |
---|
197 | private List<ITaskTreeNode[]> getEqualSublistVariantsForSubListLength(ITaskTreeNode parent, |
---|
198 | int start, |
---|
199 | int end, |
---|
200 | int subListLen) |
---|
201 | { |
---|
202 | List<ITaskTreeNode[]> equalVariants = new ArrayList<ITaskTreeNode[]>(); |
---|
203 | ITaskTreeNode[] firstVariant = new ITaskTreeNode[subListLen]; |
---|
204 | |
---|
205 | for (int i = 0; i < subListLen; i++) { |
---|
206 | firstVariant[i] = parent.getChildren().get(start + i); |
---|
207 | } |
---|
208 | |
---|
209 | equalVariants.add(firstVariant); |
---|
210 | |
---|
211 | for (int parentIdx = (start + subListLen); parentIdx <= end; parentIdx += subListLen) |
---|
212 | { |
---|
213 | ITaskTreeNode[] otherVariant = new ITaskTreeNode[subListLen]; |
---|
214 | |
---|
215 | for (int i = 0; i < subListLen; i++) { |
---|
216 | NodeEquality nodeEquality = nodeEqualityRuleManager.applyRules |
---|
217 | (firstVariant[i], parent.getChildren().get(parentIdx + i)); |
---|
218 | |
---|
219 | if (!nodeEquality.isAtLeast(NodeEquality.LEXICALLY_EQUAL)) { |
---|
220 | if (nodeEquality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)) { |
---|
221 | otherVariant[i] = parent.getChildren().get(parentIdx + i); |
---|
222 | } |
---|
223 | else { |
---|
224 | return null; |
---|
225 | } |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | // check, if there is a semantically equal other variant. If so, add it to the list of |
---|
230 | // variants |
---|
231 | boolean semanticallyUnequal = false; |
---|
232 | for (int i = 0; i < subListLen; i++) { |
---|
233 | if (otherVariant[i] == null) { |
---|
234 | otherVariant[i] = firstVariant[i]; |
---|
235 | } |
---|
236 | else { |
---|
237 | semanticallyUnequal = true; |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | if (semanticallyUnequal) { |
---|
242 | equalVariants.add(otherVariant); |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | return equalVariants; |
---|
247 | } |
---|
248 | |
---|
249 | } |
---|