Ignore:
Timestamp:
04/04/13 16:06:07 (11 years ago)
Author:
pharms
Message:
  • complete refactoring of task tree model with a separation of task models and task instances
  • appropriate adaptation of task tree generation process
  • appropriate adaptation of commands and task tree visualization
File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/autoquest-core-tasktrees-test/src/test/java/de/ugoe/cs/autoquest/tasktrees/treeimpl/TaskModelTest.java

    r1132 r1146  
    1515package de.ugoe.cs.autoquest.tasktrees.treeimpl; 
    1616 
    17 import static org.junit.Assert.assertEquals; 
    18 import static org.junit.Assert.assertNotNull; 
    19 import static org.junit.Assert.assertNull; 
     17import static org.junit.Assert.*; 
    2018 
    2119import java.util.HashMap; 
     20import java.util.LinkedList; 
     21import java.util.List; 
    2222import java.util.Map; 
    2323 
    2424import org.junit.Test; 
    2525 
     26import de.ugoe.cs.autoquest.eventcore.IEventTarget; 
     27import de.ugoe.cs.autoquest.eventcore.IEventType; 
    2628import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; 
    2729import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; 
     30import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; 
    2831import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; 
    2932import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; 
    30 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTree; 
    31 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder; 
    32 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; 
    33 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeFactory; 
    34 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeInfo; 
    35 import de.ugoe.cs.autoquest.tasktrees.treeimpl.NodeInfo; 
    36 import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskTreeBuilder; 
    37 import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskTreeNodeFactory; 
     33import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; 
     34import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; 
     35import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel; 
     36import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder; 
     37import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory; 
     38import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo; 
     39import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; 
     40import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskInfo; 
     41import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskBuilder; 
     42import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskFactory; 
    3843import de.ugoe.cs.autoquest.test.DummyGUIElement; 
    3944import de.ugoe.cs.autoquest.test.DummyInteraction; 
     
    4550 * @author 2012, last modified by $Author: patrick$ 
    4651 */ 
    47 public class TaskTreeImplTest { 
     52public class TaskModelTest { 
    4853     
    4954    /** */ 
     
    5156 
    5257    /** */ 
    53     private ITaskTreeBuilder taskTreeBuilder = new TaskTreeBuilder(); 
     58    private ITaskBuilder taskBuilder = new TaskBuilder(); 
    5459 
    5560    /** */ 
    56     private ITaskTreeNodeFactory taskTreeNodeFactory = new TaskTreeNodeFactory(); 
    57  
    58     /** 
    59      * @throws Exception 
    60      *  
     61    private ITaskFactory taskFactory = new TaskFactory(); 
     62 
     63    /** 
     64     * 
     65     */ 
     66    @Test 
     67    public void test_EventTask_01() throws Exception { 
     68        IEventType eventType = new DummyInteraction("interaction", 1); 
     69        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     70         
     71        IEventTask task = taskFactory.createNewEventTask(eventType, eventTarget); 
     72         
     73        assertNotNull(task); 
     74        assertNotNull(task.getDescription()); 
     75        assertNotNull(task.getId()); 
     76        assertTrue(task.equals(task)); 
     77         
     78        assertEquals(eventType, task.getEventType()); 
     79        assertEquals(eventTarget, task.getEventTarget()); 
     80    } 
     81 
     82    /** 
     83     * 
     84     */ 
     85    @Test 
     86    public void test_EventTask_02() throws Exception { 
     87        IEventType eventType = new DummyInteraction("interaction", 1); 
     88        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     89         
     90        IEventTask task1 = taskFactory.createNewEventTask(eventType, eventTarget); 
     91        IEventTask task2 = taskFactory.createNewEventTask(eventType, eventTarget); 
     92         
     93        // the tasks will not be equal as they should have a different id 
     94        assertFalse(task1.equals(task2)); 
     95    } 
     96 
     97    /** 
     98     * 
     99     */ 
     100    @Test 
     101    public void test_EventTask_03() throws Exception { 
     102        IEventType eventType = new DummyInteraction("interaction", 1); 
     103        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     104         
     105        IEventTask task = taskFactory.createNewEventTask(eventType, eventTarget); 
     106        ITaskInstance taskInstance1 = taskFactory.createNewTaskInstance(task); 
     107        ITaskInstance taskInstance2 = taskFactory.createNewTaskInstance(task); 
     108         
     109        assertFalse(taskInstance1.equals(taskInstance2)); 
     110    } 
     111 
     112    /** 
     113     * 
     114     */ 
     115    @Test 
     116    public void test_Sequence_01() throws Exception { 
     117        ISequence task = taskFactory.createNewSequence(); 
     118         
     119        assertNotNull(task); 
     120        assertNotNull(task.getDescription()); 
     121        assertNotNull(task.getId()); 
     122        assertNotNull(task.getChildren()); 
     123        assertEquals(0, task.getChildren().size()); 
     124        assertTrue(task.equals(task)); 
     125    } 
     126 
     127    /** 
     128     * 
     129     */ 
     130    @Test 
     131    public void test_Sequence_02() throws Exception { 
     132        IEventType eventType = new DummyInteraction("interaction", 1); 
     133        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     134         
     135        IEventTask child = taskFactory.createNewEventTask(eventType, eventTarget); 
     136 
     137        ISequence task = taskFactory.createNewSequence(); 
     138         
     139        taskBuilder.addChild(task, child); 
     140         
     141        assertNotNull(task.getChildren()); 
     142        assertEquals(1, task.getChildren().size()); 
     143        assertEquals(child, task.getChildren().get(0)); 
     144    } 
     145 
     146    /** 
     147     * 
     148     */ 
     149    @Test 
     150    public void test_Sequence_03() throws Exception { 
     151        IEventType eventType = new DummyInteraction("interaction", 1); 
     152        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     153         
     154        IEventTask child1 = taskFactory.createNewEventTask(eventType, eventTarget); 
     155        IEventTask child2 = taskFactory.createNewEventTask(eventType, eventTarget); 
     156        IEventTask child3 = taskFactory.createNewEventTask(eventType, eventTarget); 
     157        IEventTask child4 = taskFactory.createNewEventTask(eventType, eventTarget); 
     158        IEventTask child5 = taskFactory.createNewEventTask(eventType, eventTarget); 
     159 
     160        ISequence task = taskFactory.createNewSequence(); 
     161         
     162        taskBuilder.addChild(task, child1); 
     163        taskBuilder.addChild(task, child2); 
     164        taskBuilder.addChild(task, child3); 
     165        taskBuilder.addChild(task, child4); 
     166        taskBuilder.addChild(task, child5); 
     167         
     168        assertNotNull(task.getChildren()); 
     169        assertEquals(5, task.getChildren().size()); 
     170        assertEquals(child1, task.getChildren().get(0)); 
     171        assertEquals(child2, task.getChildren().get(1)); 
     172        assertEquals(child3, task.getChildren().get(2)); 
     173        assertEquals(child4, task.getChildren().get(3)); 
     174        assertEquals(child5, task.getChildren().get(4)); 
     175    } 
     176 
     177    /** 
     178     * 
     179     */ 
     180    @Test 
     181    public void test_Selection_01() throws Exception { 
     182        ISelection task = taskFactory.createNewSelection(); 
     183         
     184        assertNotNull(task); 
     185        assertNotNull(task.getDescription()); 
     186        assertNotNull(task.getId()); 
     187        assertNotNull(task.getChildren()); 
     188        assertEquals(0, task.getChildren().size()); 
     189        assertTrue(task.equals(task)); 
     190    } 
     191 
     192    /** 
     193     * 
     194     */ 
     195    @Test 
     196    public void test_Selection_02() throws Exception { 
     197        IEventType eventType = new DummyInteraction("interaction", 1); 
     198        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     199         
     200        IEventTask child = taskFactory.createNewEventTask(eventType, eventTarget); 
     201 
     202        ISelection task = taskFactory.createNewSelection(); 
     203         
     204        taskBuilder.addChild(task, child); 
     205         
     206        assertNotNull(task.getChildren()); 
     207        assertEquals(1, task.getChildren().size()); 
     208        assertEquals(child, task.getChildren().get(0)); 
     209    } 
     210 
     211    /** 
     212     * 
     213     */ 
     214    @Test 
     215    public void test_Selection_03() throws Exception { 
     216        IEventType eventType = new DummyInteraction("interaction", 1); 
     217        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     218         
     219        IEventTask child1 = taskFactory.createNewEventTask(eventType, eventTarget); 
     220        IEventTask child2 = taskFactory.createNewEventTask(eventType, eventTarget); 
     221        IEventTask child3 = taskFactory.createNewEventTask(eventType, eventTarget); 
     222        IEventTask child4 = taskFactory.createNewEventTask(eventType, eventTarget); 
     223        IEventTask child5 = taskFactory.createNewEventTask(eventType, eventTarget); 
     224 
     225        ISelection task = taskFactory.createNewSelection(); 
     226         
     227        taskBuilder.addChild(task, child1); 
     228        taskBuilder.addChild(task, child2); 
     229        taskBuilder.addChild(task, child3); 
     230        taskBuilder.addChild(task, child4); 
     231        taskBuilder.addChild(task, child5); 
     232         
     233        assertNotNull(task.getChildren()); 
     234        assertEquals(5, task.getChildren().size()); 
     235        assertEquals(child1, task.getChildren().get(0)); 
     236        assertEquals(child2, task.getChildren().get(1)); 
     237        assertEquals(child3, task.getChildren().get(2)); 
     238        assertEquals(child4, task.getChildren().get(3)); 
     239        assertEquals(child5, task.getChildren().get(4)); 
     240    } 
     241 
     242    /** 
     243     * 
     244     */ 
     245    @Test 
     246    public void test_Iteration_01() throws Exception { 
     247        IIteration task = taskFactory.createNewIteration(); 
     248         
     249        assertNotNull(task); 
     250        assertNotNull(task.getDescription()); 
     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 = taskFactory.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 = taskFactory.createNewEventTask(eventType, eventTarget); 
     282        IEventTask child2 = taskFactory.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.getDescription()); 
     301        assertNotNull(task.getId()); 
     302        assertNull(task.getMarkedTask()); 
     303        assertTrue(task.equals(task)); 
     304    } 
     305 
     306    /** 
     307     * 
     308     */ 
     309    @Test 
     310    public void test_Optional_02() throws Exception { 
     311        IEventType eventType = new DummyInteraction("interaction", 1); 
     312        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     313         
     314        IEventTask child = taskFactory.createNewEventTask(eventType, eventTarget); 
     315 
     316        IOptional task = taskFactory.createNewOptional(); 
     317         
     318        taskBuilder.setMarkedTask(task, child); 
     319         
     320        assertEquals(child, task.getMarkedTask()); 
     321    } 
     322 
     323    /** 
     324     * 
     325     */ 
     326    @Test 
     327    public void test_Optional_03() throws Exception { 
     328        IEventType eventType = new DummyInteraction("interaction", 1); 
     329        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     330         
     331        IEventTask child1 = taskFactory.createNewEventTask(eventType, eventTarget); 
     332        IEventTask child2 = taskFactory.createNewEventTask(eventType, eventTarget); 
     333 
     334        IOptional task = taskFactory.createNewOptional(); 
     335         
     336        taskBuilder.setMarkedTask(task, child1); 
     337        taskBuilder.setMarkedTask(task, child2); 
     338         
     339        assertEquals(child2, task.getMarkedTask()); 
     340    } 
     341 
     342    /** 
     343     * 
     344     */ 
     345    @Test 
     346    public void test_EventTaskInstance_01() throws Exception { 
     347        IEventType eventType = new DummyInteraction("interaction", 1); 
     348        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     349         
     350        IEventTask task = taskFactory.createNewEventTask(eventType, eventTarget); 
     351         
     352        ITaskInstance taskInstance = taskFactory.createNewTaskInstance(task); 
     353         
     354        assertNotNull(taskInstance); 
     355        assertEquals(task, taskInstance.getTask()); 
     356        assertTrue(taskInstance.equals(taskInstance)); 
     357        assertFalse(taskInstance.equals(task)); 
     358        assertNotNull(taskInstance.getChildren()); 
     359        assertEquals(0, taskInstance.getChildren().size()); 
     360    } 
     361 
     362    /** 
     363     * 
     364     */ 
     365    @Test 
     366    public void test_EventTaskInstance_02() throws Exception { 
     367        IEventType eventType = new DummyInteraction("interaction", 1); 
     368        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     369         
     370        IEventTask task = taskFactory.createNewEventTask(eventType, eventTarget); 
     371         
     372        ITaskInstance taskInstance1 = taskFactory.createNewTaskInstance(task); 
     373        ITaskInstance taskInstance2 = taskFactory.createNewTaskInstance(task); 
     374         
     375        assertFalse(taskInstance1.equals(taskInstance2)); 
     376    } 
     377 
     378    /** 
     379     * 
     380     */ 
     381    @Test(expected=IllegalArgumentException.class) 
     382    public void test_EventTaskInstance_03() throws Exception { 
     383        IEventType eventType = new DummyInteraction("interaction", 1); 
     384        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     385         
     386        IEventTask task = taskFactory.createNewEventTask(eventType, eventTarget); 
     387         
     388        ITaskInstance taskInstance1 = taskFactory.createNewTaskInstance(task); 
     389        ITaskInstance taskInstance2 = taskFactory.createNewTaskInstance(task); 
     390         
     391        taskBuilder.addChild(taskInstance1, taskInstance2); 
     392    } 
     393 
     394    /** 
     395     * 
     396     */ 
     397    @Test(expected=IllegalArgumentException.class) 
     398    public void test_SequenceInstance_01() throws Exception { 
     399        IEventType eventType = new DummyInteraction("interaction", 1); 
     400        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     401         
     402        IEventTask task = taskFactory.createNewEventTask(eventType, eventTarget); 
     403         
     404        ISequence sequence = taskFactory.createNewSequence(); 
     405         
     406        ITaskInstance taskInstance = taskFactory.createNewTaskInstance(task); 
     407        ITaskInstance sequenceInstance = taskFactory.createNewTaskInstance(sequence); 
     408         
     409        taskBuilder.addChild(sequenceInstance, taskInstance); 
     410    } 
     411 
     412    /** 
     413     * 
     414     */ 
     415    @Test 
     416    public void test_SequenceInstance_02() throws Exception { 
     417        IEventType eventType = new DummyInteraction("interaction", 1); 
     418        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     419         
     420        IEventTask task = taskFactory.createNewEventTask(eventType, eventTarget); 
     421         
     422        ISequence sequence = taskFactory.createNewSequence(); 
     423        taskBuilder.addChild(sequence, task); 
     424         
     425        ITaskInstance taskInstance = taskFactory.createNewTaskInstance(task); 
     426        ITaskInstance sequenceInstance = taskFactory.createNewTaskInstance(sequence); 
     427         
     428        taskBuilder.addChild(sequenceInstance, taskInstance); 
     429         
     430        assertNotNull(sequenceInstance.getChildren()); 
     431        assertEquals(1, sequenceInstance.getChildren().size()); 
     432        assertEquals(taskInstance, sequenceInstance.getChildren().get(0)); 
     433    } 
     434 
     435    /** 
     436     * 
     437     */ 
     438    @Test 
     439    public void test_SequenceInstance_03() throws Exception { 
     440        IEventType eventType = new DummyInteraction("interaction", 1); 
     441        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     442         
     443        IEventTask task1 = taskFactory.createNewEventTask(eventType, eventTarget); 
     444        IEventTask task2 = taskFactory.createNewEventTask(eventType, eventTarget); 
     445        IEventTask task3 = taskFactory.createNewEventTask(eventType, eventTarget); 
     446        IEventTask task4 = taskFactory.createNewEventTask(eventType, eventTarget); 
     447        IEventTask task5 = taskFactory.createNewEventTask(eventType, eventTarget); 
     448         
     449        ISequence sequence = taskFactory.createNewSequence(); 
     450        taskBuilder.addChild(sequence, task1); 
     451        taskBuilder.addChild(sequence, task2); 
     452        taskBuilder.addChild(sequence, task3); 
     453        taskBuilder.addChild(sequence, task4); 
     454        taskBuilder.addChild(sequence, task5); 
     455         
     456        ITaskInstance taskInstance1 = taskFactory.createNewTaskInstance(task1); 
     457        ITaskInstance taskInstance2 = taskFactory.createNewTaskInstance(task2); 
     458        ITaskInstance taskInstance3 = taskFactory.createNewTaskInstance(task3); 
     459        ITaskInstance taskInstance4 = taskFactory.createNewTaskInstance(task4); 
     460        ITaskInstance taskInstance5 = taskFactory.createNewTaskInstance(task5); 
     461        ITaskInstance sequenceInstance = taskFactory.createNewTaskInstance(sequence); 
     462         
     463        taskBuilder.addChild(sequenceInstance, taskInstance1); 
     464        taskBuilder.addChild(sequenceInstance, taskInstance2); 
     465        taskBuilder.addChild(sequenceInstance, taskInstance3); 
     466        taskBuilder.addChild(sequenceInstance, taskInstance4); 
     467        taskBuilder.addChild(sequenceInstance, taskInstance5); 
     468         
     469        assertNotNull(sequenceInstance.getChildren()); 
     470        assertEquals(5, sequenceInstance.getChildren().size()); 
     471        assertEquals(taskInstance1, sequenceInstance.getChildren().get(0)); 
     472        assertEquals(taskInstance2, sequenceInstance.getChildren().get(1)); 
     473        assertEquals(taskInstance3, sequenceInstance.getChildren().get(2)); 
     474        assertEquals(taskInstance4, sequenceInstance.getChildren().get(3)); 
     475        assertEquals(taskInstance5, sequenceInstance.getChildren().get(4)); 
     476    } 
     477 
     478    /** 
     479     * 
     480     */ 
     481    @Test(expected=IllegalArgumentException.class) 
     482    public void test_SelectionInstance_01() throws Exception { 
     483        IEventType eventType = new DummyInteraction("interaction", 1); 
     484        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     485         
     486        IEventTask task = taskFactory.createNewEventTask(eventType, eventTarget); 
     487         
     488        ISelection selection = taskFactory.createNewSelection(); 
     489         
     490        ITaskInstance taskInstance = taskFactory.createNewTaskInstance(task); 
     491        ITaskInstance selectionInstance = taskFactory.createNewTaskInstance(selection); 
     492         
     493        taskBuilder.addChild(selectionInstance, taskInstance); 
     494    } 
     495 
     496    /** 
     497     * 
     498     */ 
     499    @Test 
     500    public void test_SelectionInstance_02() throws Exception { 
     501        IEventType eventType = new DummyInteraction("interaction", 1); 
     502        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     503         
     504        IEventTask task = taskFactory.createNewEventTask(eventType, eventTarget); 
     505         
     506        ISelection selection = taskFactory.createNewSelection(); 
     507        taskBuilder.addChild(selection, task); 
     508         
     509        ITaskInstance taskInstance = taskFactory.createNewTaskInstance(task); 
     510        ITaskInstance selectionInstance = taskFactory.createNewTaskInstance(selection); 
     511         
     512        taskBuilder.addChild(selectionInstance, taskInstance); 
     513         
     514        assertNotNull(selectionInstance.getChildren()); 
     515        assertEquals(1, selectionInstance.getChildren().size()); 
     516        assertEquals(taskInstance, selectionInstance.getChildren().get(0)); 
     517    } 
     518 
     519    /** 
     520     * 
     521     */ 
     522    @Test(expected=IllegalArgumentException.class) 
     523    public void test_SelectionInstance_03() throws Exception { 
     524        IEventType eventType = new DummyInteraction("interaction", 1); 
     525        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     526         
     527        IEventTask task1 = taskFactory.createNewEventTask(eventType, eventTarget); 
     528        IEventTask task2 = taskFactory.createNewEventTask(eventType, eventTarget); 
     529        IEventTask task3 = taskFactory.createNewEventTask(eventType, eventTarget); 
     530        IEventTask task4 = taskFactory.createNewEventTask(eventType, eventTarget); 
     531        IEventTask task5 = taskFactory.createNewEventTask(eventType, eventTarget); 
     532         
     533        ISelection selection = taskFactory.createNewSelection(); 
     534        taskBuilder.addChild(selection, task1); 
     535        taskBuilder.addChild(selection, task2); 
     536        taskBuilder.addChild(selection, task3); 
     537        taskBuilder.addChild(selection, task4); 
     538        taskBuilder.addChild(selection, task5); 
     539         
     540        ITaskInstance taskInstance1 = taskFactory.createNewTaskInstance(task1); 
     541        ITaskInstance taskInstance2 = taskFactory.createNewTaskInstance(task2); 
     542        ITaskInstance selectionInstance = taskFactory.createNewTaskInstance(selection); 
     543         
     544        taskBuilder.addChild(selectionInstance, taskInstance1); 
     545        taskBuilder.addChild(selectionInstance, taskInstance2); 
     546    } 
     547 
     548    /** 
     549     * 
     550     */ 
     551    @Test(expected=IllegalArgumentException.class) 
     552    public void test_IterationInstance_01() throws Exception { 
     553        IEventType eventType = new DummyInteraction("interaction", 1); 
     554        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     555         
     556        IEventTask task = taskFactory.createNewEventTask(eventType, eventTarget); 
     557         
     558        IIteration iteration = taskFactory.createNewIteration(); 
     559         
     560        ITaskInstance taskInstance = taskFactory.createNewTaskInstance(task); 
     561        ITaskInstance iterationInstance = taskFactory.createNewTaskInstance(iteration); 
     562         
     563        taskBuilder.addChild(iterationInstance, taskInstance); 
     564    } 
     565 
     566    /** 
     567     * 
     568     */ 
     569    @Test 
     570    public void test_IterationInstance_02() throws Exception { 
     571        IEventType eventType = new DummyInteraction("interaction", 1); 
     572        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     573         
     574        IEventTask task = taskFactory.createNewEventTask(eventType, eventTarget); 
     575         
     576        IIteration iteration = taskFactory.createNewIteration(); 
     577        taskBuilder.setMarkedTask(iteration, task); 
     578         
     579        ITaskInstance taskInstance = taskFactory.createNewTaskInstance(task); 
     580        ITaskInstance iterationInstance = taskFactory.createNewTaskInstance(iteration); 
     581         
     582        taskBuilder.addChild(iterationInstance, taskInstance); 
     583         
     584        assertNotNull(iterationInstance.getChildren()); 
     585        assertEquals(1, iterationInstance.getChildren().size()); 
     586        assertEquals(taskInstance, iterationInstance.getChildren().get(0)); 
     587    } 
     588 
     589    /** 
     590     * 
     591     */ 
     592    @Test(expected=IllegalArgumentException.class) 
     593    public void test_IterationInstance_03() throws Exception { 
     594        IEventType eventType = new DummyInteraction("interaction", 1); 
     595        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     596         
     597        IEventTask task1 = taskFactory.createNewEventTask(eventType, eventTarget); 
     598        IEventTask task2 = taskFactory.createNewEventTask(eventType, eventTarget); 
     599         
     600        IIteration iteration = taskFactory.createNewIteration(); 
     601        taskBuilder.setMarkedTask(iteration, task1); 
     602        taskBuilder.setMarkedTask(iteration, task2); 
     603         
     604        ITaskInstance taskInstance1 = taskFactory.createNewTaskInstance(task1); 
     605        ITaskInstance iterationInstance = taskFactory.createNewTaskInstance(iteration); 
     606         
     607        taskBuilder.addChild(iterationInstance, taskInstance1); 
     608    } 
     609 
     610    /** 
     611     * 
     612     */ 
     613    @Test 
     614    public void test_IterationInstance_04() throws Exception { 
     615        IEventType eventType = new DummyInteraction("interaction", 1); 
     616        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     617         
     618        IEventTask task1 = taskFactory.createNewEventTask(eventType, eventTarget); 
     619        IEventTask task2 = taskFactory.createNewEventTask(eventType, eventTarget); 
     620         
     621        IIteration iteration = taskFactory.createNewIteration(); 
     622        taskBuilder.setMarkedTask(iteration, task1); 
     623        taskBuilder.setMarkedTask(iteration, task2); 
     624         
     625        ITaskInstance taskInstance2 = taskFactory.createNewTaskInstance(task2); 
     626        ITaskInstance iterationInstance = taskFactory.createNewTaskInstance(iteration); 
     627         
     628        taskBuilder.addChild(iterationInstance, taskInstance2); 
     629         
     630        assertNotNull(iterationInstance.getChildren()); 
     631        assertEquals(1, iterationInstance.getChildren().size()); 
     632        assertEquals(taskInstance2, iterationInstance.getChildren().get(0)); 
     633    } 
     634 
     635    /** 
     636     * 
     637     */ 
     638    @Test(expected=IllegalArgumentException.class) 
     639    public void test_OptionalInstance_01() throws Exception { 
     640        IEventType eventType = new DummyInteraction("interaction", 1); 
     641        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     642         
     643        IEventTask task = taskFactory.createNewEventTask(eventType, eventTarget); 
     644         
     645        IOptional optional = taskFactory.createNewOptional(); 
     646         
     647        ITaskInstance taskInstance = taskFactory.createNewTaskInstance(task); 
     648        ITaskInstance optionalInstance = taskFactory.createNewTaskInstance(optional); 
     649         
     650        taskBuilder.addChild(optionalInstance, taskInstance); 
     651    } 
     652 
     653    /** 
     654     * 
     655     */ 
     656    @Test 
     657    public void test_OptionalInstance_02() throws Exception { 
     658        IEventType eventType = new DummyInteraction("interaction", 1); 
     659        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     660         
     661        IEventTask task = taskFactory.createNewEventTask(eventType, eventTarget); 
     662         
     663        IOptional optional = taskFactory.createNewOptional(); 
     664        taskBuilder.setMarkedTask(optional, task); 
     665         
     666        ITaskInstance taskInstance = taskFactory.createNewTaskInstance(task); 
     667        ITaskInstance optionalInstance = taskFactory.createNewTaskInstance(optional); 
     668         
     669        taskBuilder.addChild(optionalInstance, taskInstance); 
     670         
     671        assertNotNull(optionalInstance.getChildren()); 
     672        assertEquals(1, optionalInstance.getChildren().size()); 
     673        assertEquals(taskInstance, optionalInstance.getChildren().get(0)); 
     674    } 
     675 
     676    /** 
     677     * 
     678     */ 
     679    @Test(expected=IllegalArgumentException.class) 
     680    public void test_OptionalInstance_03() throws Exception { 
     681        IEventType eventType = new DummyInteraction("interaction", 1); 
     682        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     683         
     684        IEventTask task1 = taskFactory.createNewEventTask(eventType, eventTarget); 
     685        IEventTask task2 = taskFactory.createNewEventTask(eventType, eventTarget); 
     686         
     687        IOptional optional = taskFactory.createNewOptional(); 
     688        taskBuilder.setMarkedTask(optional, task1); 
     689        taskBuilder.setMarkedTask(optional, task2); 
     690         
     691        ITaskInstance taskInstance1 = taskFactory.createNewTaskInstance(task1); 
     692        ITaskInstance optionalInstance = taskFactory.createNewTaskInstance(optional); 
     693         
     694        taskBuilder.addChild(optionalInstance, taskInstance1); 
     695    } 
     696 
     697    /** 
     698     * 
     699     */ 
     700    @Test 
     701    public void test_OptionalInstance_04() throws Exception { 
     702        IEventType eventType = new DummyInteraction("interaction", 1); 
     703        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     704         
     705        IEventTask task1 = taskFactory.createNewEventTask(eventType, eventTarget); 
     706        IEventTask task2 = taskFactory.createNewEventTask(eventType, eventTarget); 
     707         
     708        IOptional optional = taskFactory.createNewOptional(); 
     709        taskBuilder.setMarkedTask(optional, task1); 
     710        taskBuilder.setMarkedTask(optional, task2); 
     711         
     712        ITaskInstance taskInstance2 = taskFactory.createNewTaskInstance(task2); 
     713        ITaskInstance optionalInstance = taskFactory.createNewTaskInstance(optional); 
     714         
     715        taskBuilder.addChild(optionalInstance, taskInstance2); 
     716         
     717        assertNotNull(optionalInstance.getChildren()); 
     718        assertEquals(1, optionalInstance.getChildren().size()); 
     719        assertEquals(taskInstance2, optionalInstance.getChildren().get(0)); 
     720    } 
     721 
     722    /** 
     723     * 
     724     */ 
     725    @Test 
     726    public void test_UserSession_01() throws Exception { 
     727        IUserSession userSession = taskFactory.createUserSession(); 
     728         
     729        assertNotNull(userSession); 
     730        assertNotNull(userSession.getExecutedTasks()); 
     731        assertEquals(0, userSession.getExecutedTasks().size()); 
     732    } 
     733 
     734    /** 
     735     * 
     736     */ 
     737    @Test 
     738    public void test_UserSession_02() throws Exception { 
     739        IEventType eventType = new DummyInteraction("interaction", 1); 
     740        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     741         
     742        IEventTask task = taskFactory.createNewEventTask(eventType, eventTarget); 
     743         
     744        IUserSession userSession = taskFactory.createUserSession(); 
     745         
     746        ITaskInstance taskInstance = taskFactory.createNewTaskInstance(task); 
     747         
     748        taskBuilder.addExecutedTask(userSession, taskInstance); 
     749         
     750        assertNotNull(userSession.getExecutedTasks()); 
     751        assertEquals(1, userSession.getExecutedTasks().size()); 
     752        assertEquals(taskInstance, userSession.getExecutedTasks().get(0)); 
     753    } 
     754 
     755    /** 
     756     * 
     757     */ 
     758    @Test 
     759    public void test_UserSession_03() throws Exception { 
     760        IEventType eventType = new DummyInteraction("interaction", 1); 
     761        IEventTarget eventTarget = new DummyGUIElement("elem"); 
     762         
     763        IEventTask task = taskFactory.createNewEventTask(eventType, eventTarget); 
     764        ISequence sequence = taskFactory.createNewSequence(); 
     765        ISelection selection = taskFactory.createNewSelection(); 
     766        IIteration iteration = taskFactory.createNewIteration(); 
     767        IOptional optional = taskFactory.createNewOptional(); 
     768         
     769        taskBuilder.addChild(sequence, task); 
     770        taskBuilder.addChild(selection, task); 
     771        taskBuilder.setMarkedTask(iteration, task); 
     772        taskBuilder.setMarkedTask(optional, task); 
     773         
     774        ITaskInstance taskInstance = taskFactory.createNewTaskInstance(task); 
     775        ITaskInstance sequenceInstance = taskFactory.createNewTaskInstance(sequence); 
     776        ITaskInstance selectionInstance = taskFactory.createNewTaskInstance(selection); 
     777        ITaskInstance iterationInstance = taskFactory.createNewTaskInstance(iteration); 
     778        ITaskInstance optionalInstance = taskFactory.createNewTaskInstance(optional); 
     779         
     780        taskBuilder.addChild(sequenceInstance, taskFactory.createNewTaskInstance(task)); 
     781        taskBuilder.addChild(selectionInstance, taskFactory.createNewTaskInstance(task)); 
     782        taskBuilder.addChild(iterationInstance, taskFactory.createNewTaskInstance(task)); 
     783        taskBuilder.addChild(optionalInstance, taskFactory.createNewTaskInstance(task)); 
     784         
     785        IUserSession userSession = taskFactory.createUserSession(); 
     786         
     787        taskBuilder.addExecutedTask(userSession, taskInstance); 
     788        taskBuilder.addExecutedTask(userSession, sequenceInstance); 
     789        taskBuilder.addExecutedTask(userSession, selectionInstance); 
     790        taskBuilder.addExecutedTask(userSession, iterationInstance); 
     791        taskBuilder.addExecutedTask(userSession, optionalInstance); 
     792         
     793        assertNotNull(userSession.getExecutedTasks()); 
     794        assertEquals(5, userSession.getExecutedTasks().size()); 
     795        assertEquals(taskInstance, userSession.getExecutedTasks().get(0)); 
     796        assertEquals(sequenceInstance, userSession.getExecutedTasks().get(1)); 
     797        assertEquals(selectionInstance, userSession.getExecutedTasks().get(2)); 
     798        assertEquals(iterationInstance, userSession.getExecutedTasks().get(3)); 
     799        assertEquals(optionalInstance, userSession.getExecutedTasks().get(4)); 
     800    } 
     801     
     802    /** 
     803     * 
    61804     */ 
    62805    @Test 
     
    67810 
    68811        for (int i = 0; i < noOfTrees; i++) { 
    69             System.err.println("iteration " + (i + 1) + ":"); 
    70             System.err.println("  creating tree"); 
    71             Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos = 
    72                 new HashMap<ITaskTreeNode, ITaskTreeNodeInfo>(); 
    73             ITaskTreeNode rootNode = createTree(noOfMaxChildren, maxDepth, treeInfos); 
    74             System.err.println("  creating task tree"); 
    75             ITaskTree taskTree = taskTreeNodeFactory.createTaskTree(rootNode); 
     812            System.err.println("\niteration " + (i + 1) + ":"); 
     813            System.err.println("  creating tasks"); 
     814            Map<ITask, ITaskInfo> expectedTaskInfos = new HashMap<ITask, ITaskInfo>(); 
     815            ITask task = createTaskTree(noOfMaxChildren, maxDepth, expectedTaskInfos); 
     816            if (!(task instanceof ISequence)) { 
     817                ISequence sequence = taskFactory.createNewSequence(); 
     818                taskBuilder.addChild(sequence, task); 
     819                task = sequence; 
     820            } 
     821            else { 
     822                expectedTaskInfos.remove(task); 
     823            } 
     824             
     825            ITaskInstance taskInstance = instantiateTask(task, noOfMaxChildren); 
     826             
     827            System.err.println("  creating user session"); 
     828             
     829            IUserSession session = taskFactory.createUserSession(); 
     830             
     831            for (ITaskInstance child : taskInstance.getChildren()) { 
     832                taskBuilder.addExecutedTask(session, child); 
     833            } 
     834             
     835            List<IUserSession> sessions = new LinkedList<IUserSession>(); 
     836            sessions.add(session); 
     837             
     838            ITaskModel taskTree = taskFactory.createTaskModel(sessions); 
    76839 
    77840            System.err.println("  validating task tree"); 
    78             assertEquals(rootNode, taskTree.getRoot()); 
    79             assertMapsEqual(treeInfos, taskTree.getTaskMap()); 
    80         } 
    81     } 
    82  
    83     /** 
    84      * TODO: comment 
    85      *  
    86      * @param treeInfos 
    87      * @param taskMap 
    88      */ 
    89     private void assertMapsEqual(Map<ITaskTreeNode, ITaskTreeNodeInfo> map1, 
    90                                  Map<ITaskTreeNode, ITaskTreeNodeInfo> map2) 
     841            Map<ITask, ITaskInfo> actualTaskInfos = new HashMap<ITask, ITaskInfo>(); 
     842             
     843            for (ITask currentTask : taskTree.getTasks()) { 
     844                actualTaskInfos.put(currentTask, taskTree.getTaskInfo(currentTask)); 
     845            } 
     846             
     847            assertMapsEqual(expectedTaskInfos, actualTaskInfos); 
     848        } 
     849    } 
     850 
     851    /** 
     852     * 
     853     */ 
     854    private void assertMapsEqual(Map<ITask, ITaskInfo> map1, 
     855                                 Map<ITask, ITaskInfo> map2) 
    91856    { 
    92857        try { 
     
    98863            assertEquals(map1.size(), map2.size()); 
    99864 
    100             for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : map1.entrySet()) { 
    101                 ITaskTreeNodeInfo value2 = map2.get(entry.getKey()); 
     865            for (Map.Entry<ITask, ITaskInfo> entry : map1.entrySet()) { 
     866                ITaskInfo value2 = map2.get(entry.getKey()); 
    102867                assertNotNull(value2); 
    103868                assertEquals(entry.getValue().getTask(), value2.getTask()); 
     
    114879 
    115880    /** 
    116      * TODO: comment 
    117      *  
    118      * @param map2 
    119      */ 
    120     private void dumpMap(Map<ITaskTreeNode, ITaskTreeNodeInfo> map) { 
     881     * 
     882     */ 
     883    private void dumpMap(Map<ITask, ITaskInfo> map) { 
    121884        System.err.println(); 
    122885 
     
    126889        else { 
    127890            System.err.println("map:"); 
    128             for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : map.entrySet()) { 
     891            for (Map.Entry<ITask, ITaskInfo> entry : map.entrySet()) { 
    129892                System.err.print("  "); 
    130893                System.err.print(entry.getKey()); 
    131                 for (int i = entry.getKey().toString().length(); i < 49; i++) { 
     894                for (int i = entry.getKey().toString().length(); i < 60; i++) { 
    132895                    System.err.print(" "); 
    133896                } 
     
    141904 
    142905    /** 
    143      * TODO: comment 
    144      *  
    145      * @param noOfMaxChildren 
    146      * @param maxDepth 
    147      * @param treeInfos 
    148      * @return 
    149      */ 
    150     private ITaskTreeNode createTree(int                                   maxNoOfChildren, 
    151                                      int                                   maxDepth, 
    152                                      Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos) 
     906     * 
     907     */ 
     908    private ITask createTaskTree(int                   maxNoOfChildren, 
     909                                 int                   maxDepth, 
     910                                 Map<ITask, ITaskInfo> taskInfos) 
    153911        throws Exception 
    154912    { 
    155         /* 
    156          * for (int i = 0; i < (MAX_TREE_DEPTH + 1 - maxDepth); i++) { System.err.print("  "); } 
    157          */ 
    158  
    159         ITaskTreeNode tree; 
     913 
     914        ITask task; 
    160915 
    161916        // integrating the maximum depth here assures, that either something between 0 and 8 will 
    162917        // be the type, or if the max depth decreases near 0 only event tasks will be created 
    163918        // to finish the tree creation 
    164         int type = (int) (Math.random() * (Math.min(8, maxDepth))); 
     919        int type = randomize(Math.min(10, maxDepth)); 
    165920 
    166921        switch (type) 
     
    168923            case 0: { 
    169924                // System.err.print("creating new event task "); 
    170                 tree = createNewEventTask(treeInfos); 
     925                task = createNewEventTask(taskInfos); 
    171926                break; 
    172927            } 
    173928            case 1: { 
    174929                // System.err.print("reusing event task "); 
    175                 tree = reuseEventTask(treeInfos); 
     930                task = reuseEventTask(taskInfos); 
    176931                break; 
    177932            } 
    178933            case 2: { 
    179934                // System.err.println("creating new sequence {"); 
    180                 tree = createNewSequence(maxNoOfChildren, maxDepth, treeInfos); 
     935                task = createNewSequence(maxNoOfChildren, maxDepth, taskInfos); 
    181936                break; 
    182937            } 
    183938            case 3: { 
    184939                // System.err.println("reusing sequence {"); 
    185                 tree = reuseSequence(maxNoOfChildren, maxDepth, treeInfos); 
     940                task = reuseSequence(maxNoOfChildren, maxDepth, taskInfos); 
    186941                break; 
    187942            } 
    188943            case 4: { 
    189944                // System.err.println("creating new selection {"); 
    190                 tree = createNewSelection(maxNoOfChildren, maxDepth, treeInfos); 
     945                task = createNewSelection(maxNoOfChildren, maxDepth, taskInfos); 
    191946                break; 
    192947            } 
    193948            case 5: { 
    194949                // System.err.println("reusing selection {"); 
    195                 tree = reuseSelection(maxNoOfChildren, maxDepth, treeInfos); 
     950                task = reuseSelection(maxNoOfChildren, maxDepth, taskInfos); 
    196951                break; 
    197952            } 
    198953            case 6: { 
    199954                // System.err.println("creating new iteration {"); 
    200                 tree = createNewIteration(maxNoOfChildren, maxDepth, treeInfos); 
     955                task = createNewIteration(maxNoOfChildren, maxDepth, taskInfos); 
    201956                break; 
    202957            } 
    203958            case 7: { 
    204959                // System.err.println("reusing iteration {"); 
    205                 tree = reuseIteration(maxNoOfChildren, maxDepth, treeInfos); 
     960                task = reuseIteration(maxNoOfChildren, maxDepth, taskInfos); 
     961                break; 
     962            } 
     963            case 8: { 
     964                // System.err.println("creating new optional {"); 
     965                task = createNewOptional(maxNoOfChildren, maxDepth, taskInfos); 
     966                break; 
     967            } 
     968            case 9: { 
     969                // System.err.println("reusing optional {"); 
     970                task = reuseOptional(maxNoOfChildren, maxDepth, taskInfos); 
    206971                break; 
    207972            } 
    208973            default: { 
    209974                // System.err.print("creating new event task per default "); 
    210                 tree = createNewEventTask(treeInfos); 
    211             } 
    212         } 
    213  
    214         /* 
    215          * if (!(tree instanceof InteractionTask)) { for (int i = 0; i < (MAX_TREE_DEPTH + 1 - 
    216          * maxDepth); i++) { System.err.print("  "); } 
    217          *  
    218          * System.err.print("} "); } 
    219          *  
    220          * System.err.println(tree); 
    221          */ 
    222  
    223         return tree; 
    224     } 
    225  
    226     /** 
    227      * TODO: comment 
    228      *  
    229      * @param treeInfos 
    230      * @return 
    231      */ 
    232     private IEventTask createNewEventTask(Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos) 
     975                task = createNewEventTask(taskInfos); 
     976            } 
     977        } 
     978 
     979        return task; 
     980    } 
     981 
     982    /** 
     983     * 
     984     */ 
     985    private ITask createNewEventTask(Map<ITask, ITaskInfo> taskInfos) 
    233986        throws Exception 
    234987    { 
    235988        Thread.sleep(2); 
    236989        long id = System.currentTimeMillis(); 
    237         IEventTask task = 
    238             taskTreeNodeFactory.createNewEventTask(new DummyInteraction("interaction" + id, 1), 
    239                                                    new DummyGUIElement("elem" + id)); 
    240  
    241         treeInfos.put(task, new NodeInfo(task)); 
     990        IEventTask task = taskFactory.createNewEventTask 
     991            (new DummyInteraction("interaction" + id, 1), new DummyGUIElement("elem" + id)); 
     992 
     993        taskInfos.put(task, new TaskInfo(task)); 
    242994 
    243995        return task; 
     
    245997 
    246998    /** 
    247      * TODO: comment 
    248      *  
    249      * @param treeInfos 
    250      * @return 
    251      */ 
    252     private IEventTask reuseEventTask(Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos) 
     999     * 
     1000     */ 
     1001    private ITask reuseEventTask(Map<ITask, ITaskInfo> taskInfos) 
    2531002        throws Exception 
    2541003    { 
    255         int noOfEventTasks = 0; 
    256  
    257         for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : treeInfos.entrySet()) { 
    258             if (entry.getKey() instanceof IEventTask) { 
    259                 noOfEventTasks++; 
    260             } 
    261         } 
    262  
    263         if (noOfEventTasks > 0) { 
    264             noOfEventTasks = (int) (Math.random() * noOfEventTasks); 
    265  
    266             for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : treeInfos.entrySet()) { 
    267                 if (entry.getKey() instanceof IEventTask) { 
    268                     if (--noOfEventTasks <= 0) { 
    269                         return (IEventTask) entry.getKey(); 
     1004        ITask eventTask = reuseTask(taskInfos, IEventTask.class); 
     1005         
     1006        if (eventTask == null) { 
     1007            eventTask = createNewEventTask(taskInfos); 
     1008        } 
     1009 
     1010        return eventTask; 
     1011    } 
     1012 
     1013    /** 
     1014     * 
     1015     */ 
     1016    private ITask createNewSequence(int                   maxNoOfChildren, 
     1017                                    int                   maxDepth, 
     1018                                    Map<ITask, ITaskInfo> taskInfos) 
     1019        throws Exception 
     1020    { 
     1021        ISequence sequence = taskFactory.createNewSequence(); 
     1022 
     1023        // ensure at the minimum 2 children 
     1024        int noOfChildren = randomize(2, maxNoOfChildren); 
     1025 
     1026        for (int i = 0; i < noOfChildren; i++) { 
     1027            ITask child = createTaskTree(maxNoOfChildren, maxDepth - 1, taskInfos); 
     1028            taskBuilder.addChild(sequence, child); 
     1029        } 
     1030 
     1031        taskInfos.put(sequence, new TaskInfo(sequence)); 
     1032        return sequence; 
     1033    } 
     1034 
     1035    /** 
     1036     * 
     1037     */ 
     1038    private ITask reuseSequence(int                   maxNoOfChildren, 
     1039                                int                   maxDepth, 
     1040                                Map<ITask, ITaskInfo> taskInfos) 
     1041        throws Exception 
     1042    { 
     1043        ITask sequence = reuseTask(taskInfos, ISequence.class); 
     1044         
     1045        if (sequence == null) { 
     1046            sequence = createNewSequence(maxNoOfChildren, maxDepth, taskInfos); 
     1047        } 
     1048 
     1049        return sequence; 
     1050    } 
     1051 
     1052    /** 
     1053     * 
     1054     */ 
     1055    private ITask createNewSelection(int                   maxNoOfChildren, 
     1056                                     int                   maxDepth, 
     1057                                     Map<ITask, ITaskInfo> taskInfos) 
     1058        throws Exception 
     1059    { 
     1060        ISelection selection = taskFactory.createNewSelection(); 
     1061 
     1062        // ensure at the minimum 1 child 
     1063        int noOfChildren = randomize(1, maxNoOfChildren); 
     1064         
     1065        for (int i = 0; i < noOfChildren; i++) { 
     1066            ITask child = createTaskTree(maxNoOfChildren, maxDepth - 1, taskInfos); 
     1067            taskBuilder.addChild(selection, child); 
     1068        } 
     1069 
     1070        taskInfos.put(selection, new TaskInfo(selection)); 
     1071        return selection; 
     1072    } 
     1073 
     1074    /** 
     1075     * 
     1076     */ 
     1077    private ITask reuseSelection(int                   maxNoOfChildren, 
     1078                                 int                   maxDepth, 
     1079                                 Map<ITask, ITaskInfo> taskInfos) 
     1080        throws Exception 
     1081    { 
     1082        ITask selection = reuseTask(taskInfos, ISelection.class); 
     1083         
     1084        if (selection == null) { 
     1085            selection = createNewSelection(maxNoOfChildren, maxDepth, taskInfos); 
     1086        } 
     1087 
     1088        return selection; 
     1089    } 
     1090 
     1091    /** 
     1092     *  
     1093     */ 
     1094    private ITask createNewIteration(int                   maxNoOfChildren, 
     1095                                     int                   maxDepth, 
     1096                                     Map<ITask, ITaskInfo> taskInfos) 
     1097        throws Exception 
     1098    { 
     1099        IIteration iteration = taskFactory.createNewIteration(); 
     1100 
     1101        ITask child = createTaskTree(maxNoOfChildren, maxDepth - 1, taskInfos); 
     1102        taskBuilder.setMarkedTask(iteration, child); 
     1103 
     1104        taskInfos.put(iteration, new TaskInfo(iteration)); 
     1105        return iteration; 
     1106    } 
     1107 
     1108    /** 
     1109     * 
     1110     */ 
     1111    private ITask reuseIteration(int                   maxNoOfChildren, 
     1112                                 int                   maxDepth, 
     1113                                 Map<ITask, ITaskInfo> taskInfos) 
     1114        throws Exception 
     1115    { 
     1116        ITask iteration = reuseTask(taskInfos, IIteration.class); 
     1117         
     1118        if (iteration == null) { 
     1119            iteration = createNewIteration(maxNoOfChildren, maxDepth, taskInfos); 
     1120        } 
     1121 
     1122        return iteration; 
     1123    } 
     1124 
     1125    /** 
     1126     *  
     1127     */ 
     1128    private ITask createNewOptional(int                   maxNoOfChildren, 
     1129                                    int                   maxDepth, 
     1130                                    Map<ITask, ITaskInfo> taskInfos) 
     1131        throws Exception 
     1132    { 
     1133        IOptional optional = taskFactory.createNewOptional(); 
     1134 
     1135        ITask child = createTaskTree(maxNoOfChildren, maxDepth - 1, taskInfos); 
     1136        taskBuilder.setMarkedTask(optional, child); 
     1137 
     1138        taskInfos.put(optional, new TaskInfo(optional)); 
     1139        return optional; 
     1140    } 
     1141 
     1142    /** 
     1143     * 
     1144     */ 
     1145    private ITask reuseOptional(int                   maxNoOfChildren, 
     1146                                int                   maxDepth, 
     1147                                Map<ITask, ITaskInfo> taskInfos) 
     1148        throws Exception 
     1149    { 
     1150        ITask optional = reuseTask(taskInfos, IOptional.class); 
     1151         
     1152        if (optional == null) { 
     1153            optional = createNewOptional(maxNoOfChildren, maxDepth, taskInfos); 
     1154        } 
     1155 
     1156        return optional; 
     1157    } 
     1158 
     1159    /** 
     1160     * 
     1161     */ 
     1162    private ITask reuseTask(Map<ITask, ITaskInfo> taskInfos, Class<? extends ITask> type) 
     1163        throws Exception 
     1164    { 
     1165        int noOfTasks = 0; 
     1166 
     1167        for (Map.Entry<ITask, ITaskInfo> entry : taskInfos.entrySet()) { 
     1168            if (type.isInstance(entry.getKey())) { 
     1169                noOfTasks++; 
     1170            } 
     1171        } 
     1172 
     1173        if (noOfTasks > 0) { 
     1174            noOfTasks = randomize(noOfTasks); 
     1175 
     1176            for (Map.Entry<ITask, ITaskInfo> entry : taskInfos.entrySet()) { 
     1177                if (type.isInstance(entry.getKey())) { 
     1178                    if (--noOfTasks <= 0) { 
     1179                        return entry.getKey(); 
    2701180                    } 
    2711181                } 
     
    2731183        } 
    2741184        else { 
    275             return createNewEventTask(treeInfos); 
     1185            return null; 
    2761186        } 
    2771187 
     
    2801190 
    2811191    /** 
    282      * TODO: comment 
    283      *  
    284      * @param treeInfos 
    285      * @return 
    286      */ 
    287     private ISequence createNewSequence(int                                   maxNoOfChildren, 
    288                                         int                                   maxDepth, 
    289                                         Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos) 
    290         throws Exception 
    291     { 
    292         ISequence sequence = taskTreeNodeFactory.createNewSequence(); 
    293  
    294         int noOfChildren = (int) (Math.random() * maxNoOfChildren); 
    295  
    296         for (int i = 0; i < noOfChildren; i++) { 
    297             ITaskTreeNode child = createTree(maxNoOfChildren, maxDepth - 1, treeInfos); 
    298  
    299             // through first removing an existing parent it is assured, that a parent is recorded 
    300             // only once. This is needed, because parent may be reused in a tree as well, but we 
    301             // always 
    302             // iterate the whole tree 
    303             ((NodeInfo) treeInfos.get(child)).removeParent(sequence); 
    304             ((NodeInfo) treeInfos.get(child)).addParent(sequence); 
    305             taskTreeBuilder.addChild(sequence, child); 
    306         } 
    307  
    308         treeInfos.put(sequence, new NodeInfo(sequence)); 
    309         return sequence; 
    310     } 
    311  
    312     /** 
    313      * TODO: comment 
    314      *  
    315      * @param treeInfos 
    316      * @return 
    317      */ 
    318     private ISequence reuseSequence(int                                   maxNoOfChildren, 
    319                                     int                                   maxDepth, 
    320                                     Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos) 
    321         throws Exception 
    322     { 
    323         int noOfSequences = 0; 
    324  
    325         for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : treeInfos.entrySet()) { 
    326             if (entry.getKey() instanceof ISequence) { 
    327                 noOfSequences++; 
    328             } 
    329         } 
    330  
    331         if (noOfSequences > 0) { 
    332             noOfSequences = (int) (Math.random() * noOfSequences); 
    333  
    334             for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : treeInfos.entrySet()) { 
    335                 if (entry.getKey() instanceof ISequence) { 
    336                     if (--noOfSequences <= 0) { 
    337                         return (ISequence) entry.getKey(); 
    338                     } 
    339                 } 
    340             } 
    341         } 
    342         else { 
    343             return createNewSequence(maxNoOfChildren, maxDepth, treeInfos); 
    344         } 
    345  
    346         throw new RuntimeException("this is an implementation error"); 
    347     } 
    348  
    349     /** 
    350      * TODO: comment 
    351      *  
    352      * @param treeInfos 
    353      * @return 
    354      */ 
    355     private ISelection createNewSelection(int                                   maxNoOfChildren, 
    356                                           int                                   maxDepth, 
    357                                           Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos) 
    358         throws Exception 
    359     { 
    360         ISelection selection = taskTreeNodeFactory.createNewSelection(); 
    361  
    362         int noOfChildren = (int) (Math.random() * maxNoOfChildren); 
    363  
    364         for (int i = 0; i < noOfChildren; i++) { 
    365             ITaskTreeNode child = createTree(maxNoOfChildren, maxDepth - 1, treeInfos); 
    366  
    367             // through first removing an existing parent it is assured, that a parent is recorded 
    368             // only once. This is needed, because parent may be reused in a tree as well, but we 
    369             // always 
    370             // iterate the whole tree 
    371             ((NodeInfo) treeInfos.get(child)).removeParent(selection); 
    372             ((NodeInfo) treeInfos.get(child)).addParent(selection); 
    373             taskTreeBuilder.addChild(selection, child); 
    374         } 
    375  
    376         treeInfos.put(selection, new NodeInfo(selection)); 
    377         return selection; 
    378     } 
    379  
    380     /** 
    381      * TODO: comment 
    382      *  
    383      * @param treeInfos 
    384      * @return 
    385      */ 
    386     private ISelection reuseSelection(int                                   maxNoOfChildren, 
    387                                       int                                   maxDepth, 
    388                                       Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos) 
    389         throws Exception 
    390     { 
    391         int noOfSelections = 0; 
    392  
    393         for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : treeInfos.entrySet()) { 
    394             if (entry.getKey() instanceof ISelection) { 
    395                 noOfSelections++; 
    396             } 
    397         } 
    398  
    399         if (noOfSelections > 0) { 
    400             noOfSelections = (int) (Math.random() * noOfSelections); 
    401  
    402             for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : treeInfos.entrySet()) { 
    403                 if (entry.getKey() instanceof ISelection) { 
    404                     if (--noOfSelections <= 0) { 
    405                         return (ISelection) entry.getKey(); 
    406                     } 
    407                 } 
    408             } 
    409         } 
    410         else { 
    411             return createNewSelection(maxNoOfChildren, maxDepth, treeInfos); 
    412         } 
    413  
    414         throw new RuntimeException("this is an implementation error"); 
    415     } 
    416  
    417     /** 
    418      * TODO: comment 
    419      *  
    420      * @param treeInfos 
    421      * @return 
    422      */ 
    423     private IIteration createNewIteration(int                                   maxNoOfChildren, 
    424                                           int                                   maxDepth, 
    425                                           Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos) 
    426         throws Exception 
    427     { 
    428         IIteration iteration = taskTreeNodeFactory.createNewIteration(); 
    429  
    430         ITaskTreeNode child = createTree(maxNoOfChildren, maxDepth - 1, treeInfos); 
    431  
    432         // through first removing an existing parent it is assured, that a parent is recorded 
    433         // only once. This is needed, because parent may be reused in a tree as well, but we always 
    434         // iterate the whole tree 
    435         ((NodeInfo) treeInfos.get(child)).removeParent(iteration); 
    436         ((NodeInfo) treeInfos.get(child)).addParent(iteration); 
    437         taskTreeBuilder.setChild(iteration, child); 
    438  
    439         treeInfos.put(iteration, new NodeInfo(iteration)); 
    440         return iteration; 
    441     } 
    442  
    443     /** 
    444      * TODO: comment 
    445      *  
    446      * @param treeInfos 
    447      * @return 
    448      */ 
    449     private IIteration reuseIteration(int                                   maxNoOfChildren, 
    450                                       int                                   maxDepth, 
    451                                       Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos) 
    452         throws Exception 
    453     { 
    454         int noOfIterations = 0; 
    455  
    456         for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : treeInfos.entrySet()) { 
    457             if (entry.getKey() instanceof IIteration) { 
    458                 noOfIterations++; 
    459             } 
    460         } 
    461  
    462         if (noOfIterations > 0) { 
    463             noOfIterations = (int) (Math.random() * noOfIterations); 
    464  
    465             for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : treeInfos.entrySet()) { 
    466                 if (entry.getKey() instanceof IIteration) { 
    467                     if (--noOfIterations <= 0) { 
    468                         return (IIteration) entry.getKey(); 
    469                     } 
    470                 } 
    471             } 
    472         } 
    473         else { 
    474             return createNewIteration(maxNoOfChildren, maxDepth, treeInfos); 
    475         } 
    476  
    477         throw new RuntimeException("this is an implementation error"); 
    478     } 
    479  
     1192     * 
     1193     */ 
     1194    private ITaskInstance instantiateTask(ITask task, int maxIterationCount) throws Exception { 
     1195        ITaskInstance instance = taskFactory.createNewTaskInstance(task); 
     1196 
     1197        if (task instanceof ISequence) { 
     1198            for (ITask child : ((ISequence) task).getChildren()) { 
     1199                taskBuilder.addChild(instance, instantiateTask(child, maxIterationCount)); 
     1200            } 
     1201        } 
     1202        else if (task instanceof ISelection) { 
     1203            List<ITask> children = ((ISelection) task).getChildren(); 
     1204            int index = randomize(children.size()); 
     1205            taskBuilder.addChild(instance, instantiateTask(children.get(index), maxIterationCount)); 
     1206        } 
     1207        else if (task instanceof IIteration) { 
     1208            int count = randomize(maxIterationCount); 
     1209            ITask child = ((IIteration) task).getMarkedTask(); 
     1210             
     1211            for (int i = 0; i < count; i++) { 
     1212                taskBuilder.addChild(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.addChild(instance, instantiateTask(child, maxIterationCount)); 
     1220            } 
     1221        } 
     1222         
     1223        return instance; 
     1224    } 
     1225 
     1226    /** 
     1227     * 
     1228     */ 
     1229    private int randomize(int max) throws Exception { 
     1230        return randomize(0, max); 
     1231    } 
     1232     
     1233    /** 
     1234     * 
     1235     */ 
     1236    private int randomize(int min, int max) throws Exception { 
     1237        if (min > max) { 
     1238            throw new IllegalArgumentException("min must always be smaller or equal than max"); 
     1239        } 
     1240         
     1241        int deviation = max - min; 
     1242        int value = (int) (Math.random() * deviation); 
     1243         
     1244        return value + min; 
     1245    } 
    4801246} 
Note: See TracChangeset for help on using the changeset viewer.