1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.tasktrees.treeimpl; |
---|
16 | |
---|
17 | import static org.junit.Assert.*; |
---|
18 | |
---|
19 | import java.util.HashMap; |
---|
20 | import java.util.LinkedList; |
---|
21 | import java.util.List; |
---|
22 | import java.util.Map; |
---|
23 | |
---|
24 | import org.junit.Test; |
---|
25 | |
---|
26 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
27 | import de.ugoe.cs.autoquest.eventcore.IEventTarget; |
---|
28 | import de.ugoe.cs.autoquest.eventcore.IEventType; |
---|
29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
30 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance; |
---|
31 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
32 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance; |
---|
33 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
34 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance; |
---|
35 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
36 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; |
---|
37 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
38 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance; |
---|
39 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
40 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
41 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel; |
---|
42 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder; |
---|
43 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory; |
---|
44 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo; |
---|
45 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
46 | import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskInfo; |
---|
47 | import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskBuilder; |
---|
48 | import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskFactory; |
---|
49 | import de.ugoe.cs.autoquest.test.DummyGUIElement; |
---|
50 | import de.ugoe.cs.autoquest.test.DummyInteraction; |
---|
51 | |
---|
52 | /** |
---|
53 | * |
---|
54 | */ |
---|
55 | public class TaskModelTest { |
---|
56 | |
---|
57 | /** */ |
---|
58 | private static final int MAX_TREE_DEPTH = 15; |
---|
59 | |
---|
60 | /** */ |
---|
61 | private ITaskBuilder taskBuilder = new TaskBuilder(); |
---|
62 | |
---|
63 | /** */ |
---|
64 | private ITaskFactory taskFactory = new TaskFactory(); |
---|
65 | |
---|
66 | /** |
---|
67 | * |
---|
68 | */ |
---|
69 | @Test |
---|
70 | public void test_EventTask_01() throws Exception { |
---|
71 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
72 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
73 | |
---|
74 | IEventTask task = createNewEventTask(eventType, eventTarget); |
---|
75 | |
---|
76 | assertNotNull(task); |
---|
77 | assertNotNull(task.getDescription()); |
---|
78 | assertNotNull(task.getId()); |
---|
79 | assertTrue(task.equals(task)); |
---|
80 | |
---|
81 | IEventTaskInstance instance = (IEventTaskInstance) task.getInstances().iterator().next(); |
---|
82 | assertEquals(eventType, instance.getEvent().getType()); |
---|
83 | assertEquals(eventTarget, instance.getEvent().getTarget()); |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * |
---|
88 | */ |
---|
89 | @Test |
---|
90 | public void test_EventTask_02() throws Exception { |
---|
91 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
92 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
93 | |
---|
94 | IEventTask task1 = createNewEventTask(eventType, eventTarget); |
---|
95 | IEventTask task2 = createNewEventTask(eventType, eventTarget); |
---|
96 | |
---|
97 | // the tasks will not be equal as they should have a different id |
---|
98 | assertFalse(task1.equals(task2)); |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | * |
---|
103 | */ |
---|
104 | @Test |
---|
105 | public void test_EventTask_03() throws Exception { |
---|
106 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
107 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
108 | |
---|
109 | IEventTask task = createNewEventTask(eventType, eventTarget); |
---|
110 | ITaskInstance taskInstance1 = createNewTaskInstance(task); |
---|
111 | ITaskInstance taskInstance2 = createNewTaskInstance(task); |
---|
112 | |
---|
113 | assertFalse(taskInstance1.equals(taskInstance2)); |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * |
---|
118 | */ |
---|
119 | @Test |
---|
120 | public void test_Sequence_01() throws Exception { |
---|
121 | ISequence task = taskFactory.createNewSequence(); |
---|
122 | |
---|
123 | assertNotNull(task); |
---|
124 | assertNotNull(task.getDescription()); |
---|
125 | assertNotNull(task.getId()); |
---|
126 | assertNotNull(task.getChildren()); |
---|
127 | assertEquals(0, task.getChildren().size()); |
---|
128 | assertTrue(task.equals(task)); |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * |
---|
133 | */ |
---|
134 | @Test |
---|
135 | public void test_Sequence_02() throws Exception { |
---|
136 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
137 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
138 | |
---|
139 | IEventTask child = createNewEventTask(eventType, eventTarget); |
---|
140 | |
---|
141 | ISequence task = taskFactory.createNewSequence(); |
---|
142 | |
---|
143 | taskBuilder.addChild(task, child); |
---|
144 | |
---|
145 | assertNotNull(task.getChildren()); |
---|
146 | assertEquals(1, task.getChildren().size()); |
---|
147 | assertEquals(child, task.getChildren().get(0)); |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * |
---|
152 | */ |
---|
153 | @Test |
---|
154 | public void test_Sequence_03() throws Exception { |
---|
155 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
156 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
157 | |
---|
158 | IEventTask child1 = createNewEventTask(eventType, eventTarget); |
---|
159 | IEventTask child2 = createNewEventTask(eventType, eventTarget); |
---|
160 | IEventTask child3 = createNewEventTask(eventType, eventTarget); |
---|
161 | IEventTask child4 = createNewEventTask(eventType, eventTarget); |
---|
162 | IEventTask child5 = createNewEventTask(eventType, eventTarget); |
---|
163 | |
---|
164 | ISequence task = taskFactory.createNewSequence(); |
---|
165 | |
---|
166 | taskBuilder.addChild(task, child1); |
---|
167 | taskBuilder.addChild(task, child2); |
---|
168 | taskBuilder.addChild(task, child3); |
---|
169 | taskBuilder.addChild(task, child4); |
---|
170 | taskBuilder.addChild(task, child5); |
---|
171 | |
---|
172 | assertNotNull(task.getChildren()); |
---|
173 | assertEquals(5, task.getChildren().size()); |
---|
174 | assertEquals(child1, task.getChildren().get(0)); |
---|
175 | assertEquals(child2, task.getChildren().get(1)); |
---|
176 | assertEquals(child3, task.getChildren().get(2)); |
---|
177 | assertEquals(child4, task.getChildren().get(3)); |
---|
178 | assertEquals(child5, task.getChildren().get(4)); |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * |
---|
183 | */ |
---|
184 | @Test |
---|
185 | public void test_Selection_01() throws Exception { |
---|
186 | ISelection task = taskFactory.createNewSelection(); |
---|
187 | |
---|
188 | assertNotNull(task); |
---|
189 | assertNotNull(task.getDescription()); |
---|
190 | assertNotNull(task.getId()); |
---|
191 | assertNotNull(task.getChildren()); |
---|
192 | assertEquals(0, task.getChildren().size()); |
---|
193 | assertTrue(task.equals(task)); |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * |
---|
198 | */ |
---|
199 | @Test |
---|
200 | public void test_Selection_02() throws Exception { |
---|
201 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
202 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
203 | |
---|
204 | IEventTask child = createNewEventTask(eventType, eventTarget); |
---|
205 | |
---|
206 | ISelection task = taskFactory.createNewSelection(); |
---|
207 | |
---|
208 | taskBuilder.addChild(task, child); |
---|
209 | |
---|
210 | assertNotNull(task.getChildren()); |
---|
211 | assertEquals(1, task.getChildren().size()); |
---|
212 | assertEquals(child, task.getChildren().get(0)); |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | * |
---|
217 | */ |
---|
218 | @Test |
---|
219 | public void test_Selection_03() throws Exception { |
---|
220 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
221 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
222 | |
---|
223 | IEventTask child1 = createNewEventTask(eventType, eventTarget); |
---|
224 | IEventTask child2 = createNewEventTask(eventType, eventTarget); |
---|
225 | IEventTask child3 = createNewEventTask(eventType, eventTarget); |
---|
226 | IEventTask child4 = createNewEventTask(eventType, eventTarget); |
---|
227 | IEventTask child5 = createNewEventTask(eventType, eventTarget); |
---|
228 | |
---|
229 | ISelection task = taskFactory.createNewSelection(); |
---|
230 | |
---|
231 | taskBuilder.addChild(task, child1); |
---|
232 | taskBuilder.addChild(task, child2); |
---|
233 | taskBuilder.addChild(task, child3); |
---|
234 | taskBuilder.addChild(task, child4); |
---|
235 | taskBuilder.addChild(task, child5); |
---|
236 | |
---|
237 | assertNotNull(task.getChildren()); |
---|
238 | assertEquals(5, task.getChildren().size()); |
---|
239 | assertEquals(child1, task.getChildren().get(0)); |
---|
240 | assertEquals(child2, task.getChildren().get(1)); |
---|
241 | assertEquals(child3, task.getChildren().get(2)); |
---|
242 | assertEquals(child4, task.getChildren().get(3)); |
---|
243 | assertEquals(child5, task.getChildren().get(4)); |
---|
244 | } |
---|
245 | |
---|
246 | /** |
---|
247 | * |
---|
248 | */ |
---|
249 | @Test |
---|
250 | public void test_Iteration_01() throws Exception { |
---|
251 | IIteration task = taskFactory.createNewIteration(); |
---|
252 | |
---|
253 | assertNotNull(task); |
---|
254 | assertNotNull(task.getDescription()); |
---|
255 | assertNotNull(task.getId()); |
---|
256 | assertNull(task.getMarkedTask()); |
---|
257 | assertTrue(task.equals(task)); |
---|
258 | } |
---|
259 | |
---|
260 | /** |
---|
261 | * |
---|
262 | */ |
---|
263 | @Test |
---|
264 | public void test_Iteration_02() throws Exception { |
---|
265 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
266 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
267 | |
---|
268 | IEventTask child = createNewEventTask(eventType, eventTarget); |
---|
269 | |
---|
270 | IIteration task = taskFactory.createNewIteration(); |
---|
271 | |
---|
272 | taskBuilder.setMarkedTask(task, child); |
---|
273 | |
---|
274 | assertEquals(child, task.getMarkedTask()); |
---|
275 | } |
---|
276 | |
---|
277 | /** |
---|
278 | * |
---|
279 | */ |
---|
280 | @Test |
---|
281 | public void test_Iteration_03() throws Exception { |
---|
282 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
283 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
284 | |
---|
285 | IEventTask child1 = createNewEventTask(eventType, eventTarget); |
---|
286 | IEventTask child2 = createNewEventTask(eventType, eventTarget); |
---|
287 | |
---|
288 | IIteration task = taskFactory.createNewIteration(); |
---|
289 | |
---|
290 | taskBuilder.setMarkedTask(task, child1); |
---|
291 | taskBuilder.setMarkedTask(task, child2); |
---|
292 | |
---|
293 | assertEquals(child2, task.getMarkedTask()); |
---|
294 | } |
---|
295 | |
---|
296 | /** |
---|
297 | * |
---|
298 | */ |
---|
299 | @Test |
---|
300 | public void test_Optional_01() throws Exception { |
---|
301 | IOptional task = taskFactory.createNewOptional(); |
---|
302 | |
---|
303 | assertNotNull(task); |
---|
304 | assertNotNull(task.getDescription()); |
---|
305 | assertNotNull(task.getId()); |
---|
306 | assertNull(task.getMarkedTask()); |
---|
307 | assertTrue(task.equals(task)); |
---|
308 | } |
---|
309 | |
---|
310 | /** |
---|
311 | * |
---|
312 | */ |
---|
313 | @Test |
---|
314 | public void test_Optional_02() throws Exception { |
---|
315 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
316 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
317 | |
---|
318 | IEventTask child = createNewEventTask(eventType, eventTarget); |
---|
319 | |
---|
320 | IOptional task = taskFactory.createNewOptional(); |
---|
321 | |
---|
322 | taskBuilder.setMarkedTask(task, child); |
---|
323 | |
---|
324 | assertEquals(child, task.getMarkedTask()); |
---|
325 | } |
---|
326 | |
---|
327 | /** |
---|
328 | * |
---|
329 | */ |
---|
330 | @Test |
---|
331 | public void test_Optional_03() throws Exception { |
---|
332 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
333 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
334 | |
---|
335 | IEventTask child1 = createNewEventTask(eventType, eventTarget); |
---|
336 | IEventTask child2 = createNewEventTask(eventType, eventTarget); |
---|
337 | |
---|
338 | IOptional task = taskFactory.createNewOptional(); |
---|
339 | |
---|
340 | taskBuilder.setMarkedTask(task, child1); |
---|
341 | taskBuilder.setMarkedTask(task, child2); |
---|
342 | |
---|
343 | assertEquals(child2, task.getMarkedTask()); |
---|
344 | } |
---|
345 | |
---|
346 | /** |
---|
347 | * |
---|
348 | */ |
---|
349 | @Test |
---|
350 | public void test_EventTaskInstance_01() throws Exception { |
---|
351 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
352 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
353 | |
---|
354 | IEventTask task = createNewEventTask(eventType, eventTarget); |
---|
355 | |
---|
356 | IEventTaskInstance taskInstance = createNewTaskInstance(task); |
---|
357 | |
---|
358 | assertNotNull(taskInstance); |
---|
359 | assertEquals(task, taskInstance.getTask()); |
---|
360 | assertTrue(taskInstance.equals(taskInstance)); |
---|
361 | assertFalse(taskInstance.equals(task)); |
---|
362 | } |
---|
363 | |
---|
364 | /** |
---|
365 | * |
---|
366 | */ |
---|
367 | @Test |
---|
368 | public void test_EventTaskInstance_02() throws Exception { |
---|
369 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
370 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
371 | |
---|
372 | IEventTask task = createNewEventTask(eventType, eventTarget); |
---|
373 | |
---|
374 | ITaskInstance taskInstance1 = createNewTaskInstance(task); |
---|
375 | ITaskInstance taskInstance2 = createNewTaskInstance(task); |
---|
376 | |
---|
377 | assertFalse(taskInstance1.equals(taskInstance2)); |
---|
378 | } |
---|
379 | |
---|
380 | /** |
---|
381 | * |
---|
382 | */ |
---|
383 | @Test(expected=IllegalArgumentException.class) |
---|
384 | public void test_SequenceInstance_01() throws Exception { |
---|
385 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
386 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
387 | |
---|
388 | IEventTask task = createNewEventTask(eventType, eventTarget); |
---|
389 | |
---|
390 | ISequence sequence = taskFactory.createNewSequence(); |
---|
391 | |
---|
392 | ITaskInstance taskInstance = createNewTaskInstance(task); |
---|
393 | ISequenceInstance sequenceInstance = createNewTaskInstance(sequence); |
---|
394 | |
---|
395 | taskBuilder.addChild(sequenceInstance, taskInstance); |
---|
396 | } |
---|
397 | |
---|
398 | /** |
---|
399 | * |
---|
400 | */ |
---|
401 | @Test |
---|
402 | public void test_SequenceInstance_02() throws Exception { |
---|
403 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
404 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
405 | |
---|
406 | IEventTask task = createNewEventTask(eventType, eventTarget); |
---|
407 | |
---|
408 | ISequence sequence = taskFactory.createNewSequence(); |
---|
409 | taskBuilder.addChild(sequence, task); |
---|
410 | |
---|
411 | ITaskInstance taskInstance = createNewTaskInstance(task); |
---|
412 | ISequenceInstance sequenceInstance = createNewTaskInstance(sequence); |
---|
413 | |
---|
414 | taskBuilder.addChild(sequenceInstance, taskInstance); |
---|
415 | |
---|
416 | assertEquals(1, sequenceInstance.size()); |
---|
417 | assertEquals(taskInstance, sequenceInstance.get(0)); |
---|
418 | } |
---|
419 | |
---|
420 | /** |
---|
421 | * |
---|
422 | */ |
---|
423 | @Test |
---|
424 | public void test_SequenceInstance_03() throws Exception { |
---|
425 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
426 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
427 | |
---|
428 | IEventTask task1 = createNewEventTask(eventType, eventTarget); |
---|
429 | IEventTask task2 = createNewEventTask(eventType, eventTarget); |
---|
430 | IEventTask task3 = createNewEventTask(eventType, eventTarget); |
---|
431 | IEventTask task4 = createNewEventTask(eventType, eventTarget); |
---|
432 | IEventTask task5 = createNewEventTask(eventType, eventTarget); |
---|
433 | |
---|
434 | ISequence sequence = taskFactory.createNewSequence(); |
---|
435 | taskBuilder.addChild(sequence, task1); |
---|
436 | taskBuilder.addChild(sequence, task2); |
---|
437 | taskBuilder.addChild(sequence, task3); |
---|
438 | taskBuilder.addChild(sequence, task4); |
---|
439 | taskBuilder.addChild(sequence, task5); |
---|
440 | |
---|
441 | ITaskInstance taskInstance1 = createNewTaskInstance(task1); |
---|
442 | ITaskInstance taskInstance2 = createNewTaskInstance(task2); |
---|
443 | ITaskInstance taskInstance3 = createNewTaskInstance(task3); |
---|
444 | ITaskInstance taskInstance4 = createNewTaskInstance(task4); |
---|
445 | ITaskInstance taskInstance5 = createNewTaskInstance(task5); |
---|
446 | ISequenceInstance sequenceInstance = createNewTaskInstance(sequence); |
---|
447 | |
---|
448 | taskBuilder.addChild(sequenceInstance, taskInstance1); |
---|
449 | taskBuilder.addChild(sequenceInstance, taskInstance2); |
---|
450 | taskBuilder.addChild(sequenceInstance, taskInstance3); |
---|
451 | taskBuilder.addChild(sequenceInstance, taskInstance4); |
---|
452 | taskBuilder.addChild(sequenceInstance, taskInstance5); |
---|
453 | |
---|
454 | assertEquals(5, sequenceInstance.size()); |
---|
455 | assertEquals(taskInstance1, sequenceInstance.get(0)); |
---|
456 | assertEquals(taskInstance2, sequenceInstance.get(1)); |
---|
457 | assertEquals(taskInstance3, sequenceInstance.get(2)); |
---|
458 | assertEquals(taskInstance4, sequenceInstance.get(3)); |
---|
459 | assertEquals(taskInstance5, sequenceInstance.get(4)); |
---|
460 | } |
---|
461 | |
---|
462 | /** |
---|
463 | * |
---|
464 | */ |
---|
465 | @Test(expected=IllegalArgumentException.class) |
---|
466 | public void test_SelectionInstance_01() throws Exception { |
---|
467 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
468 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
469 | |
---|
470 | IEventTask task = createNewEventTask(eventType, eventTarget); |
---|
471 | |
---|
472 | ISelection selection = taskFactory.createNewSelection(); |
---|
473 | |
---|
474 | ITaskInstance taskInstance = createNewTaskInstance(task); |
---|
475 | ISelectionInstance selectionInstance = createNewTaskInstance(selection); |
---|
476 | |
---|
477 | taskBuilder.setChild(selectionInstance, taskInstance); |
---|
478 | } |
---|
479 | |
---|
480 | /** |
---|
481 | * |
---|
482 | */ |
---|
483 | @Test |
---|
484 | public void test_SelectionInstance_02() throws Exception { |
---|
485 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
486 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
487 | |
---|
488 | IEventTask task = createNewEventTask(eventType, eventTarget); |
---|
489 | |
---|
490 | ISelection selection = taskFactory.createNewSelection(); |
---|
491 | taskBuilder.addChild(selection, task); |
---|
492 | |
---|
493 | ITaskInstance taskInstance = createNewTaskInstance(task); |
---|
494 | ISelectionInstance selectionInstance = createNewTaskInstance(selection); |
---|
495 | |
---|
496 | taskBuilder.setChild(selectionInstance, taskInstance); |
---|
497 | |
---|
498 | assertNotNull(selectionInstance.getChild()); |
---|
499 | assertEquals(taskInstance, selectionInstance.getChild()); |
---|
500 | } |
---|
501 | |
---|
502 | /** |
---|
503 | * |
---|
504 | */ |
---|
505 | @Test |
---|
506 | public void test_SelectionInstance_03() throws Exception { |
---|
507 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
508 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
509 | |
---|
510 | IEventTask task1 = createNewEventTask(eventType, eventTarget); |
---|
511 | IEventTask task2 = createNewEventTask(eventType, eventTarget); |
---|
512 | IEventTask task3 = createNewEventTask(eventType, eventTarget); |
---|
513 | IEventTask task4 = createNewEventTask(eventType, eventTarget); |
---|
514 | IEventTask task5 = createNewEventTask(eventType, eventTarget); |
---|
515 | |
---|
516 | ISelection selection = taskFactory.createNewSelection(); |
---|
517 | taskBuilder.addChild(selection, task1); |
---|
518 | taskBuilder.addChild(selection, task2); |
---|
519 | taskBuilder.addChild(selection, task3); |
---|
520 | taskBuilder.addChild(selection, task4); |
---|
521 | taskBuilder.addChild(selection, task5); |
---|
522 | |
---|
523 | ITaskInstance taskInstance1 = createNewTaskInstance(task1); |
---|
524 | ITaskInstance taskInstance2 = createNewTaskInstance(task2); |
---|
525 | ISelectionInstance selectionInstance = createNewTaskInstance(selection); |
---|
526 | |
---|
527 | taskBuilder.setChild(selectionInstance, taskInstance1); |
---|
528 | taskBuilder.setChild(selectionInstance, taskInstance2); |
---|
529 | |
---|
530 | assertEquals(taskInstance2, selectionInstance.getChild()); |
---|
531 | } |
---|
532 | |
---|
533 | /** |
---|
534 | * |
---|
535 | */ |
---|
536 | @Test(expected=IllegalArgumentException.class) |
---|
537 | public void test_IterationInstance_01() throws Exception { |
---|
538 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
539 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
540 | |
---|
541 | IEventTask task = createNewEventTask(eventType, eventTarget); |
---|
542 | |
---|
543 | IIteration iteration = taskFactory.createNewIteration(); |
---|
544 | |
---|
545 | ITaskInstance taskInstance = createNewTaskInstance(task); |
---|
546 | IIterationInstance iterationInstance = createNewTaskInstance(iteration); |
---|
547 | |
---|
548 | taskBuilder.addChild(iterationInstance, taskInstance); |
---|
549 | } |
---|
550 | |
---|
551 | /** |
---|
552 | * |
---|
553 | */ |
---|
554 | @Test |
---|
555 | public void test_IterationInstance_02() throws Exception { |
---|
556 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
557 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
558 | |
---|
559 | IEventTask task = createNewEventTask(eventType, eventTarget); |
---|
560 | |
---|
561 | IIteration iteration = taskFactory.createNewIteration(); |
---|
562 | taskBuilder.setMarkedTask(iteration, task); |
---|
563 | |
---|
564 | ITaskInstance taskInstance = createNewTaskInstance(task); |
---|
565 | IIterationInstance iterationInstance = createNewTaskInstance(iteration); |
---|
566 | |
---|
567 | taskBuilder.addChild(iterationInstance, taskInstance); |
---|
568 | |
---|
569 | assertEquals(1, iterationInstance.size()); |
---|
570 | assertEquals(taskInstance, iterationInstance.get(0)); |
---|
571 | } |
---|
572 | |
---|
573 | /** |
---|
574 | * |
---|
575 | */ |
---|
576 | @Test(expected=IllegalArgumentException.class) |
---|
577 | public void test_IterationInstance_03() throws Exception { |
---|
578 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
579 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
580 | |
---|
581 | IEventTask task1 = createNewEventTask(eventType, eventTarget); |
---|
582 | IEventTask task2 = createNewEventTask(eventType, eventTarget); |
---|
583 | |
---|
584 | IIteration iteration = taskFactory.createNewIteration(); |
---|
585 | taskBuilder.setMarkedTask(iteration, task1); |
---|
586 | taskBuilder.setMarkedTask(iteration, task2); |
---|
587 | |
---|
588 | ITaskInstance taskInstance1 = createNewTaskInstance(task1); |
---|
589 | IIterationInstance iterationInstance = createNewTaskInstance(iteration); |
---|
590 | |
---|
591 | taskBuilder.addChild(iterationInstance, taskInstance1); |
---|
592 | } |
---|
593 | |
---|
594 | /** |
---|
595 | * |
---|
596 | */ |
---|
597 | @Test |
---|
598 | public void test_IterationInstance_04() throws Exception { |
---|
599 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
600 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
601 | |
---|
602 | IEventTask task1 = createNewEventTask(eventType, eventTarget); |
---|
603 | IEventTask task2 = createNewEventTask(eventType, eventTarget); |
---|
604 | |
---|
605 | IIteration iteration = taskFactory.createNewIteration(); |
---|
606 | taskBuilder.setMarkedTask(iteration, task1); |
---|
607 | taskBuilder.setMarkedTask(iteration, task2); |
---|
608 | |
---|
609 | ITaskInstance taskInstance2 = createNewTaskInstance(task2); |
---|
610 | IIterationInstance iterationInstance = createNewTaskInstance(iteration); |
---|
611 | |
---|
612 | taskBuilder.addChild(iterationInstance, taskInstance2); |
---|
613 | |
---|
614 | assertEquals(1, iterationInstance.size()); |
---|
615 | assertEquals(taskInstance2, iterationInstance.get(0)); |
---|
616 | } |
---|
617 | |
---|
618 | /** |
---|
619 | * |
---|
620 | */ |
---|
621 | @Test(expected=IllegalArgumentException.class) |
---|
622 | public void test_OptionalInstance_01() throws Exception { |
---|
623 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
624 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
625 | |
---|
626 | IEventTask task = createNewEventTask(eventType, eventTarget); |
---|
627 | |
---|
628 | IOptional optional = taskFactory.createNewOptional(); |
---|
629 | |
---|
630 | ITaskInstance taskInstance = createNewTaskInstance(task); |
---|
631 | IOptionalInstance optionalInstance = createNewTaskInstance(optional); |
---|
632 | |
---|
633 | taskBuilder.setChild(optionalInstance, taskInstance); |
---|
634 | } |
---|
635 | |
---|
636 | /** |
---|
637 | * |
---|
638 | */ |
---|
639 | @Test |
---|
640 | public void test_OptionalInstance_02() throws Exception { |
---|
641 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
642 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
643 | |
---|
644 | IEventTask task = createNewEventTask(eventType, eventTarget); |
---|
645 | |
---|
646 | IOptional optional = taskFactory.createNewOptional(); |
---|
647 | taskBuilder.setMarkedTask(optional, task); |
---|
648 | |
---|
649 | ITaskInstance taskInstance = createNewTaskInstance(task); |
---|
650 | IOptionalInstance optionalInstance = createNewTaskInstance(optional); |
---|
651 | |
---|
652 | taskBuilder.setChild(optionalInstance, taskInstance); |
---|
653 | |
---|
654 | assertNotNull(optionalInstance.getChild()); |
---|
655 | assertEquals(taskInstance, optionalInstance.getChild()); |
---|
656 | } |
---|
657 | |
---|
658 | /** |
---|
659 | * |
---|
660 | */ |
---|
661 | @Test(expected=IllegalArgumentException.class) |
---|
662 | public void test_OptionalInstance_03() throws Exception { |
---|
663 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
664 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
665 | |
---|
666 | IEventTask task1 = createNewEventTask(eventType, eventTarget); |
---|
667 | IEventTask task2 = createNewEventTask(eventType, eventTarget); |
---|
668 | |
---|
669 | IOptional optional = taskFactory.createNewOptional(); |
---|
670 | taskBuilder.setMarkedTask(optional, task1); |
---|
671 | taskBuilder.setMarkedTask(optional, task2); |
---|
672 | |
---|
673 | ITaskInstance taskInstance1 = createNewTaskInstance(task1); |
---|
674 | IOptionalInstance optionalInstance = createNewTaskInstance(optional); |
---|
675 | |
---|
676 | taskBuilder.setChild(optionalInstance, taskInstance1); |
---|
677 | } |
---|
678 | |
---|
679 | /** |
---|
680 | * |
---|
681 | */ |
---|
682 | @Test |
---|
683 | public void test_OptionalInstance_04() throws Exception { |
---|
684 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
685 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
686 | |
---|
687 | IEventTask task1 = createNewEventTask(eventType, eventTarget); |
---|
688 | IEventTask task2 = createNewEventTask(eventType, eventTarget); |
---|
689 | |
---|
690 | IOptional optional = taskFactory.createNewOptional(); |
---|
691 | taskBuilder.setMarkedTask(optional, task1); |
---|
692 | taskBuilder.setMarkedTask(optional, task2); |
---|
693 | |
---|
694 | ITaskInstance taskInstance2 = createNewTaskInstance(task2); |
---|
695 | IOptionalInstance optionalInstance = createNewTaskInstance(optional); |
---|
696 | |
---|
697 | taskBuilder.setChild(optionalInstance, taskInstance2); |
---|
698 | |
---|
699 | assertNotNull(optionalInstance.getChild()); |
---|
700 | assertEquals(taskInstance2, optionalInstance.getChild()); |
---|
701 | } |
---|
702 | |
---|
703 | /** |
---|
704 | * |
---|
705 | */ |
---|
706 | @Test |
---|
707 | public void test_UserSession_01() throws Exception { |
---|
708 | IUserSession userSession = taskFactory.createUserSession(); |
---|
709 | |
---|
710 | assertNotNull(userSession); |
---|
711 | assertNotNull(userSession.getExecutedTasks()); |
---|
712 | assertEquals(0, userSession.getExecutedTasks().size()); |
---|
713 | } |
---|
714 | |
---|
715 | /** |
---|
716 | * |
---|
717 | */ |
---|
718 | @Test |
---|
719 | public void test_UserSession_02() throws Exception { |
---|
720 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
721 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
722 | |
---|
723 | IEventTask task = createNewEventTask(eventType, eventTarget); |
---|
724 | |
---|
725 | IUserSession userSession = taskFactory.createUserSession(); |
---|
726 | |
---|
727 | ITaskInstance taskInstance = createNewTaskInstance(task); |
---|
728 | |
---|
729 | taskBuilder.addExecutedTask(userSession, taskInstance); |
---|
730 | |
---|
731 | assertNotNull(userSession.getExecutedTasks()); |
---|
732 | assertEquals(1, userSession.getExecutedTasks().size()); |
---|
733 | assertEquals(taskInstance, userSession.getExecutedTasks().get(0)); |
---|
734 | } |
---|
735 | |
---|
736 | /** |
---|
737 | * |
---|
738 | */ |
---|
739 | @Test |
---|
740 | public void test_UserSession_03() throws Exception { |
---|
741 | IEventType eventType = new DummyInteraction("interaction", 1); |
---|
742 | IEventTarget eventTarget = new DummyGUIElement("elem"); |
---|
743 | |
---|
744 | IEventTask task = createNewEventTask(eventType, eventTarget); |
---|
745 | ISequence sequence = taskFactory.createNewSequence(); |
---|
746 | ISelection selection = taskFactory.createNewSelection(); |
---|
747 | IIteration iteration = taskFactory.createNewIteration(); |
---|
748 | IOptional optional = taskFactory.createNewOptional(); |
---|
749 | |
---|
750 | taskBuilder.addChild(sequence, task); |
---|
751 | taskBuilder.addChild(selection, task); |
---|
752 | taskBuilder.setMarkedTask(iteration, task); |
---|
753 | taskBuilder.setMarkedTask(optional, task); |
---|
754 | |
---|
755 | ITaskInstance taskInstance = createNewTaskInstance(task); |
---|
756 | ISequenceInstance sequenceInstance = createNewTaskInstance(sequence); |
---|
757 | ISelectionInstance selectionInstance = createNewTaskInstance(selection); |
---|
758 | IIterationInstance iterationInstance = createNewTaskInstance(iteration); |
---|
759 | IOptionalInstance optionalInstance = createNewTaskInstance(optional); |
---|
760 | |
---|
761 | taskBuilder.addChild(sequenceInstance, createNewTaskInstance(task)); |
---|
762 | taskBuilder.setChild(selectionInstance, createNewTaskInstance(task)); |
---|
763 | taskBuilder.addChild(iterationInstance, createNewTaskInstance(task)); |
---|
764 | taskBuilder.setChild(optionalInstance, createNewTaskInstance(task)); |
---|
765 | |
---|
766 | IUserSession userSession = taskFactory.createUserSession(); |
---|
767 | |
---|
768 | taskBuilder.addExecutedTask(userSession, taskInstance); |
---|
769 | taskBuilder.addExecutedTask(userSession, sequenceInstance); |
---|
770 | taskBuilder.addExecutedTask(userSession, selectionInstance); |
---|
771 | taskBuilder.addExecutedTask(userSession, iterationInstance); |
---|
772 | taskBuilder.addExecutedTask(userSession, optionalInstance); |
---|
773 | |
---|
774 | assertNotNull(userSession.getExecutedTasks()); |
---|
775 | assertEquals(5, userSession.getExecutedTasks().size()); |
---|
776 | assertEquals(taskInstance, userSession.getExecutedTasks().get(0)); |
---|
777 | assertEquals(sequenceInstance, userSession.getExecutedTasks().get(1)); |
---|
778 | assertEquals(selectionInstance, userSession.getExecutedTasks().get(2)); |
---|
779 | assertEquals(iterationInstance, userSession.getExecutedTasks().get(3)); |
---|
780 | assertEquals(optionalInstance, userSession.getExecutedTasks().get(4)); |
---|
781 | } |
---|
782 | |
---|
783 | /** |
---|
784 | * |
---|
785 | */ |
---|
786 | @Test |
---|
787 | public void testRandomTrees() throws Exception { |
---|
788 | int noOfTrees = 10; |
---|
789 | int noOfMaxChildren = 8; |
---|
790 | int maxDepth = MAX_TREE_DEPTH; |
---|
791 | |
---|
792 | for (int i = 0; i < noOfTrees; i++) { |
---|
793 | System.err.println("\niteration " + (i + 1) + ":"); |
---|
794 | System.err.println(" creating tasks"); |
---|
795 | Map<ITask, ITaskInfo> expectedTaskInfos = new HashMap<ITask, ITaskInfo>(); |
---|
796 | ITask task = createTaskTree(noOfMaxChildren, maxDepth, expectedTaskInfos); |
---|
797 | if (!(task instanceof ISequence)) { |
---|
798 | ISequence sequence = taskFactory.createNewSequence(); |
---|
799 | taskBuilder.addChild(sequence, task); |
---|
800 | task = sequence; |
---|
801 | } |
---|
802 | else { |
---|
803 | expectedTaskInfos.remove(task); |
---|
804 | } |
---|
805 | |
---|
806 | ISequenceInstance sequenceInstance = |
---|
807 | (ISequenceInstance) instantiateTask(task, noOfMaxChildren); |
---|
808 | |
---|
809 | System.err.println(" creating user session"); |
---|
810 | |
---|
811 | IUserSession session = taskFactory.createUserSession(); |
---|
812 | |
---|
813 | for (ITaskInstance child : sequenceInstance) { |
---|
814 | taskBuilder.addExecutedTask(session, child); |
---|
815 | } |
---|
816 | |
---|
817 | List<IUserSession> sessions = new LinkedList<IUserSession>(); |
---|
818 | sessions.add(session); |
---|
819 | |
---|
820 | ITaskModel taskModel = taskFactory.createTaskModel(sessions); |
---|
821 | |
---|
822 | System.err.println(" validating task tree"); |
---|
823 | Map<ITask, ITaskInfo> actualTaskInfos = new HashMap<ITask, ITaskInfo>(); |
---|
824 | |
---|
825 | for (ITask currentTask : taskModel.getTasks()) { |
---|
826 | actualTaskInfos.put(currentTask, taskModel.getTaskInfo(currentTask)); |
---|
827 | } |
---|
828 | |
---|
829 | assertMapsEqual(expectedTaskInfos, actualTaskInfos); |
---|
830 | } |
---|
831 | } |
---|
832 | |
---|
833 | /** |
---|
834 | * |
---|
835 | */ |
---|
836 | private void assertMapsEqual(Map<ITask, ITaskInfo> map1, |
---|
837 | Map<ITask, ITaskInfo> map2) |
---|
838 | { |
---|
839 | try { |
---|
840 | if (map1 == null) { |
---|
841 | assertNull(map2); |
---|
842 | return; |
---|
843 | } |
---|
844 | |
---|
845 | assertEquals(map1.size(), map2.size()); |
---|
846 | |
---|
847 | for (Map.Entry<ITask, ITaskInfo> entry : map1.entrySet()) { |
---|
848 | ITaskInfo value2 = map2.get(entry.getKey()); |
---|
849 | assertNotNull(value2); |
---|
850 | assertEquals(entry.getValue().getTask(), value2.getTask()); |
---|
851 | //assertEquals(entry.getValue().getNoOfOccurencesInTree(), |
---|
852 | // value2.getNoOfOccurencesInTree()); |
---|
853 | } |
---|
854 | } |
---|
855 | catch (AssertionError e) { |
---|
856 | dumpMap(map1); |
---|
857 | dumpMap(map2); |
---|
858 | throw e; |
---|
859 | } |
---|
860 | } |
---|
861 | |
---|
862 | /** |
---|
863 | * |
---|
864 | */ |
---|
865 | private void dumpMap(Map<ITask, ITaskInfo> map) { |
---|
866 | System.err.println(); |
---|
867 | |
---|
868 | if (map == null) { |
---|
869 | System.err.println("map is null"); |
---|
870 | } |
---|
871 | else { |
---|
872 | System.err.println("map:"); |
---|
873 | for (Map.Entry<ITask, ITaskInfo> entry : map.entrySet()) { |
---|
874 | System.err.print(" "); |
---|
875 | System.err.print(entry.getKey()); |
---|
876 | for (int i = entry.getKey().toString().length(); i < 60; i++) { |
---|
877 | System.err.print(" "); |
---|
878 | } |
---|
879 | System.err.print(" : "); |
---|
880 | System.err.println(entry.getValue()); |
---|
881 | } |
---|
882 | } |
---|
883 | |
---|
884 | System.err.println(); |
---|
885 | } |
---|
886 | |
---|
887 | /** |
---|
888 | * |
---|
889 | */ |
---|
890 | private ITask createTaskTree(int maxNoOfChildren, |
---|
891 | int maxDepth, |
---|
892 | Map<ITask, ITaskInfo> taskInfos) |
---|
893 | throws Exception |
---|
894 | { |
---|
895 | |
---|
896 | ITask task; |
---|
897 | |
---|
898 | // integrating the maximum depth here assures, that either something between 0 and 8 will |
---|
899 | // be the type, or if the max depth decreases near 0 only event tasks will be created |
---|
900 | // to finish the tree creation |
---|
901 | int type = randomize(Math.min(10, maxDepth)); |
---|
902 | |
---|
903 | switch (type) |
---|
904 | { |
---|
905 | case 0: { |
---|
906 | // System.err.print("creating new event task "); |
---|
907 | task = createNewEventTask(taskInfos); |
---|
908 | break; |
---|
909 | } |
---|
910 | case 1: { |
---|
911 | // System.err.print("reusing event task "); |
---|
912 | task = reuseEventTask(taskInfos); |
---|
913 | break; |
---|
914 | } |
---|
915 | case 2: { |
---|
916 | // System.err.println("creating new sequence {"); |
---|
917 | task = createNewSequence(maxNoOfChildren, maxDepth, taskInfos); |
---|
918 | break; |
---|
919 | } |
---|
920 | case 3: { |
---|
921 | // System.err.println("reusing sequence {"); |
---|
922 | task = reuseSequence(maxNoOfChildren, maxDepth, taskInfos); |
---|
923 | break; |
---|
924 | } |
---|
925 | case 4: { |
---|
926 | // System.err.println("creating new selection {"); |
---|
927 | task = createNewSelection(maxNoOfChildren, maxDepth, taskInfos); |
---|
928 | break; |
---|
929 | } |
---|
930 | case 5: { |
---|
931 | // System.err.println("reusing selection {"); |
---|
932 | task = reuseSelection(maxNoOfChildren, maxDepth, taskInfos); |
---|
933 | break; |
---|
934 | } |
---|
935 | case 6: { |
---|
936 | // System.err.println("creating new iteration {"); |
---|
937 | task = createNewIteration(maxNoOfChildren, maxDepth, taskInfos); |
---|
938 | break; |
---|
939 | } |
---|
940 | case 7: { |
---|
941 | // System.err.println("reusing iteration {"); |
---|
942 | task = reuseIteration(maxNoOfChildren, maxDepth, taskInfos); |
---|
943 | break; |
---|
944 | } |
---|
945 | case 8: { |
---|
946 | // System.err.println("creating new optional {"); |
---|
947 | task = createNewOptional(maxNoOfChildren, maxDepth, taskInfos); |
---|
948 | break; |
---|
949 | } |
---|
950 | case 9: { |
---|
951 | // System.err.println("reusing optional {"); |
---|
952 | task = reuseOptional(maxNoOfChildren, maxDepth, taskInfos); |
---|
953 | break; |
---|
954 | } |
---|
955 | default: { |
---|
956 | // System.err.print("creating new event task per default "); |
---|
957 | task = createNewEventTask(taskInfos); |
---|
958 | } |
---|
959 | } |
---|
960 | |
---|
961 | return task; |
---|
962 | } |
---|
963 | |
---|
964 | /** |
---|
965 | * |
---|
966 | */ |
---|
967 | private ITask createNewEventTask(Map<ITask, ITaskInfo> taskInfos) |
---|
968 | throws Exception |
---|
969 | { |
---|
970 | Thread.sleep(2); |
---|
971 | long id = System.currentTimeMillis(); |
---|
972 | IEventTask task = createNewEventTask |
---|
973 | (new DummyInteraction("interaction" + id, 1), new DummyGUIElement("elem" + id)); |
---|
974 | |
---|
975 | taskInfos.put(task, new TaskInfo(task)); |
---|
976 | |
---|
977 | return task; |
---|
978 | } |
---|
979 | |
---|
980 | /** |
---|
981 | * |
---|
982 | */ |
---|
983 | private ITask reuseEventTask(Map<ITask, ITaskInfo> taskInfos) |
---|
984 | throws Exception |
---|
985 | { |
---|
986 | ITask eventTask = reuseTask(taskInfos, IEventTask.class); |
---|
987 | |
---|
988 | if (eventTask == null) { |
---|
989 | eventTask = createNewEventTask(taskInfos); |
---|
990 | } |
---|
991 | |
---|
992 | return eventTask; |
---|
993 | } |
---|
994 | |
---|
995 | /** |
---|
996 | * |
---|
997 | */ |
---|
998 | private ITask createNewSequence(int maxNoOfChildren, |
---|
999 | int maxDepth, |
---|
1000 | Map<ITask, ITaskInfo> taskInfos) |
---|
1001 | throws Exception |
---|
1002 | { |
---|
1003 | ISequence sequence = taskFactory.createNewSequence(); |
---|
1004 | |
---|
1005 | // ensure at the minimum 2 children |
---|
1006 | int noOfChildren = randomize(2, maxNoOfChildren); |
---|
1007 | |
---|
1008 | for (int i = 0; i < noOfChildren; i++) { |
---|
1009 | ITask child = createTaskTree(maxNoOfChildren, maxDepth - 1, taskInfos); |
---|
1010 | taskBuilder.addChild(sequence, child); |
---|
1011 | } |
---|
1012 | |
---|
1013 | taskInfos.put(sequence, new TaskInfo(sequence)); |
---|
1014 | return sequence; |
---|
1015 | } |
---|
1016 | |
---|
1017 | /** |
---|
1018 | * |
---|
1019 | */ |
---|
1020 | private ITask reuseSequence(int maxNoOfChildren, |
---|
1021 | int maxDepth, |
---|
1022 | Map<ITask, ITaskInfo> taskInfos) |
---|
1023 | throws Exception |
---|
1024 | { |
---|
1025 | ITask sequence = reuseTask(taskInfos, ISequence.class); |
---|
1026 | |
---|
1027 | if (sequence == null) { |
---|
1028 | sequence = createNewSequence(maxNoOfChildren, maxDepth, taskInfos); |
---|
1029 | } |
---|
1030 | |
---|
1031 | return sequence; |
---|
1032 | } |
---|
1033 | |
---|
1034 | /** |
---|
1035 | * |
---|
1036 | */ |
---|
1037 | private ITask createNewSelection(int maxNoOfChildren, |
---|
1038 | int maxDepth, |
---|
1039 | Map<ITask, ITaskInfo> taskInfos) |
---|
1040 | throws Exception |
---|
1041 | { |
---|
1042 | ISelection selection = taskFactory.createNewSelection(); |
---|
1043 | |
---|
1044 | // ensure at the minimum 1 child |
---|
1045 | int noOfChildren = randomize(1, maxNoOfChildren); |
---|
1046 | |
---|
1047 | for (int i = 0; i < noOfChildren; i++) { |
---|
1048 | ITask child; |
---|
1049 | do { |
---|
1050 | child = createTaskTree(maxNoOfChildren, maxDepth - 1, taskInfos); |
---|
1051 | } |
---|
1052 | while (child instanceof ISelection); |
---|
1053 | |
---|
1054 | taskBuilder.addChild(selection, child); |
---|
1055 | } |
---|
1056 | |
---|
1057 | taskInfos.put(selection, new TaskInfo(selection)); |
---|
1058 | return selection; |
---|
1059 | } |
---|
1060 | |
---|
1061 | /** |
---|
1062 | * |
---|
1063 | */ |
---|
1064 | private ITask reuseSelection(int maxNoOfChildren, |
---|
1065 | int maxDepth, |
---|
1066 | Map<ITask, ITaskInfo> taskInfos) |
---|
1067 | throws Exception |
---|
1068 | { |
---|
1069 | ITask selection = reuseTask(taskInfos, ISelection.class); |
---|
1070 | |
---|
1071 | if (selection == null) { |
---|
1072 | selection = createNewSelection(maxNoOfChildren, maxDepth, taskInfos); |
---|
1073 | } |
---|
1074 | |
---|
1075 | return selection; |
---|
1076 | } |
---|
1077 | |
---|
1078 | /** |
---|
1079 | * |
---|
1080 | */ |
---|
1081 | private ITask createNewIteration(int maxNoOfChildren, |
---|
1082 | int maxDepth, |
---|
1083 | Map<ITask, ITaskInfo> taskInfos) |
---|
1084 | throws Exception |
---|
1085 | { |
---|
1086 | IIteration iteration = taskFactory.createNewIteration(); |
---|
1087 | |
---|
1088 | ITask child; |
---|
1089 | do { |
---|
1090 | child = createTaskTree(maxNoOfChildren, maxDepth - 1, taskInfos); |
---|
1091 | } |
---|
1092 | while ((child instanceof IOptional) || (child instanceof IIteration)); |
---|
1093 | |
---|
1094 | taskBuilder.setMarkedTask(iteration, child); |
---|
1095 | |
---|
1096 | taskInfos.put(iteration, new TaskInfo(iteration)); |
---|
1097 | return iteration; |
---|
1098 | } |
---|
1099 | |
---|
1100 | /** |
---|
1101 | * |
---|
1102 | */ |
---|
1103 | private ITask reuseIteration(int maxNoOfChildren, |
---|
1104 | int maxDepth, |
---|
1105 | Map<ITask, ITaskInfo> taskInfos) |
---|
1106 | throws Exception |
---|
1107 | { |
---|
1108 | ITask iteration = reuseTask(taskInfos, IIteration.class); |
---|
1109 | |
---|
1110 | if (iteration == null) { |
---|
1111 | iteration = createNewIteration(maxNoOfChildren, maxDepth, taskInfos); |
---|
1112 | } |
---|
1113 | |
---|
1114 | return iteration; |
---|
1115 | } |
---|
1116 | |
---|
1117 | /** |
---|
1118 | * |
---|
1119 | */ |
---|
1120 | private ITask createNewOptional(int maxNoOfChildren, |
---|
1121 | int maxDepth, |
---|
1122 | Map<ITask, ITaskInfo> taskInfos) |
---|
1123 | throws Exception |
---|
1124 | { |
---|
1125 | IOptional optional = taskFactory.createNewOptional(); |
---|
1126 | |
---|
1127 | ITask child; |
---|
1128 | do { |
---|
1129 | child = createTaskTree(maxNoOfChildren, maxDepth - 1, taskInfos); |
---|
1130 | } |
---|
1131 | while (child instanceof IOptional); |
---|
1132 | |
---|
1133 | taskBuilder.setMarkedTask(optional, child); |
---|
1134 | |
---|
1135 | taskInfos.put(optional, new TaskInfo(optional)); |
---|
1136 | return optional; |
---|
1137 | } |
---|
1138 | |
---|
1139 | /** |
---|
1140 | * |
---|
1141 | */ |
---|
1142 | private ITask reuseOptional(int maxNoOfChildren, |
---|
1143 | int maxDepth, |
---|
1144 | Map<ITask, ITaskInfo> taskInfos) |
---|
1145 | throws Exception |
---|
1146 | { |
---|
1147 | ITask optional = reuseTask(taskInfos, IOptional.class); |
---|
1148 | |
---|
1149 | if (optional == null) { |
---|
1150 | optional = createNewOptional(maxNoOfChildren, maxDepth, taskInfos); |
---|
1151 | } |
---|
1152 | |
---|
1153 | return optional; |
---|
1154 | } |
---|
1155 | |
---|
1156 | /** |
---|
1157 | * |
---|
1158 | */ |
---|
1159 | private ITask reuseTask(Map<ITask, ITaskInfo> taskInfos, Class<? extends ITask> type) |
---|
1160 | throws Exception |
---|
1161 | { |
---|
1162 | int noOfTasks = 0; |
---|
1163 | |
---|
1164 | for (Map.Entry<ITask, ITaskInfo> entry : taskInfos.entrySet()) { |
---|
1165 | if (type.isInstance(entry.getKey())) { |
---|
1166 | noOfTasks++; |
---|
1167 | } |
---|
1168 | } |
---|
1169 | |
---|
1170 | if (noOfTasks > 0) { |
---|
1171 | noOfTasks = randomize(noOfTasks); |
---|
1172 | |
---|
1173 | for (Map.Entry<ITask, ITaskInfo> entry : taskInfos.entrySet()) { |
---|
1174 | if (type.isInstance(entry.getKey())) { |
---|
1175 | if (--noOfTasks <= 0) { |
---|
1176 | return entry.getKey(); |
---|
1177 | } |
---|
1178 | } |
---|
1179 | } |
---|
1180 | } |
---|
1181 | else { |
---|
1182 | return null; |
---|
1183 | } |
---|
1184 | |
---|
1185 | throw new RuntimeException("this is an implementation error"); |
---|
1186 | } |
---|
1187 | |
---|
1188 | /** |
---|
1189 | * |
---|
1190 | */ |
---|
1191 | private ITaskInstance instantiateTask(ITask task, int maxIterationCount) throws Exception { |
---|
1192 | ITaskInstance instance = createNewTaskInstance(task); |
---|
1193 | |
---|
1194 | if (task instanceof ISequence) { |
---|
1195 | for (ITask child : ((ISequence) task).getChildren()) { |
---|
1196 | taskBuilder.addChild |
---|
1197 | ((ISequenceInstance) instance, instantiateTask(child, maxIterationCount)); |
---|
1198 | } |
---|
1199 | } |
---|
1200 | else if (task instanceof ISelection) { |
---|
1201 | List<ITask> children = ((ISelection) task).getChildren(); |
---|
1202 | int index = randomize(children.size()); |
---|
1203 | taskBuilder.setChild((ISelectionInstance) instance, |
---|
1204 | instantiateTask(children.get(index),maxIterationCount)); |
---|
1205 | } |
---|
1206 | else if (task instanceof IIteration) { |
---|
1207 | int count = randomize(maxIterationCount); |
---|
1208 | ITask child = ((IIteration) task).getMarkedTask(); |
---|
1209 | |
---|
1210 | for (int i = 0; i < count; i++) { |
---|
1211 | taskBuilder.addChild |
---|
1212 | ((IIterationInstance) instance, instantiateTask(child, maxIterationCount)); |
---|
1213 | } |
---|
1214 | } |
---|
1215 | else if (task instanceof IOptional) { |
---|
1216 | ITask child = ((IOptional) task).getMarkedTask(); |
---|
1217 | |
---|
1218 | if (randomize(1) == 0) { |
---|
1219 | taskBuilder.setChild((IOptionalInstance) instance, |
---|
1220 | instantiateTask(child, maxIterationCount)); |
---|
1221 | } |
---|
1222 | } |
---|
1223 | |
---|
1224 | return instance; |
---|
1225 | } |
---|
1226 | |
---|
1227 | /** |
---|
1228 | * |
---|
1229 | */ |
---|
1230 | private int randomize(int max) throws Exception { |
---|
1231 | return randomize(0, max); |
---|
1232 | } |
---|
1233 | |
---|
1234 | /** |
---|
1235 | * |
---|
1236 | */ |
---|
1237 | private int randomize(int min, int max) throws Exception { |
---|
1238 | if (min > max) { |
---|
1239 | throw new IllegalArgumentException("min must always be smaller or equal than max"); |
---|
1240 | } |
---|
1241 | |
---|
1242 | int deviation = max - min; |
---|
1243 | int value = (int) (Math.random() * deviation); |
---|
1244 | |
---|
1245 | return value + min; |
---|
1246 | } |
---|
1247 | |
---|
1248 | /** |
---|
1249 | * |
---|
1250 | */ |
---|
1251 | protected IEventTask createNewEventTask(IEventType eventType, IEventTarget eventTarget) { |
---|
1252 | IEventTask eventTask = taskFactory.createNewEventTask(eventType + " --> " + eventTarget); |
---|
1253 | taskFactory.createNewTaskInstance(eventTask, new Event(eventType, eventTarget)); |
---|
1254 | return eventTask; |
---|
1255 | } |
---|
1256 | |
---|
1257 | /** |
---|
1258 | * |
---|
1259 | */ |
---|
1260 | private ITaskInstance createNewTaskInstance(ITask task) { |
---|
1261 | if (task instanceof IEventTask) { |
---|
1262 | return createNewTaskInstance((IEventTask) task); |
---|
1263 | } |
---|
1264 | else if (task instanceof ISequence) { |
---|
1265 | return createNewTaskInstance((ISequence) task); |
---|
1266 | } |
---|
1267 | else if (task instanceof ISelection) { |
---|
1268 | return createNewTaskInstance((ISelection) task); |
---|
1269 | } |
---|
1270 | else if (task instanceof IIteration) { |
---|
1271 | return createNewTaskInstance((IIteration) task); |
---|
1272 | } |
---|
1273 | else if (task instanceof IOptional) { |
---|
1274 | return createNewTaskInstance((IOptional) task); |
---|
1275 | } |
---|
1276 | |
---|
1277 | throw new IllegalArgumentException("unknown type of task"); |
---|
1278 | } |
---|
1279 | |
---|
1280 | /** |
---|
1281 | * |
---|
1282 | */ |
---|
1283 | private IEventTaskInstance createNewTaskInstance(IEventTask task) { |
---|
1284 | IEventTaskInstance existingInstance = |
---|
1285 | (IEventTaskInstance) task.getInstances().iterator().next(); |
---|
1286 | return taskFactory.createNewTaskInstance(task, existingInstance.getEvent()); |
---|
1287 | } |
---|
1288 | |
---|
1289 | /** |
---|
1290 | * |
---|
1291 | */ |
---|
1292 | private ISequenceInstance createNewTaskInstance(ISequence task) { |
---|
1293 | return taskFactory.createNewTaskInstance(task); |
---|
1294 | } |
---|
1295 | |
---|
1296 | /** |
---|
1297 | * |
---|
1298 | */ |
---|
1299 | private ISelectionInstance createNewTaskInstance(ISelection task) { |
---|
1300 | return taskFactory.createNewTaskInstance(task); |
---|
1301 | } |
---|
1302 | |
---|
1303 | /** |
---|
1304 | * |
---|
1305 | */ |
---|
1306 | private IIterationInstance createNewTaskInstance(IIteration task) { |
---|
1307 | return taskFactory.createNewTaskInstance(task); |
---|
1308 | } |
---|
1309 | |
---|
1310 | /** |
---|
1311 | * |
---|
1312 | */ |
---|
1313 | private IOptionalInstance createNewTaskInstance(IOptional task) { |
---|
1314 | return taskFactory.createNewTaskInstance(task); |
---|
1315 | } |
---|
1316 | |
---|
1317 | } |
---|