Index: /trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/CondenseSimilarTasksRule.java
===================================================================
--- /trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/CondenseSimilarTasksRule.java	(revision 1966)
+++ /trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/CondenseSimilarTasksRule.java	(revision 1967)
@@ -578,5 +578,6 @@
                 else {
                     // its time to create a selection of both variants. If one is already
-                    // a selection, it is reused and extended, if required
+                    // a selection, it is reused and extended, if required.
+                    
                     ITask expectedChild1 = null;
                     ITask expectedChild2 = null;
@@ -607,13 +608,68 @@
                     }
                     else if (selection == null) {
-                        // none of both is already a selection, so create a new one with both
-                        // of the children as variants
-                        selection = taskFactory.createNewSelection();
-                        // ((Task) selection).setDescription(selection.getDescription() +
-                        //                                   " created for " + delta.getType());
-                        taskBuilder.addChild(selection, child1);
-                        taskBuilder.addChild(selection, child2);
-                        expectedChild1 = child1;
-                        expectedChild2 = child2;
+                        // it may also be the case, that one is a child of a selection and occurs
+                        // only as such and nowhere else. Then the parent selection is reused and
+                        // extended.
+                        for (ITask candidate : appData.taskModel.getTasks()) {
+                            if (candidate instanceof ISelection) {
+                                selection = (ISelection) candidate;
+                                ITask existingChildTask = null;
+                                
+                                if (selection.getChildren().contains(child1)) {
+                                    existingChildTask = child1;
+                                }
+                                else if (selection.getChildren().contains(child2)) {
+                                    existingChildTask = child2;
+                               }
+                                
+                                if (existingChildTask != null) {
+                                    int noOfInstances = 0;
+                                    
+                                    for (ITaskInstance instance : selection.getInstances()) {
+                                        ITaskInstance childInstance =
+                                            ((ISelectionInstance) instance).getChild();
+                                            
+                                        if (childInstance.getTask() == existingChildTask) {
+                                            noOfInstances++;
+                                        }
+                                    }
+                                    
+                                    if (noOfInstances < existingChildTask.getInstances().size()) {
+                                        // not all instances of the existing child task are covered
+                                        // by the parent selection. Hence, throw selection away and
+                                        // search for another one
+                                        selection = null;
+                                    }
+                                    else {
+                                        // found a parent selection --> reuse it but add the new
+                                        // variant
+                                        if (child1 == existingChildTask) {
+                                            addSelectionChildIfRequired(selection, child2);
+                                        }
+                                        else {
+                                            addSelectionChildIfRequired(selection, child1);
+                                        }
+                                        expectedChild1 = child1;
+                                        expectedChild2 = child2;
+                                        break;
+                                    }
+                                }
+                                else {
+                                    selection = null;
+                                }
+                            }
+                        }
+                        
+                        if (selection == null) {
+                            // none of both is already a selection, so create a new one with both
+                            // of the children as variants
+                            selection = taskFactory.createNewSelection();
+                            // ((Task) selection).setDescription(selection.getDescription() +
+                            //                                   " created for " + delta.getType());
+                            taskBuilder.addChild(selection, child1);
+                            taskBuilder.addChild(selection, child2);
+                            expectedChild1 = child1;
+                            expectedChild2 = child2;
+                        }
                     }
 
@@ -1148,4 +1204,8 @@
                 
                 if (replacement instanceof ISelectionInstance) {
+                    // if the replacement itself is a selection instance, we cannot add
+                    // a selection instance as the child of the parent selection instance.
+                    // therefore, we just use the child of the replacement as new child of the
+                    // existing selection instance
                     taskBuilder.discardTaskInstance(replacement);
                     replacement = ((ISelectionInstance) replacement).getChild();
