1 | //------------------------------------------------------------------------------------------------- |
---|
2 | // Module : $RCSfile: TaskTreeImplTest.java,v $ |
---|
3 | // Version : $Revision: 0.0 $ $Author: patrick $ $Date: 02.04.2012 $ |
---|
4 | // Project : TaskTreeImpl |
---|
5 | // Creation : 2012 by patrick |
---|
6 | // Copyright : Patrick Harms, 2012 |
---|
7 | //------------------------------------------------------------------------------------------------- |
---|
8 | package de.ugoe.cs.quest.tasktrees.treeimpl; |
---|
9 | |
---|
10 | import static org.junit.Assert.assertEquals; |
---|
11 | import static org.junit.Assert.assertNotNull; |
---|
12 | import static org.junit.Assert.assertNull; |
---|
13 | |
---|
14 | import java.util.HashMap; |
---|
15 | import java.util.Map; |
---|
16 | |
---|
17 | import org.junit.Test; |
---|
18 | |
---|
19 | import de.ugoe.cs.quest.tasktrees.testutils.DummyGUIElement; |
---|
20 | import de.ugoe.cs.quest.tasktrees.testutils.DummyInteraction; |
---|
21 | import de.ugoe.cs.quest.tasktrees.treeifc.InteractionTask; |
---|
22 | import de.ugoe.cs.quest.tasktrees.treeifc.Iteration; |
---|
23 | import de.ugoe.cs.quest.tasktrees.treeifc.NodeInfo; |
---|
24 | import de.ugoe.cs.quest.tasktrees.treeifc.Selection; |
---|
25 | import de.ugoe.cs.quest.tasktrees.treeifc.Sequence; |
---|
26 | import de.ugoe.cs.quest.tasktrees.treeifc.TaskTree; |
---|
27 | import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeBuilder; |
---|
28 | import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeNode; |
---|
29 | import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeNodeFactory; |
---|
30 | import de.ugoe.cs.quest.tasktrees.treeimpl.NodeInfoImpl; |
---|
31 | import de.ugoe.cs.quest.tasktrees.treeimpl.TaskTreeBuilderImpl; |
---|
32 | import de.ugoe.cs.quest.tasktrees.treeimpl.TaskTreeNodeFactoryImpl; |
---|
33 | |
---|
34 | //------------------------------------------------------------------------------------------------- |
---|
35 | /** |
---|
36 | * TODO comment |
---|
37 | * |
---|
38 | * @version $Revision: $ $Date: 02.04.2012$ |
---|
39 | * @author 2012, last modified by $Author: patrick$ |
---|
40 | */ |
---|
41 | //------------------------------------------------------------------------------------------------- |
---|
42 | public class TaskTreeImplTest |
---|
43 | { |
---|
44 | /** */ |
---|
45 | private static final int MAX_TREE_DEPTH = 15; |
---|
46 | |
---|
47 | /** */ |
---|
48 | private TaskTreeBuilder mTaskTreeBuilder = new TaskTreeBuilderImpl(); |
---|
49 | |
---|
50 | /** */ |
---|
51 | private TaskTreeNodeFactory mTaskTreeNodeFactory = new TaskTreeNodeFactoryImpl(); |
---|
52 | |
---|
53 | //----------------------------------------------------------------------------------------------- |
---|
54 | /** |
---|
55 | * @throws Exception |
---|
56 | * |
---|
57 | */ |
---|
58 | //----------------------------------------------------------------------------------------------- |
---|
59 | @Test |
---|
60 | public void testRandomTrees() throws Exception |
---|
61 | { |
---|
62 | int noOfTrees = 10; |
---|
63 | int noOfMaxChildren = 8; |
---|
64 | int maxDepth = MAX_TREE_DEPTH; |
---|
65 | |
---|
66 | for (int i = 0; i < noOfTrees; i++) |
---|
67 | { |
---|
68 | System.err.println("iteration " + (i + 1) + ":"); |
---|
69 | System.err.println(" creating tree"); |
---|
70 | Map<TaskTreeNode, NodeInfo> treeInfos = new HashMap<TaskTreeNode, NodeInfo>(); |
---|
71 | TaskTreeNode rootNode = createTree(noOfMaxChildren, maxDepth, treeInfos); |
---|
72 | System.err.println(" creating task tree"); |
---|
73 | TaskTree taskTree = mTaskTreeNodeFactory.createTaskTree(rootNode); |
---|
74 | |
---|
75 | System.err.println(" validating task tree"); |
---|
76 | assertEquals(rootNode, taskTree.getRoot()); |
---|
77 | assertMapsEqual(treeInfos, taskTree.getTaskMap()); |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | //----------------------------------------------------------------------------------------------- |
---|
82 | /** |
---|
83 | * TODO: comment |
---|
84 | * |
---|
85 | * @param treeInfos |
---|
86 | * @param taskMap |
---|
87 | */ |
---|
88 | //----------------------------------------------------------------------------------------------- |
---|
89 | private void assertMapsEqual(Map<TaskTreeNode, NodeInfo> map1, |
---|
90 | Map<TaskTreeNode, NodeInfo> map2) |
---|
91 | { |
---|
92 | try |
---|
93 | { |
---|
94 | if (map1 == null) |
---|
95 | { |
---|
96 | assertNull(map2); |
---|
97 | return; |
---|
98 | } |
---|
99 | |
---|
100 | assertEquals(map1.size(), map2.size()); |
---|
101 | |
---|
102 | for (Map.Entry<TaskTreeNode, NodeInfo> entry : map1.entrySet()) |
---|
103 | { |
---|
104 | NodeInfo value2 = map2.get(entry.getKey()); |
---|
105 | assertNotNull(value2); |
---|
106 | assertEquals(entry.getValue().getTask(), value2.getTask()); |
---|
107 | assertEquals(entry.getValue().getNoOfOccurencesInTree(), value2.getNoOfOccurencesInTree()); |
---|
108 | } |
---|
109 | } |
---|
110 | catch (AssertionError e) |
---|
111 | { |
---|
112 | dumpMap(map1); |
---|
113 | dumpMap(map2); |
---|
114 | throw e; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | //----------------------------------------------------------------------------------------------- |
---|
119 | /** |
---|
120 | * TODO: comment |
---|
121 | * |
---|
122 | * @param map2 |
---|
123 | */ |
---|
124 | //----------------------------------------------------------------------------------------------- |
---|
125 | private void dumpMap(Map<TaskTreeNode, NodeInfo> map) |
---|
126 | { |
---|
127 | System.err.println(); |
---|
128 | |
---|
129 | if (map == null) |
---|
130 | { |
---|
131 | System.err.println("map is null"); |
---|
132 | } |
---|
133 | else |
---|
134 | { |
---|
135 | System.err.println("map:"); |
---|
136 | for (Map.Entry<TaskTreeNode, NodeInfo> entry : map.entrySet()) |
---|
137 | { |
---|
138 | System.err.print(" "); |
---|
139 | System.err.print(entry.getKey()); |
---|
140 | for (int i = entry.getKey().toString().length(); i < 49; i++) |
---|
141 | { |
---|
142 | System.err.print(" "); |
---|
143 | } |
---|
144 | System.err.print(" : "); |
---|
145 | System.err.println(entry.getValue()); |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | System.err.println(); |
---|
150 | } |
---|
151 | |
---|
152 | //----------------------------------------------------------------------------------------------- |
---|
153 | /** |
---|
154 | * TODO: comment |
---|
155 | * |
---|
156 | * @param noOfMaxChildren |
---|
157 | * @param maxDepth |
---|
158 | * @param treeInfos |
---|
159 | * @return |
---|
160 | */ |
---|
161 | //----------------------------------------------------------------------------------------------- |
---|
162 | private TaskTreeNode createTree(int maxNoOfChildren, |
---|
163 | int maxDepth, |
---|
164 | Map<TaskTreeNode, NodeInfo> treeInfos) |
---|
165 | throws Exception |
---|
166 | { |
---|
167 | /*for (int i = 0; i < (MAX_TREE_DEPTH + 1 - maxDepth); i++) |
---|
168 | { |
---|
169 | System.err.print(" "); |
---|
170 | }*/ |
---|
171 | |
---|
172 | TaskTreeNode tree; |
---|
173 | |
---|
174 | // integrating the maximum depth here assures, that either something between 0 and 8 will |
---|
175 | // be the type, or if the max depth decreases near 0 only interaction tasks will be created |
---|
176 | // to finish the tree creation |
---|
177 | int type = (int) (Math.random() * (Math.min(8, maxDepth))); |
---|
178 | |
---|
179 | switch (type) |
---|
180 | { |
---|
181 | case 0: |
---|
182 | { |
---|
183 | //System.err.print("creating new interaction task "); |
---|
184 | tree = createNewInteractionTask(treeInfos); |
---|
185 | break; |
---|
186 | } |
---|
187 | case 1: |
---|
188 | { |
---|
189 | //System.err.print("reusing interaction task "); |
---|
190 | tree = reuseInteractionTask(treeInfos); |
---|
191 | break; |
---|
192 | } |
---|
193 | case 2: |
---|
194 | { |
---|
195 | //System.err.println("creating new sequence {"); |
---|
196 | tree = createNewSequence(maxNoOfChildren, maxDepth, treeInfos); |
---|
197 | break; |
---|
198 | } |
---|
199 | case 3: |
---|
200 | { |
---|
201 | //System.err.println("reusing sequence {"); |
---|
202 | tree = reuseSequence(maxNoOfChildren, maxDepth, treeInfos); |
---|
203 | break; |
---|
204 | } |
---|
205 | case 4: |
---|
206 | { |
---|
207 | //System.err.println("creating new selection {"); |
---|
208 | tree = createNewSelection(maxNoOfChildren, maxDepth, treeInfos); |
---|
209 | break; |
---|
210 | } |
---|
211 | case 5: |
---|
212 | { |
---|
213 | //System.err.println("reusing selection {"); |
---|
214 | tree = reuseSelection(maxNoOfChildren, maxDepth, treeInfos); |
---|
215 | break; |
---|
216 | } |
---|
217 | case 6: |
---|
218 | { |
---|
219 | //System.err.println("creating new iteration {"); |
---|
220 | tree = createNewIteration(maxNoOfChildren, maxDepth, treeInfos); |
---|
221 | break; |
---|
222 | } |
---|
223 | case 7: |
---|
224 | { |
---|
225 | //System.err.println("reusing iteration {"); |
---|
226 | tree = reuseIteration(maxNoOfChildren, maxDepth, treeInfos); |
---|
227 | break; |
---|
228 | } |
---|
229 | default: |
---|
230 | { |
---|
231 | //System.err.print("creating new interaction task per default "); |
---|
232 | tree = createNewInteractionTask(treeInfos); |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | /* |
---|
237 | if (!(tree instanceof InteractionTask)) |
---|
238 | { |
---|
239 | for (int i = 0; i < (MAX_TREE_DEPTH + 1 - maxDepth); i++) |
---|
240 | { |
---|
241 | System.err.print(" "); |
---|
242 | } |
---|
243 | |
---|
244 | System.err.print("} "); |
---|
245 | } |
---|
246 | |
---|
247 | System.err.println(tree);*/ |
---|
248 | |
---|
249 | return tree; |
---|
250 | } |
---|
251 | |
---|
252 | //----------------------------------------------------------------------------------------------- |
---|
253 | /** |
---|
254 | * TODO: comment |
---|
255 | * |
---|
256 | * @param treeInfos |
---|
257 | * @return |
---|
258 | */ |
---|
259 | //----------------------------------------------------------------------------------------------- |
---|
260 | private InteractionTask createNewInteractionTask(Map<TaskTreeNode, NodeInfo> treeInfos) |
---|
261 | throws Exception |
---|
262 | { |
---|
263 | Thread.sleep(2); |
---|
264 | long id = System.currentTimeMillis(); |
---|
265 | InteractionTask task = mTaskTreeNodeFactory.createNewInteractionTask |
---|
266 | (new DummyGUIElement("elem" + id), new DummyInteraction("interaction" + id, 1)); |
---|
267 | |
---|
268 | treeInfos.put(task, new NodeInfoImpl(task)); |
---|
269 | |
---|
270 | return task; |
---|
271 | } |
---|
272 | |
---|
273 | //----------------------------------------------------------------------------------------------- |
---|
274 | /** |
---|
275 | * TODO: comment |
---|
276 | * |
---|
277 | * @param treeInfos |
---|
278 | * @return |
---|
279 | */ |
---|
280 | //----------------------------------------------------------------------------------------------- |
---|
281 | private InteractionTask reuseInteractionTask(Map<TaskTreeNode, NodeInfo> treeInfos) |
---|
282 | throws Exception |
---|
283 | { |
---|
284 | int noOfInteractionTasks = 0; |
---|
285 | |
---|
286 | for (Map.Entry<TaskTreeNode, NodeInfo> entry : treeInfos.entrySet()) |
---|
287 | { |
---|
288 | if (entry.getKey() instanceof InteractionTask) |
---|
289 | { |
---|
290 | noOfInteractionTasks++; |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | if (noOfInteractionTasks > 0) |
---|
295 | { |
---|
296 | noOfInteractionTasks = (int) (Math.random() * noOfInteractionTasks); |
---|
297 | |
---|
298 | for (Map.Entry<TaskTreeNode, NodeInfo> entry : treeInfos.entrySet()) |
---|
299 | { |
---|
300 | if (entry.getKey() instanceof InteractionTask) |
---|
301 | { |
---|
302 | if (--noOfInteractionTasks <= 0) |
---|
303 | { |
---|
304 | return (InteractionTask) entry.getKey(); |
---|
305 | } |
---|
306 | } |
---|
307 | } |
---|
308 | } |
---|
309 | else |
---|
310 | { |
---|
311 | return createNewInteractionTask(treeInfos); |
---|
312 | } |
---|
313 | |
---|
314 | throw new RuntimeException("this is an implementation error"); |
---|
315 | } |
---|
316 | |
---|
317 | //----------------------------------------------------------------------------------------------- |
---|
318 | /** |
---|
319 | * TODO: comment |
---|
320 | * |
---|
321 | * @param treeInfos |
---|
322 | * @return |
---|
323 | */ |
---|
324 | //----------------------------------------------------------------------------------------------- |
---|
325 | private Sequence createNewSequence(int maxNoOfChildren, |
---|
326 | int maxDepth, |
---|
327 | Map<TaskTreeNode, NodeInfo> treeInfos) |
---|
328 | throws Exception |
---|
329 | { |
---|
330 | Sequence sequence = mTaskTreeNodeFactory.createNewSequence(); |
---|
331 | |
---|
332 | int noOfChildren = (int) (Math.random() * maxNoOfChildren); |
---|
333 | |
---|
334 | for (int i = 0; i < noOfChildren; i++) |
---|
335 | { |
---|
336 | TaskTreeNode child = createTree(maxNoOfChildren, maxDepth - 1, treeInfos); |
---|
337 | |
---|
338 | // through first removing an existing parent it is assured, that a parent is recorded |
---|
339 | // only once. This is needed, because parent may be reused in a tree as well, but we always |
---|
340 | // iterate the whole tree |
---|
341 | ((NodeInfoImpl) treeInfos.get(child)).removeParent(sequence); |
---|
342 | ((NodeInfoImpl) treeInfos.get(child)).addParent(sequence); |
---|
343 | mTaskTreeBuilder.addChild(sequence, child); |
---|
344 | } |
---|
345 | |
---|
346 | treeInfos.put(sequence, new NodeInfoImpl(sequence)); |
---|
347 | return sequence; |
---|
348 | } |
---|
349 | |
---|
350 | //----------------------------------------------------------------------------------------------- |
---|
351 | /** |
---|
352 | * TODO: comment |
---|
353 | * |
---|
354 | * @param treeInfos |
---|
355 | * @return |
---|
356 | */ |
---|
357 | //----------------------------------------------------------------------------------------------- |
---|
358 | private Sequence reuseSequence(int maxNoOfChildren, |
---|
359 | int maxDepth, |
---|
360 | Map<TaskTreeNode, NodeInfo> treeInfos) |
---|
361 | throws Exception |
---|
362 | { |
---|
363 | int noOfSequences = 0; |
---|
364 | |
---|
365 | for (Map.Entry<TaskTreeNode, NodeInfo> entry : treeInfos.entrySet()) |
---|
366 | { |
---|
367 | if (entry.getKey() instanceof Sequence) |
---|
368 | { |
---|
369 | noOfSequences++; |
---|
370 | } |
---|
371 | } |
---|
372 | |
---|
373 | if (noOfSequences > 0) |
---|
374 | { |
---|
375 | noOfSequences = (int) (Math.random() * noOfSequences); |
---|
376 | |
---|
377 | for (Map.Entry<TaskTreeNode, NodeInfo> entry : treeInfos.entrySet()) |
---|
378 | { |
---|
379 | if (entry.getKey() instanceof Sequence) |
---|
380 | { |
---|
381 | if (--noOfSequences <= 0) |
---|
382 | { |
---|
383 | return (Sequence) entry.getKey(); |
---|
384 | } |
---|
385 | } |
---|
386 | } |
---|
387 | } |
---|
388 | else |
---|
389 | { |
---|
390 | return createNewSequence(maxNoOfChildren, maxDepth, treeInfos); |
---|
391 | } |
---|
392 | |
---|
393 | throw new RuntimeException("this is an implementation error"); |
---|
394 | } |
---|
395 | |
---|
396 | //----------------------------------------------------------------------------------------------- |
---|
397 | /** |
---|
398 | * TODO: comment |
---|
399 | * |
---|
400 | * @param treeInfos |
---|
401 | * @return |
---|
402 | */ |
---|
403 | //----------------------------------------------------------------------------------------------- |
---|
404 | private Selection createNewSelection(int maxNoOfChildren, |
---|
405 | int maxDepth, |
---|
406 | Map<TaskTreeNode, NodeInfo> treeInfos) |
---|
407 | throws Exception |
---|
408 | { |
---|
409 | Selection selection = mTaskTreeNodeFactory.createNewSelection(); |
---|
410 | |
---|
411 | int noOfChildren = (int) (Math.random() * maxNoOfChildren); |
---|
412 | |
---|
413 | for (int i = 0; i < noOfChildren; i++) |
---|
414 | { |
---|
415 | TaskTreeNode child = createTree(maxNoOfChildren, maxDepth - 1, treeInfos); |
---|
416 | |
---|
417 | // through first removing an existing parent it is assured, that a parent is recorded |
---|
418 | // only once. This is needed, because parent may be reused in a tree as well, but we always |
---|
419 | // iterate the whole tree |
---|
420 | ((NodeInfoImpl) treeInfos.get(child)).removeParent(selection); |
---|
421 | ((NodeInfoImpl) treeInfos.get(child)).addParent(selection); |
---|
422 | mTaskTreeBuilder.addChild(selection, child); |
---|
423 | } |
---|
424 | |
---|
425 | treeInfos.put(selection, new NodeInfoImpl(selection)); |
---|
426 | return selection; |
---|
427 | } |
---|
428 | |
---|
429 | //----------------------------------------------------------------------------------------------- |
---|
430 | /** |
---|
431 | * TODO: comment |
---|
432 | * |
---|
433 | * @param treeInfos |
---|
434 | * @return |
---|
435 | */ |
---|
436 | //----------------------------------------------------------------------------------------------- |
---|
437 | private Selection reuseSelection(int maxNoOfChildren, |
---|
438 | int maxDepth, |
---|
439 | Map<TaskTreeNode, NodeInfo> treeInfos) |
---|
440 | throws Exception |
---|
441 | { |
---|
442 | int noOfSelections = 0; |
---|
443 | |
---|
444 | for (Map.Entry<TaskTreeNode, NodeInfo> entry : treeInfos.entrySet()) |
---|
445 | { |
---|
446 | if (entry.getKey() instanceof Selection) |
---|
447 | { |
---|
448 | noOfSelections++; |
---|
449 | } |
---|
450 | } |
---|
451 | |
---|
452 | if (noOfSelections > 0) |
---|
453 | { |
---|
454 | noOfSelections = (int) (Math.random() * noOfSelections); |
---|
455 | |
---|
456 | for (Map.Entry<TaskTreeNode, NodeInfo> entry : treeInfos.entrySet()) |
---|
457 | { |
---|
458 | if (entry.getKey() instanceof Selection) |
---|
459 | { |
---|
460 | if (--noOfSelections <= 0) |
---|
461 | { |
---|
462 | return (Selection) entry.getKey(); |
---|
463 | } |
---|
464 | } |
---|
465 | } |
---|
466 | } |
---|
467 | else |
---|
468 | { |
---|
469 | return createNewSelection(maxNoOfChildren, maxDepth, treeInfos); |
---|
470 | } |
---|
471 | |
---|
472 | throw new RuntimeException("this is an implementation error"); |
---|
473 | } |
---|
474 | |
---|
475 | //----------------------------------------------------------------------------------------------- |
---|
476 | /** |
---|
477 | * TODO: comment |
---|
478 | * |
---|
479 | * @param treeInfos |
---|
480 | * @return |
---|
481 | */ |
---|
482 | //----------------------------------------------------------------------------------------------- |
---|
483 | private Iteration createNewIteration(int maxNoOfChildren, |
---|
484 | int maxDepth, |
---|
485 | Map<TaskTreeNode, NodeInfo> treeInfos) |
---|
486 | throws Exception |
---|
487 | { |
---|
488 | Iteration iteration = mTaskTreeNodeFactory.createNewIteration(); |
---|
489 | |
---|
490 | TaskTreeNode child = createTree(maxNoOfChildren, maxDepth - 1, treeInfos); |
---|
491 | |
---|
492 | // through first removing an existing parent it is assured, that a parent is recorded |
---|
493 | // only once. This is needed, because parent may be reused in a tree as well, but we always |
---|
494 | // iterate the whole tree |
---|
495 | ((NodeInfoImpl) treeInfos.get(child)).removeParent(iteration); |
---|
496 | ((NodeInfoImpl) treeInfos.get(child)).addParent(iteration); |
---|
497 | mTaskTreeBuilder.setChild(iteration, child); |
---|
498 | |
---|
499 | treeInfos.put(iteration, new NodeInfoImpl(iteration)); |
---|
500 | return iteration; |
---|
501 | } |
---|
502 | |
---|
503 | //----------------------------------------------------------------------------------------------- |
---|
504 | /** |
---|
505 | * TODO: comment |
---|
506 | * |
---|
507 | * @param treeInfos |
---|
508 | * @return |
---|
509 | */ |
---|
510 | //----------------------------------------------------------------------------------------------- |
---|
511 | private Iteration reuseIteration(int maxNoOfChildren, |
---|
512 | int maxDepth, |
---|
513 | Map<TaskTreeNode, NodeInfo> treeInfos) |
---|
514 | throws Exception |
---|
515 | { |
---|
516 | int noOfIterations = 0; |
---|
517 | |
---|
518 | for (Map.Entry<TaskTreeNode, NodeInfo> entry : treeInfos.entrySet()) |
---|
519 | { |
---|
520 | if (entry.getKey() instanceof Iteration) |
---|
521 | { |
---|
522 | noOfIterations++; |
---|
523 | } |
---|
524 | } |
---|
525 | |
---|
526 | if (noOfIterations > 0) |
---|
527 | { |
---|
528 | noOfIterations = (int) (Math.random() * noOfIterations); |
---|
529 | |
---|
530 | for (Map.Entry<TaskTreeNode, NodeInfo> entry : treeInfos.entrySet()) |
---|
531 | { |
---|
532 | if (entry.getKey() instanceof Iteration) |
---|
533 | { |
---|
534 | if (--noOfIterations <= 0) |
---|
535 | { |
---|
536 | return (Iteration) entry.getKey(); |
---|
537 | } |
---|
538 | } |
---|
539 | } |
---|
540 | } |
---|
541 | else |
---|
542 | { |
---|
543 | return createNewIteration(maxNoOfChildren, maxDepth, treeInfos); |
---|
544 | } |
---|
545 | |
---|
546 | throw new RuntimeException("this is an implementation error"); |
---|
547 | } |
---|
548 | |
---|
549 | } |
---|