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