Index: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/GUIEventTaskComparisonRule.java
===================================================================
--- trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/GUIEventTaskComparisonRule.java	(revision 1855)
+++ trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/GUIEventTaskComparisonRule.java	(revision 1887)
@@ -341,4 +341,6 @@
             }
             else {
+                // pressing a key on the same target, e.g. a text field, usually has the same
+                // semantics
                 return TaskEquality.SEMANTICALLY_EQUAL;
             }
@@ -389,4 +391,6 @@
         }
         
+        // performing drag and drops on the same target usually have the same semantic meaning,
+        // i.e., the same function is called
         return TaskEquality.SEMANTICALLY_EQUAL;
     }
@@ -459,4 +463,6 @@
             }
             else {
+                // in most situations, the coordinates are not of interest. But if they are, then
+                // the event can be at most semantically equal
                 return TaskEquality.SEMANTICALLY_EQUAL;
             }
@@ -468,22 +474,10 @@
     /**
      * <p>
-     * compares two mouse button interactions such as clicks, mouse button down, or double clicks.
-     * If both interactions have the same coordinates, they are lexically equal. Otherwise, they
-     * are semantically equal. Mouse clicks for which the coordinates make no lexical difference
-     * (see {@link #clickCoordinatesMakeLexicalDifference(IEventTarget)}) are treated as
-     * lexically equal.
-     * </p>
-     * <p>
-     * The provided equality level can be used to restrict the quality check to the given level.
-     * This is done for optimization purposes. The returned equality level is as concrete as
-     * the provided one. It may be more concrete if there is no difference regarding the
-     * comparison on the levels.
-     * </p>
-     *
-     * @param interaction1  the first mouse button interaction to compare
-     * @param interaction2  the second mouse button interaction to compare
-     * @param eventTarget   the event target on which the interactions happened (used within
-     *                      special comparisons like mouse clicks on buttons, where the coordinates
-     *                      can be ignored)
+     * compares two scrolls and considers them as lexically equal if they have the same coordinates.
+     * Otherwise, they are syntactically equal as the happen on the same target
+     * </p>
+     *
+     * @param interaction1  the first scroll interaction to compare
+     * @param interaction2  the second scroll interaction to compare
      * @param equalityLevel the equality level to be checked for
      * 
@@ -494,5 +488,5 @@
                                         TaskEquality equalityLevel)
     {
-        if (equalityLevel.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL)) {
+        if (equalityLevel.isAtLeast(TaskEquality.LEXICALLY_EQUAL)) {
             int x1 = interaction1.getXPosition();
             int x2 = interaction2.getXPosition();
@@ -505,5 +499,5 @@
         }
         
-        return TaskEquality.SEMANTICALLY_EQUAL;
+        return TaskEquality.SYNTACTICALLY_EQUAL;
     }
 
Index: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/IterationComparisonRule.java
===================================================================
--- trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/IterationComparisonRule.java	(revision 1855)
+++ trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/IterationComparisonRule.java	(revision 1887)
@@ -192,5 +192,5 @@
         IIterationInstance iteration2 = (IIterationInstance) instance2;
 
-        // if both sequences do not have children, they are equal although this doesn't make sense
+        // if both iterations do not have children, they are equal although this doesn't make sense
         if ((iteration1.size() == 0) && (iteration2.size() == 0)) {
             return true;
Index: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/OptionalComparisonRule.java
===================================================================
--- trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/OptionalComparisonRule.java	(revision 1887)
+++ trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/OptionalComparisonRule.java	(revision 1887)
@@ -0,0 +1,380 @@
+//   Copyright 2012 Georg-August-Universität Göttingen, Germany
+//
+//   Licensed under the Apache License, Version 2.0 (the "License");
+//   you may not use this file except in compliance with the License.
+//   You may obtain a copy of the License at
+//
+//       http://www.apache.org/licenses/LICENSE-2.0
+//
+//   Unless required by applicable law or agreed to in writing, software
+//   distributed under the License is distributed on an "AS IS" BASIS,
+//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//   See the License for the specific language governing permissions and
+//   limitations under the License.
+
+package de.ugoe.cs.autoquest.tasktrees.taskequality;
+
+import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
+import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance;
+import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
+import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
+import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
+
+/**
+ * <p>
+ * This class is capable of comparing Optionals. Optionals equal at distinct levels
+ * in distinct situations. The following table shows the results of the comparison for the
+ * specific situations (the parameters are commutative). In any other situation, the comparison
+ * returns <code>NodeEquality.UNEQUAL</code>:
+ * </p>
+ * 
+ * <table border="1">
+ *   <tr>
+ *     <th>optional 1</th>
+ *     <th>optional 2</th>
+ *     <th>comparison result</th>
+ *   </tr>
+ *   <tr>
+ *     <td>any optional</td>
+ *     <td>any optional with a child that is lexically equal to the child of optional 1</td>
+ *     <td><code>NodeEquality.LEXICALLY_EQUAL</code></td>
+ *   </tr>
+ *   <tr>
+ *     <td>any optional</td>
+ *     <td>any optional with a child that is syntactically equal to the child of optional 1</td>
+ *     <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td>
+ *   </tr>
+ *   <tr>
+ *     <td>any optional</td>
+ *     <td>any optional with a child that is semantically equal to the child of optional 1</td>
+ *     <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td>
+ *   </tr>
+ *   <tr>
+ *     <td>an optional with a selection of syntactically equal children</td>
+ *     <td>an optional with a child that is syntactically equal to the children of the child
+ *     selection of optional 1</td>
+ *     <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td>
+ *   </tr>
+ *   <tr>
+ *     <td>an optional with a selection of syntactically equal children</td>
+ *     <td>an optional with a selection of syntactically equal children that are all syntactically
+ *     equal to the selection of children of optional 1</td>
+ *     <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td>
+ *   </tr>
+ *   <tr>
+ *     <td>an optional with a selection of semantically equal children</td>
+ *     <td>an optional with a child that is semantically equal to the children of the child
+ *     selection of optional 1</td>
+ *     <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td>
+ *   </tr>
+ *   <tr>
+ *     <td>an optional with a selection of semantically equal children</td>
+ *     <td>an optional with a selection of semantically equal children that are all semantically
+ *     equal to the selection of children of optional 1</td>
+ *     <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td>
+ *   </tr>
+ * </table>
+ * 
+ * @version $Revision: $ $Date: 19.02.2012$
+ * @author 2012, last modified by $Author: patrick$
+ */
+public class OptionalComparisonRule implements TaskComparisonRule {
+    
+    /* (non-Javadoc)
+     * @see TaskComparisonRule#isApplicable(ITask, ITask)
+     */
+    @Override
+    public boolean isApplicable(ITask task1, ITask task2) {
+        return (task1 instanceof IOptional) && (task2 instanceof IOptional);
+    }
+
+    /* (non-Javadoc)
+     * @see TaskComparisonRule#areLexicallyEqual(ITask, ITask)
+     */
+    @Override
+    public boolean areLexicallyEqual(ITask task1, ITask task2) {
+        ITask child1 = ((IOptional) task1).getMarkedTask();
+        ITask child2 = ((IOptional) task2).getMarkedTask();
+        
+        if (child1 != null) {
+            if (child2 == null) {
+                return false;
+            }
+            else {
+                // optionals may have 3 different structures.
+                // 1. they have one child, which is the optional one
+                // 2. they have a sequence of children, which is optional
+                // 3. they have a selection of different optional variants (usually the variants
+                //    are semantically equal)
+                // ignore the type of the children but check them for equality.
+                
+                return getNodeEquality(child1, child2).isAtLeast(TaskEquality.LEXICALLY_EQUAL);
+            }
+        }
+        else if (child2 == null) {
+            return true;
+        }
+        
+        return false;
+    }
+
+    /* (non-Javadoc)
+     * @see TaskComparisonRule#areSyntacticallyEqual(ITask, ITask)
+     */
+    @Override
+    public boolean areSyntacticallyEqual(ITask task1, ITask task2) {
+        return areLexicallyEqual(task1, task2);
+    }
+
+    /* (non-Javadoc)
+     * @see TaskComparisonRule#areSemanticallyEqual(ITask, ITask)
+     */
+    @Override
+    public boolean areSemanticallyEqual(ITask task1, ITask task2) {
+        return compare(task1, task2).isAtLeast(TaskEquality.SEMANTICALLY_EQUAL);
+    }
+
+    /* (non-Javadoc)
+     * @see TaskComparisonRule#compare(ITask, ITask)
+     */
+    @Override
+    public TaskEquality compare(ITask task1, ITask task2) {
+        ITask child1 = ((IOptional) task1).getMarkedTask();
+        ITask child2 = ((IOptional) task2).getMarkedTask();
+
+        // if both optionals do not have a child, they are equal although this doesn't make sense
+        if (child1 == child2) {
+            return TaskEquality.LEXICALLY_EQUAL;
+        }
+        else if ((child1 == null) || (child2 == null)) {
+            return TaskEquality.UNEQUAL;
+        }
+
+        // optionals may have 3 different structures.
+        // 1. they have one child, which is the optional one
+        // 2. they have a sequence of children, which is optional
+        // 3. they have a selection of different optional variants (usually the variants are
+        // semantically equal)
+        //
+        // the permutations of the three variants in combination must be checked
+
+        // check if both tasks are the same variants of optional and if their children are equal.
+        // This condition matches, if both optionals are the same variants of optional. I.e. three
+        // combinations of the permutation are handled herewith.
+        TaskEquality taskEquality = getNodeEquality(child1, child2);
+        
+        if (taskEquality != null) {
+            return taskEquality;
+        }
+
+        // compare one optional with a single task as a child and another one with a selection of
+        // semantically equal tasks
+        return selectionChildrenSemanticallyEqualNode(child1, child2);
+        
+        // all other combinations (i.e. sequence with single child and sequence with selection)
+        // can not match
+    }
+
+    /* (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) {
+        IOptionalInstance optional1 = (IOptionalInstance) instance1;
+        IOptionalInstance optional2 = (IOptionalInstance) instance2;
+
+        // if both optionals do not have a child, they are equal although this doesn't make sense
+        if (optional1.getChild() == optional2.getChild()) {
+            return true;
+        }
+        else if ((optional1.getChild() == null) || (optional2.getChild() == null)) {
+            return false;
+        }
+
+        TaskEquality taskEquality = callRuleManager
+            (optional1.getChild(), optional2.getChild(), TaskEquality.LEXICALLY_EQUAL);
+
+        if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) {
+            return false;
+        }
+
+        return true;
+    }
+
+    /* (non-Javadoc)
+     * @see TaskComparisonRule#areSyntacticallyEqual(ITaskInstance, ITaskInstance)
+     */
+    @Override
+    public boolean areSyntacticallyEqual(ITaskInstance instance1, ITaskInstance instance2) {
+        return areLexicallyEqual(instance1, instance2);
+    }
+
+    /* (non-Javadoc)
+     * @see TaskComparisonRule#areSemanticallyEqual(ITaskInstance, ITaskInstance)
+     */
+    @Override
+    public boolean areSemanticallyEqual(ITaskInstance instance1, ITaskInstance instance2) {
+        return areLexicallyEqual(instance1, instance2);
+    }
+
+    /* (non-Javadoc)
+     * @see TaskComparisonRule#compare(ITaskInstance, ITaskInstance)
+     */
+    @Override
+    public TaskEquality compare(ITaskInstance instance1, ITaskInstance instance2) {
+        if (areLexicallyEqual(instance1, instance2)) {
+            return TaskEquality.LEXICALLY_EQUAL;
+        }
+        else {
+            return TaskEquality.UNEQUAL;
+        }
+    }
+
+    /**
+     * <p>
+     * compares two tasks with each other by calling the rule manager. If the rule manager returns
+     * identity, then the returned equality is set to lexically equal. The reason is, that
+     * the children of the optionals are compared and that therefore the distinct optionals
+     * can be at most lexically equal.
+     * </p>
+     * 
+     * @param child1 the first task to be compared
+     * @param child2 the second task to be compared
+     * 
+     * @return the determined equality being at most lexical equality.
+     */
+    private TaskEquality getNodeEquality(ITask child1, ITask child2) {
+        TaskEquality taskEquality = callRuleManager(child1, child2, null);
+
+        if (taskEquality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)) {
+            // prevent, that identical is returned, because the optionals itself are not identical
+            // although the optional tasks are
+            if (taskEquality == TaskEquality.IDENTICAL) {
+                return TaskEquality.LEXICALLY_EQUAL;
+            }
+            else {
+                return taskEquality;
+            }
+        }
+        
+        return TaskEquality.UNEQUAL;
+    }
+
+    /**
+     * <p>
+     * compares two tasks. One of them must be a selection, the other one can be any task.
+     * The method returns a task equality that is not <code>NodeEquality.UNEQUAL</code>
+     * if the other task is at least semantically equal to the children of the selection. It
+     * returns more concrete equalities, if the equality between the other task and the children
+     * of the selection is more concrete.
+     * </p> 
+     * 
+     * @param task1 the first task to compare
+     * @param task2 the second task to compare
+     * 
+     * @return as described
+     */
+    private TaskEquality selectionChildrenSemanticallyEqualNode(ITask task1, ITask task2) {
+        ISelection selection = null;
+        ITask task = null;
+        if (task1 instanceof ISelection) {
+            selection = (ISelection) task1;
+            task = task2;
+        }
+        else if (task2 instanceof ISelection) {
+            selection = (ISelection) task2;
+            task = task1;
+        }
+        else {
+            return TaskEquality.UNEQUAL;
+        }
+
+        // Iterations, where one has a selection and the other one not can at most be syntactically
+        // equal but not identical
+        TaskEquality commonDenominatorForAllComparisons = TaskEquality.SYNTACTICALLY_EQUAL;
+
+        for (ITask child : selection.getChildren()) {
+            TaskEquality taskEquality =
+                  callRuleManager(task, child, commonDenominatorForAllComparisons);
+
+            if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) {
+                return TaskEquality.UNEQUAL;
+            }
+            
+            commonDenominatorForAllComparisons =
+                commonDenominatorForAllComparisons.getCommonDenominator(taskEquality);
+        }
+
+        return commonDenominatorForAllComparisons;
+    }
+
+    /**
+     * <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 child1                the first task to be compared
+     * @param child2                the second task to be compared
+     * @param requiredEqualityLevel the equality level to be checked for
+     * 
+     * @return the determined equality
+     */
+    private TaskEquality callRuleManager(ITask        child1,
+                                         ITask        child2,
+                                         TaskEquality requiredEqualityLevel)
+    {
+        if (requiredEqualityLevel == null) {
+            return TaskEqualityRuleManager.getInstance().compare(child1, child2);
+        }
+        else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual
+                    (child1, child2, requiredEqualityLevel))
+        {
+            return requiredEqualityLevel;
+        }
+        else {
+            return TaskEquality.UNEQUAL;
+        }
+    }
+    
+    /**
+     * <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;
+        }
+    }
+}
Index: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/SequenceComparisonRule.java
===================================================================
--- trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/SequenceComparisonRule.java	(revision 1855)
+++ trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/SequenceComparisonRule.java	(revision 1887)
@@ -147,10 +147,14 @@
         }
 
-        TaskEquality resultingEquality = TaskEquality.LEXICALLY_EQUAL;
+        TaskEquality resultingEquality = requiredEqualityLevel != null ?
+            requiredEqualityLevel : TaskEquality.LEXICALLY_EQUAL;
+        
         for (int i = 0; i < children1.size(); i++) {
             ITask child1 = children1.get(i);
             ITask child2 = children2.get(i);
 
-            TaskEquality taskEquality = callRuleManager(child1, child2, requiredEqualityLevel);
+            // it is sufficient to check for the current at most achievable equality, i.e.,
+            // the so far resulting equality
+            TaskEquality taskEquality = callRuleManager(child1, child2, resultingEquality);
 
             if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) {
@@ -224,10 +228,14 @@
         }
 
-        TaskEquality resultingEquality = TaskEquality.LEXICALLY_EQUAL;
+        TaskEquality resultingEquality = requiredEqualityLevel != null ?
+            requiredEqualityLevel : TaskEquality.LEXICALLY_EQUAL;
+        
         for (int i = 0; i < sequence1.size(); i++) {
             ITaskInstance child1 = sequence1.get(i);
             ITaskInstance child2 = sequence2.get(i);
 
-            TaskEquality taskEquality = callRuleManager(child1, child2, requiredEqualityLevel);
+            // it is sufficient to check for the current at most achievable equality, i.e.,
+            // the so far resulting equality
+            TaskEquality taskEquality = callRuleManager(child1, child2, resultingEquality);
 
             if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) {
Index: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/TaskAndOptionalComparisonRule.java
===================================================================
--- trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/TaskAndOptionalComparisonRule.java	(revision 1887)
+++ trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/TaskAndOptionalComparisonRule.java	(revision 1887)
@@ -0,0 +1,309 @@
+//   Copyright 2012 Georg-August-Universität Göttingen, Germany
+//
+//   Licensed under the Apache License, Version 2.0 (the "License");
+//   you may not use this file except in compliance with the License.
+//   You may obtain a copy of the License at
+//
+//       http://www.apache.org/licenses/LICENSE-2.0
+//
+//   Unless required by applicable law or agreed to in writing, software
+//   distributed under the License is distributed on an "AS IS" BASIS,
+//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//   See the License for the specific language governing permissions and
+//   limitations under the License.
+
+package de.ugoe.cs.autoquest.tasktrees.taskequality;
+
+import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
+import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance;
+import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
+import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
+
+/**
+ * <p>
+ * This class is capable of comparing any task which is not an optional with an
+ * optional. This is needed, because optionals may mark exactly that task as optional. In this
+ * case, the optional would be equal to that task if it was executed. The rule
+ * returns lexically equal, it the child of the optional is lexically equal to the task
+ * or if the child of the optional is a selection and this selections contains a lexically equal
+ * task. The same applies for syntactical and semantical equality.
+ * </p>
+
+ * @author Patrick Harms
+ */
+public class TaskAndOptionalComparisonRule implements TaskComparisonRule {
+    
+    /* (non-Javadoc)
+     * @see TaskComparisonRule#isApplicable(ITask, ITask)
+     */
+    @Override
+    public boolean isApplicable(ITask task1, ITask task2) {
+        return ((task1 instanceof IOptional) && (!(task2 instanceof IOptional))) ||
+               ((task2 instanceof IOptional) && (!(task1 instanceof IOptional)));
+    }
+
+    /* (non-Javadoc)
+     * @see TaskComparisonRule#areLexicallyEqual(ITask, ITask)
+     */
+    @Override
+    public boolean areLexicallyEqual(ITask task1, ITask task2) {
+        TaskEquality equality = getEquality(task1, task2, TaskEquality.LEXICALLY_EQUAL);
+        return (equality != null) && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL));
+    }
+
+    /* (non-Javadoc)
+     * @see TaskComparisonRule#areSyntacticallyEqual(ITask, ITask)
+     */
+    @Override
+    public boolean areSyntacticallyEqual(ITask task1, ITask task2) {
+        TaskEquality equality = getEquality(task1, task2, TaskEquality.SYNTACTICALLY_EQUAL);
+        return (equality != null) && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL));
+    }
+
+    /* (non-Javadoc)
+     * @see TaskComparisonRule#areSemanticallyEqual(ITask, ITask)
+     */
+    @Override
+    public boolean areSemanticallyEqual(ITask task1, ITask task2) {
+        TaskEquality equality = getEquality(task1, task2, TaskEquality.SEMANTICALLY_EQUAL);
+        return (equality != null) && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL));
+    }
+
+    /* (non-Javadoc)
+     * @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);
+    }
+
+    /**
+     * <p>
+     * compares two tasks with each other checking for the provided required level of
+     * equality. One of the tasks must be an optional, the other one not. If this is not the
+     * case, the method returns null. The returned equality level is at most lexical equality
+     * as the optional can not be identical to something not being an optional.
+     * </p>
+     * 
+     * @param task1                 the first task to be compared
+     * @param task2                 the second task to be compared
+     * @param requiredEqualityLevel the equality level to be checked for
+     * 
+     * @return the determined equality.
+     */
+    private TaskEquality getEquality(ITask task1, ITask task2, TaskEquality requiredEqualityLevel) {
+        IOptional optional = null;
+        ITask task = null;
+        
+        if (task1 instanceof IOptional) {
+            if (task2 instanceof IOptional) {
+                // the rule is not responsible for two optionals
+                return null;
+            }
+            
+            optional = (IOptional) task1;
+            task = task2;
+        }
+        else if (task2 instanceof IOptional) {
+            if (task1 instanceof IOptional) {
+                // the rule is not responsible for two optionals
+                return null;
+            }
+            
+            optional = (IOptional) task2;
+            task = task1;
+        }
+        else {
+            return null;
+        }
+
+        ITask child = optional.getMarkedTask();
+        
+        // now, that we found the optional and the task, lets compare the child of the optional
+        // with the task.
+        if (child == null) {
+            return null;
+        }
+
+        TaskEquality taskEquality = callRuleManager(child, task, requiredEqualityLevel);
+
+        // although the subtask may be identical to the task, we can not return identical, as
+        // the optional is not identical to the task, but at most lexically equal
+        if (taskEquality == TaskEquality.IDENTICAL) {
+            return TaskEquality.LEXICALLY_EQUAL;
+        }
+        else {
+            return taskEquality;
+        }
+
+    }
+    
+    /**
+     * <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 child1                the first task to be compared
+     * @param child2                the second task to be compared
+     * @param requiredEqualityLevel the equality level to be checked for
+     * 
+     * @return the determined equality
+     */
+    private TaskEquality callRuleManager(ITask        child1,
+                                         ITask        child2,
+                                         TaskEquality requiredEqualityLevel)
+    {
+        if (requiredEqualityLevel == null) {
+            return TaskEqualityRuleManager.getInstance().compare(child1, child2);
+        }
+        else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual
+                     (child1, child2, requiredEqualityLevel))
+        {
+            return requiredEqualityLevel;
+        }
+        else {
+            return TaskEquality.UNEQUAL;
+        }
+    }
+
+    /**
+     * <p>
+     * compares two task instances with each other checking for the provided required level of
+     * equality. One of the task instances must be an optional, the other one not. If this is not
+     * the case, the method returns null. The returned equality level is at most lexical equality
+     * as the optional can not be identical to something not being a optional.
+     * </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)
+    {
+        IOptionalInstance optional = null;
+        ITaskInstance task = null;
+        
+        if (taskInstance1 instanceof IOptionalInstance) {
+            if (taskInstance2 instanceof IOptionalInstance) {
+                // the rule is not responsible for two iterations
+                return null;
+            }
+            
+            optional = (IOptionalInstance) taskInstance1;
+            task = taskInstance2;
+        }
+        else if (taskInstance2 instanceof IOptionalInstance) {
+            if (taskInstance1 instanceof IOptionalInstance) {
+                // the rule is not responsible for two iterations
+                return null;
+            }
+            
+            optional = (IOptionalInstance) taskInstance2;
+            task = taskInstance1;
+        }
+        else {
+            return null;
+        }
+
+        // now, that we found the optional and the task, lets compare the children of the optional
+        // with the task.
+        
+        if (optional.getChild() == null) {
+            return null;
+        }
+
+        TaskEquality taskEquality =
+            callRuleManager(optional.getChild(), task, requiredEqualityLevel);
+            
+        // although the subtask may be identical to the task, we can not return identical, as
+        // the optional is not identical to the task, but at most lexically equal
+        if (taskEquality == TaskEquality.IDENTICAL) {
+            return TaskEquality.LEXICALLY_EQUAL;
+        }
+        else {
+            return taskEquality;
+        }
+
+    }
+    
+    /**
+     * <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;
+        }
+    }
+}
Index: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/TaskEquality.java
===================================================================
--- trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/TaskEquality.java	(revision 1855)
+++ trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/TaskEquality.java	(revision 1887)
@@ -35,8 +35,8 @@
  * <p>
  * Tasks are lexically equal, if they represent the same events on a key stroke level to be
- * carried out to execute the task. Identical tasks are also syntactically equal.
+ * carried out to execute the task. Identical tasks are also lexically equal.
  * </p>
  * <p>
- * Nodes are syntactically equal, if they differ in their events on key stroke level, but the
+ * Tasks are syntactically equal, if they differ in their events on key stroke level, but the
  * syntactical result is the same. For example, entering the text "hello" into a text field can
  * be done by entering the letters in their correct order, but also by copying the text into the
Index: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/TaskEqualityRuleManager.java
===================================================================
--- trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/TaskEqualityRuleManager.java	(revision 1855)
+++ trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/TaskEqualityRuleManager.java	(revision 1887)
@@ -58,7 +58,9 @@
         mRuleIndex.add(new EventTaskComparisonRule());
         mRuleIndex.add(new IterationComparisonRule());
+        mRuleIndex.add(new OptionalComparisonRule());
         mRuleIndex.add(new SequenceComparisonRule());
         mRuleIndex.add(new SelectionComparisonRule());
         mRuleIndex.add(new TaskAndIterationComparisonRule());
+        mRuleIndex.add(new TaskAndOptionalComparisonRule());
         mRuleIndex.add(new TaskAndSelectionComparisonRule());
     }
