Index: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/SelectionComparisonRule.java
===================================================================
--- trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/SelectionComparisonRule.java	(revision 1190)
+++ trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/SelectionComparisonRule.java	(revision 1294)
@@ -18,5 +18,7 @@
 
 import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
+import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance;
 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
+import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
 
 /**
@@ -39,5 +41,5 @@
 
     /* (non-Javadoc)
-     * @see NodeComparisonRule#isApplicable(ITask, ITask)
+     * @see TaskComparisonRule#isApplicable(ITask, ITask)
      */
     @Override
@@ -47,5 +49,5 @@
 
     /* (non-Javadoc)
-     * @see NodeComparisonRule#areLexicallyEqual(ITask, ITask)
+     * @see TaskComparisonRule#areLexicallyEqual(ITask, ITask)
      */
     @Override
@@ -56,5 +58,5 @@
 
     /* (non-Javadoc)
-     * @see NodeComparisonRule#areSyntacticallyEqual(ITask, ITask)
+     * @see TaskComparisonRule#areSyntacticallyEqual(ITask, ITask)
      */
     @Override
@@ -65,5 +67,5 @@
 
     /* (non-Javadoc)
-     * @see NodeComparisonRule#areSemanticallyEqual(ITask, ITask)
+     * @see TaskComparisonRule#areSemanticallyEqual(ITask, ITask)
      */
     @Override
@@ -74,9 +76,52 @@
 
     /* (non-Javadoc)
-     * @see NodeComparisonRule#compare(ITask, ITask)
+     * @see TaskComparisonRule#compare(ITask, ITask)
      */
     @Override
     public TaskEquality compare(ITask task1, ITask task2) {
         return getEquality(task1, task2, null);
+    }
+
+    /* (non-Javadoc)
+     * @see TaskComparisonRule#isApplicable(ITaskInstance, ITaskInstance)
+     */
+    @Override
+    public boolean isApplicable(ITaskInstance instance1, ITaskInstance instance2) {
+        return isApplicable(instance1.getTask(), instance2.getTask());
+    }
+
+    /* (non-Javadoc)
+     * @see TaskComparisonRule#areLexicallyEqual(ITaskInstance, ITaskInstance)
+     */
+    @Override
+    public boolean areLexicallyEqual(ITaskInstance instance1, ITaskInstance instance2) {
+        TaskEquality equality = getEquality(instance1, instance2, TaskEquality.LEXICALLY_EQUAL);
+        return (equality != null) && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL));
+    }
+
+    /* (non-Javadoc)
+     * @see TaskComparisonRule#areSyntacticallyEqual(ITaskInstance, ITaskInstance)
+     */
+    @Override
+    public boolean areSyntacticallyEqual(ITaskInstance instance1, ITaskInstance instance2) {
+        TaskEquality equality = getEquality(instance1, instance2, TaskEquality.SYNTACTICALLY_EQUAL);
+        return (equality != null) && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL));
+    }
+
+    /* (non-Javadoc)
+     * @see TaskComparisonRule#areSemanticallyEqual(ITaskInstance, ITaskInstance)
+     */
+    @Override
+    public boolean areSemanticallyEqual(ITaskInstance instance1, ITaskInstance instance2) {
+        TaskEquality equality = getEquality(instance1, instance2, TaskEquality.SEMANTICALLY_EQUAL);
+        return (equality != null) && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL));
+    }
+
+    /* (non-Javadoc)
+     * @see TaskComparisonRule#compare(ITaskInstance, ITaskInstance)
+     */
+    @Override
+    public TaskEquality compare(ITaskInstance instance1, ITaskInstance instance2) {
+        return getEquality(instance1, instance2, null);
     }
 
@@ -107,23 +152,12 @@
         }
 
-        TaskEquality selectionEquality;
-
         if (requiredEqualityLevel == null) {
             // calculate the common equality level for all children of both selections.
             // do it in both directions to ensure commutative comparison
-            selectionEquality = getCommonEqualityLevel(children1, children2);
-            if (selectionEquality != TaskEquality.UNEQUAL) {
-                return selectionEquality.getCommonDenominator
-                    (getCommonEqualityLevel(children2, children1));
-            }
-            else {
-                return TaskEquality.UNEQUAL;
-            }
+            return getMostConcreteEqualityLevel(children1, children2);
         }
         else {
             // we are searching for a specific equality
-            if (checkEqualityLevel(children1, children2, requiredEqualityLevel) &&
-                checkEqualityLevel(children2, children1, requiredEqualityLevel))
-            {
+            if (checkEqualityLevel(children1, children2, requiredEqualityLevel)) {
                 return requiredEqualityLevel;
             }
@@ -136,7 +170,7 @@
     /**
      * <p>
-     * determines the common equality level for all tasks in the first list compared to all
-     * tasks in the second list. If for one task in the first list, there is no equal task in the
-     * second list, the method return unequality. 
+     * determines the most concrete equality level for all tasks in the first list compared to all
+     * tasks in the second list. It is sufficient, if there is one task in one list for which there
+     * exist an equal task in the other list.
      * </p>
      *
@@ -144,10 +178,9 @@
      * @param children2 the second list to be compared
      * 
-     * @return the common task equality identified for all tasks in the first list with respect to
-     *         the second list
-     */
-    private TaskEquality getCommonEqualityLevel(List<ITask> children1, List<ITask> children2) {
-        TaskEquality listEquality = TaskEquality.LEXICALLY_EQUAL;
-        
+     * @return the most concrete task equality identified for all tasks in the first list with
+     *         respect to the second list
+     */
+    private TaskEquality getMostConcreteEqualityLevel(List<ITask> children1, List<ITask> children2)
+    {
         TaskEquality childEquality;
         TaskEquality currentEquality;
@@ -164,28 +197,20 @@
                     }
                     
-                    if (childEquality == TaskEquality.SEMANTICALLY_EQUAL) {
-                        // as we calculate only the common denominator, we can break up here for
-                        // the current child. We will not improve the denominator anymore
-                        break;
+                    if (childEquality.isAtLeast(TaskEquality.LEXICALLY_EQUAL)) {
+                        // as we calculate the most concrete equality, we can break up here
+                        return TaskEquality.LEXICALLY_EQUAL;
                     }
                 }
             }
-            
-            if (childEquality == null) {
-                // we did not find any child in the second list, that is equal to the searched
-                // child
-                return TaskEquality.UNEQUAL;
-            }
-            else {
-                listEquality = listEquality.getCommonDenominator(childEquality);
-            }
-        }
-
-        return listEquality;
-    }
-
-    /**
-     * <p>
-     * ensures for the two given lists, that for each task in the first list there is a task
+        }
+
+        // as the comparison should be commutative, we do not need to check, if in list 2 there is
+        // a child equal to one in list 1
+        return TaskEquality.UNEQUAL;
+    }
+
+    /**
+     * <p>
+     * ensures for the two given lists, that for at least one task in the first list there is a task
      * in the second list being on the given level equal to the task in the first list.
      * </p> 
@@ -195,6 +220,6 @@
      * @param requiredEqualityLevel the equality level to be checked for
      * 
-     * @return true if each task in the first list has an equal task in the second list when
-     *         considering the given equality level, false else.
+     * @return true if there is a task in the first list that has an equal task in the second list
+     *         when considering the given equality level, false else.
      */
     private boolean checkEqualityLevel(List<ITask>  children1,
@@ -202,8 +227,6 @@
                                        TaskEquality requiredEqualityLevel)
     {
-        TaskEquality childEquality;
         TaskEquality currentEquality;
         for (ITask child1 : children1) {
-            childEquality = null;
             for (ITask child2 : children2) {
                 currentEquality = callRuleManager(child1, child2, requiredEqualityLevel);
@@ -212,18 +235,10 @@
                     // we found at least one equal child with sufficient equality in the
                     // second list. So be can break up for this child.
-                    childEquality = currentEquality;
-                    break;
+                    return true;
                 }
             }
-            
-            if (childEquality == null) {
-                // we did not find any child in the second list, that is equal to the searched
-                // child
-                return false;
-            }
-        }
-
-        // for all children, we found an equality 
-        return true;
+        }
+
+        return false;
     }
 
@@ -259,3 +274,74 @@
     }
 
+    /**
+     * <p>
+     * compares two selection instances with each other checking for the provided required level of
+     * equality. If this level is ensured, the method immediately returns. The more concrete
+     * the required equality level, the more checks this method performs.
+     * </p>
+     * 
+     * @param taskInstance1         the first task instance to be compared
+     * @param taskInstance2         the second task instance to be compared
+     * @param requiredEqualityLevel the equality level to be checked for
+     * 
+     * @return the determined equality.
+     */
+    private TaskEquality getEquality(ITaskInstance taskInstance1,
+                                     ITaskInstance taskInstance2,
+                                     TaskEquality  requiredEqualityLevel)
+    {
+        ITaskInstance child1 = ((ISelectionInstance) taskInstance1).getChild();
+        ITaskInstance child2 = ((ISelectionInstance) taskInstance2).getChild();
+
+        // if both selections do not have children, they are lexically equal. If only one of them
+        // has children, they are unequal.
+        if ((child1 == null) && (child2 == null)) {
+            return TaskEquality.LEXICALLY_EQUAL;
+        }
+        else if ((child1 == null) || (child2 == null)) {
+            return TaskEquality.UNEQUAL;
+        }
+
+        TaskEquality equality = callRuleManager(child1, child2, requiredEqualityLevel);
+        
+        if (equality == TaskEquality.IDENTICAL) {
+            // two different selection instances can be at most lexically equal even if their
+            // children are identical
+            return TaskEquality.LEXICALLY_EQUAL;
+        }
+        else {
+            return equality;
+        }
+    }
+
+    /**
+     * <p>
+     * used to to call the task equality rule manager for the comparison of the two provided
+     * children. If no required equality level is provided, than the most concrete equality is
+     * returned. Otherwise, the required equality is returned as long as the children are equal
+     * on that level.
+     * </p> 
+     * 
+     * @param taskInstance1         the first task instance to be compared
+     * @param taskInstance2         the second task instance to be compared
+     * @param requiredEqualityLevel the equality level to be checked for
+     * 
+     * @return the determined equality
+     */
+    private TaskEquality callRuleManager(ITaskInstance taskInstance1,
+                                         ITaskInstance taskInstance2,
+                                         TaskEquality  requiredEqualityLevel)
+    {
+        if (requiredEqualityLevel == null) {
+            return TaskEqualityRuleManager.getInstance().compare(taskInstance1, taskInstance2);
+        }
+        else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual
+                     (taskInstance1, taskInstance2, requiredEqualityLevel))
+        {
+            return requiredEqualityLevel;
+        }
+        else {
+            return TaskEquality.UNEQUAL;
+        }
+    }
 }
