Ignore:
Timestamp:
08/14/13 17:04:42 (11 years ago)
Author:
pharms
Message:
  • rework of task model to move event instance stuff to task instances
  • introduction of sequence, selection, iteration and optional instances
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/SelectionComparisonRule.java

    r1190 r1294  
    1818 
    1919import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; 
     20import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; 
    2021import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; 
     22import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; 
    2123 
    2224/** 
     
    3941 
    4042    /* (non-Javadoc) 
    41      * @see NodeComparisonRule#isApplicable(ITask, ITask) 
     43     * @see TaskComparisonRule#isApplicable(ITask, ITask) 
    4244     */ 
    4345    @Override 
     
    4749 
    4850    /* (non-Javadoc) 
    49      * @see NodeComparisonRule#areLexicallyEqual(ITask, ITask) 
     51     * @see TaskComparisonRule#areLexicallyEqual(ITask, ITask) 
    5052     */ 
    5153    @Override 
     
    5658 
    5759    /* (non-Javadoc) 
    58      * @see NodeComparisonRule#areSyntacticallyEqual(ITask, ITask) 
     60     * @see TaskComparisonRule#areSyntacticallyEqual(ITask, ITask) 
    5961     */ 
    6062    @Override 
     
    6567 
    6668    /* (non-Javadoc) 
    67      * @see NodeComparisonRule#areSemanticallyEqual(ITask, ITask) 
     69     * @see TaskComparisonRule#areSemanticallyEqual(ITask, ITask) 
    6870     */ 
    6971    @Override 
     
    7476 
    7577    /* (non-Javadoc) 
    76      * @see NodeComparisonRule#compare(ITask, ITask) 
     78     * @see TaskComparisonRule#compare(ITask, ITask) 
    7779     */ 
    7880    @Override 
    7981    public TaskEquality compare(ITask task1, ITask task2) { 
    8082        return getEquality(task1, task2, null); 
     83    } 
     84 
     85    /* (non-Javadoc) 
     86     * @see TaskComparisonRule#isApplicable(ITaskInstance, ITaskInstance) 
     87     */ 
     88    @Override 
     89    public boolean isApplicable(ITaskInstance instance1, ITaskInstance instance2) { 
     90        return isApplicable(instance1.getTask(), instance2.getTask()); 
     91    } 
     92 
     93    /* (non-Javadoc) 
     94     * @see TaskComparisonRule#areLexicallyEqual(ITaskInstance, ITaskInstance) 
     95     */ 
     96    @Override 
     97    public boolean areLexicallyEqual(ITaskInstance instance1, ITaskInstance instance2) { 
     98        TaskEquality equality = getEquality(instance1, instance2, TaskEquality.LEXICALLY_EQUAL); 
     99        return (equality != null) && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL)); 
     100    } 
     101 
     102    /* (non-Javadoc) 
     103     * @see TaskComparisonRule#areSyntacticallyEqual(ITaskInstance, ITaskInstance) 
     104     */ 
     105    @Override 
     106    public boolean areSyntacticallyEqual(ITaskInstance instance1, ITaskInstance instance2) { 
     107        TaskEquality equality = getEquality(instance1, instance2, TaskEquality.SYNTACTICALLY_EQUAL); 
     108        return (equality != null) && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL)); 
     109    } 
     110 
     111    /* (non-Javadoc) 
     112     * @see TaskComparisonRule#areSemanticallyEqual(ITaskInstance, ITaskInstance) 
     113     */ 
     114    @Override 
     115    public boolean areSemanticallyEqual(ITaskInstance instance1, ITaskInstance instance2) { 
     116        TaskEquality equality = getEquality(instance1, instance2, TaskEquality.SEMANTICALLY_EQUAL); 
     117        return (equality != null) && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)); 
     118    } 
     119 
     120    /* (non-Javadoc) 
     121     * @see TaskComparisonRule#compare(ITaskInstance, ITaskInstance) 
     122     */ 
     123    @Override 
     124    public TaskEquality compare(ITaskInstance instance1, ITaskInstance instance2) { 
     125        return getEquality(instance1, instance2, null); 
    81126    } 
    82127 
     
    107152        } 
    108153 
    109         TaskEquality selectionEquality; 
    110  
    111154        if (requiredEqualityLevel == null) { 
    112155            // calculate the common equality level for all children of both selections. 
    113156            // do it in both directions to ensure commutative comparison 
    114             selectionEquality = getCommonEqualityLevel(children1, children2); 
    115             if (selectionEquality != TaskEquality.UNEQUAL) { 
    116                 return selectionEquality.getCommonDenominator 
    117                     (getCommonEqualityLevel(children2, children1)); 
    118             } 
    119             else { 
    120                 return TaskEquality.UNEQUAL; 
    121             } 
     157            return getMostConcreteEqualityLevel(children1, children2); 
    122158        } 
    123159        else { 
    124160            // we are searching for a specific equality 
    125             if (checkEqualityLevel(children1, children2, requiredEqualityLevel) && 
    126                 checkEqualityLevel(children2, children1, requiredEqualityLevel)) 
    127             { 
     161            if (checkEqualityLevel(children1, children2, requiredEqualityLevel)) { 
    128162                return requiredEqualityLevel; 
    129163            } 
     
    136170    /** 
    137171     * <p> 
    138      * determines the common equality level for all tasks in the first list compared to all 
    139      * tasks in the second list. If for one task in the first list, there is no equal task in the 
    140      * second list, the method return unequality.  
     172     * determines the most concrete equality level for all tasks in the first list compared to all 
     173     * tasks in the second list. It is sufficient, if there is one task in one list for which there 
     174     * exist an equal task in the other list. 
    141175     * </p> 
    142176     * 
     
    144178     * @param children2 the second list to be compared 
    145179     *  
    146      * @return the common task equality identified for all tasks in the first list with respect to 
    147      *         the second list 
    148      */ 
    149     private TaskEquality getCommonEqualityLevel(List<ITask> children1, List<ITask> children2) { 
    150         TaskEquality listEquality = TaskEquality.LEXICALLY_EQUAL; 
    151          
     180     * @return the most concrete task equality identified for all tasks in the first list with 
     181     *         respect to the second list 
     182     */ 
     183    private TaskEquality getMostConcreteEqualityLevel(List<ITask> children1, List<ITask> children2) 
     184    { 
    152185        TaskEquality childEquality; 
    153186        TaskEquality currentEquality; 
     
    164197                    } 
    165198                     
    166                     if (childEquality == TaskEquality.SEMANTICALLY_EQUAL) { 
    167                         // as we calculate only the common denominator, we can break up here for 
    168                         // the current child. We will not improve the denominator anymore 
    169                         break; 
     199                    if (childEquality.isAtLeast(TaskEquality.LEXICALLY_EQUAL)) { 
     200                        // as we calculate the most concrete equality, we can break up here 
     201                        return TaskEquality.LEXICALLY_EQUAL; 
    170202                    } 
    171203                } 
    172204            } 
    173              
    174             if (childEquality == null) { 
    175                 // we did not find any child in the second list, that is equal to the searched 
    176                 // child 
    177                 return TaskEquality.UNEQUAL; 
    178             } 
    179             else { 
    180                 listEquality = listEquality.getCommonDenominator(childEquality); 
    181             } 
    182         } 
    183  
    184         return listEquality; 
    185     } 
    186  
    187     /** 
    188      * <p> 
    189      * ensures for the two given lists, that for each task in the first list there is a task 
     205        } 
     206 
     207        // as the comparison should be commutative, we do not need to check, if in list 2 there is 
     208        // a child equal to one in list 1 
     209        return TaskEquality.UNEQUAL; 
     210    } 
     211 
     212    /** 
     213     * <p> 
     214     * ensures for the two given lists, that for at least one task in the first list there is a task 
    190215     * in the second list being on the given level equal to the task in the first list. 
    191216     * </p>  
     
    195220     * @param requiredEqualityLevel the equality level to be checked for 
    196221     *  
    197      * @return true if each task in the first list has an equal task in the second list when 
    198      *         considering the given equality level, false else. 
     222     * @return true if there is a task in the first list that has an equal task in the second list 
     223     *         when considering the given equality level, false else. 
    199224     */ 
    200225    private boolean checkEqualityLevel(List<ITask>  children1, 
     
    202227                                       TaskEquality requiredEqualityLevel) 
    203228    { 
    204         TaskEquality childEquality; 
    205229        TaskEquality currentEquality; 
    206230        for (ITask child1 : children1) { 
    207             childEquality = null; 
    208231            for (ITask child2 : children2) { 
    209232                currentEquality = callRuleManager(child1, child2, requiredEqualityLevel); 
     
    212235                    // we found at least one equal child with sufficient equality in the 
    213236                    // second list. So be can break up for this child. 
    214                     childEquality = currentEquality; 
    215                     break; 
     237                    return true; 
    216238                } 
    217239            } 
    218              
    219             if (childEquality == null) { 
    220                 // we did not find any child in the second list, that is equal to the searched 
    221                 // child 
    222                 return false; 
    223             } 
    224         } 
    225  
    226         // for all children, we found an equality  
    227         return true; 
     240        } 
     241 
     242        return false; 
    228243    } 
    229244 
     
    259274    } 
    260275 
     276    /** 
     277     * <p> 
     278     * compares two selection instances with each other checking for the provided required level of 
     279     * equality. If this level is ensured, the method immediately returns. The more concrete 
     280     * the required equality level, the more checks this method performs. 
     281     * </p> 
     282     *  
     283     * @param taskInstance1         the first task instance to be compared 
     284     * @param taskInstance2         the second task instance to be compared 
     285     * @param requiredEqualityLevel the equality level to be checked for 
     286     *  
     287     * @return the determined equality. 
     288     */ 
     289    private TaskEquality getEquality(ITaskInstance taskInstance1, 
     290                                     ITaskInstance taskInstance2, 
     291                                     TaskEquality  requiredEqualityLevel) 
     292    { 
     293        ITaskInstance child1 = ((ISelectionInstance) taskInstance1).getChild(); 
     294        ITaskInstance child2 = ((ISelectionInstance) taskInstance2).getChild(); 
     295 
     296        // if both selections do not have children, they are lexically equal. If only one of them 
     297        // has children, they are unequal. 
     298        if ((child1 == null) && (child2 == null)) { 
     299            return TaskEquality.LEXICALLY_EQUAL; 
     300        } 
     301        else if ((child1 == null) || (child2 == null)) { 
     302            return TaskEquality.UNEQUAL; 
     303        } 
     304 
     305        TaskEquality equality = callRuleManager(child1, child2, requiredEqualityLevel); 
     306         
     307        if (equality == TaskEquality.IDENTICAL) { 
     308            // two different selection instances can be at most lexically equal even if their 
     309            // children are identical 
     310            return TaskEquality.LEXICALLY_EQUAL; 
     311        } 
     312        else { 
     313            return equality; 
     314        } 
     315    } 
     316 
     317    /** 
     318     * <p> 
     319     * used to to call the task equality rule manager for the comparison of the two provided 
     320     * children. If no required equality level is provided, than the most concrete equality is 
     321     * returned. Otherwise, the required equality is returned as long as the children are equal 
     322     * on that level. 
     323     * </p>  
     324     *  
     325     * @param taskInstance1         the first task instance to be compared 
     326     * @param taskInstance2         the second task instance to be compared 
     327     * @param requiredEqualityLevel the equality level to be checked for 
     328     *  
     329     * @return the determined equality 
     330     */ 
     331    private TaskEquality callRuleManager(ITaskInstance taskInstance1, 
     332                                         ITaskInstance taskInstance2, 
     333                                         TaskEquality  requiredEqualityLevel) 
     334    { 
     335        if (requiredEqualityLevel == null) { 
     336            return TaskEqualityRuleManager.getInstance().compare(taskInstance1, taskInstance2); 
     337        } 
     338        else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual 
     339                     (taskInstance1, taskInstance2, requiredEqualityLevel)) 
     340        { 
     341            return requiredEqualityLevel; 
     342        } 
     343        else { 
     344            return TaskEquality.UNEQUAL; 
     345        } 
     346    } 
    261347} 
Note: See TracChangeset for help on using the changeset viewer.