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