Changeset 1933


Ignore:
Timestamp:
03/27/15 13:26:18 (9 years ago)
Author:
pharms
Message:
  • extension for detecting identical tasks, too
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/autoquest-ui-core/src/main/java/de/ugoe/cs/autoquest/commands/usability/CMDgetTaskModelSimilarity.java

    r1919 r1933  
    3131import de.ugoe.cs.autoquest.tasktrees.temporalrelation.TaskComparator; 
    3232import de.ugoe.cs.autoquest.tasktrees.temporalrelation.utils.SimilarTasks; 
     33import de.ugoe.cs.autoquest.tasktrees.temporalrelation.utils.TaskTraversal; 
    3334import de.ugoe.cs.autoquest.tasktrees.treeifc.DefaultTaskTraversingVisitor; 
    3435import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; 
    3536import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance; 
     37import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; 
     38import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; 
    3639import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; 
    3740import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; 
     
    125128            new IdentityHashMap<>(); 
    126129         
    127         Map<ISequence, Integer> index2 = new HashMap<>(); 
    128         Map<ISequence, IEventTarget> index3 = new HashMap<>(); 
     130        Map<ISequence, Integer> lengthIndex = new HashMap<>(); 
     131        Map<ISequence, IEventTarget> firstTargetIndex = new HashMap<>(); 
     132        Map<ISequence, TaskTraversal> taskTraversalIndex = new HashMap<>(); 
    129133         
    130134        final List<IEventTask> terminalNodes = new ArrayList<>(); 
     
    181185                 
    182186                compareList.add(sequence); 
    183                 index2.put(sequence, length); 
    184                 index3.put(sequence, firstTarget); 
     187                lengthIndex.put(sequence, length); 
     188                firstTargetIndex.put(sequence, firstTarget); 
     189                taskTraversalIndex.put(sequence, TaskTraversal.getTraversal(sequence, null)); 
    185190            } 
    186191        } 
     
    196201                                                     Collections.unmodifiableMap 
    197202                                                         (index1.get(model2.getKey())), 
    198                                                      Collections.unmodifiableMap(index2), 
    199                                                      Collections.unmodifiableMap(index3), 
     203                                                     Collections.unmodifiableMap(lengthIndex), 
     204                                                     Collections.unmodifiableMap(firstTargetIndex), 
     205                                                     Collections.unmodifiableMap(taskTraversalIndex), 
    200206                                                     runnables)); 
    201207            } 
     
    395401            int lowerBorder2; 
    396402            int higherBorder2; 
     403            int identicalEqualitiesOfBin = 0; 
    397404            int lexicalEqualitiesOfBin = 0; 
    398405            int syntacticalEqualitiesOfBin = 0; 
     
    410417            csvData.append(taskCounter2); 
    411418             
    412             for (int i = bins1.length - 1; i >= 0 ; i--) { 
    413                 if (i <= 0) { 
    414                     lowerBorder1 = 0; 
     419            for (int i = 0; i < bins1.length; i++) { 
     420                if (i == 0) { 
     421                    higherBorder1 = maxCoverage1; 
    415422                } 
    416423                else { 
    417                     lowerBorder1 = bins1[i - 1][0] + 1; 
    418                 } 
    419                  
    420                 higherBorder1 = bins1[i][0]; 
    421                  
    422                 int allInBin1 = countAllInBin(lowerBorder1, higherBorder1); 
     424                    higherBorder1 = bins1[i - 1][0] - 1; 
     425                } 
     426                 
     427                lowerBorder1 = bins1[i][0]; 
     428                int allInBin1 = bins1[i][1]; 
     429 
    423430                csvData.append(';'); 
    424431                csvData.append(allInBin1); 
    425432                 
     433                identicalEqualitiesOfBin = 0; 
    426434                lexicalEqualitiesOfBin = 0; 
    427435                syntacticalEqualitiesOfBin = 0; 
     
    431439                outputs[outputIndex++] = new String[] 
    432440                        { "similarities of " + bins1[i][1] + " tasks with " + lowerBorder1 + 
    433                           " to " + higherBorder1 + " covered events", "LEX", "SYN", "SEM", 
     441                          " to " + higherBorder1 + " covered events", "IDENT", "LEX", "SYN", "SEM", 
    434442                          "ALL EQUAL", "SIM", "ALL" }; 
    435443                 
    436                 for (int j = bins2.length - 1; j >= 0; j--) { 
    437                     if (j <= 0) { 
    438                         lowerBorder2 = 0; 
     444                for (int j = 0; j < bins2.length; j++) { 
     445                    if (j == 0) { 
     446                        higherBorder2 = maxCoverage2; 
    439447                    } 
    440448                    else { 
    441                         lowerBorder2 = bins2[j - 1][0] + 1; 
    442                     } 
    443                      
    444                     higherBorder2 = bins2[j][0]; 
    445                      
    446                     int allInBin2 = countAllInBin(lowerBorder2, higherBorder2); 
     449                        higherBorder2 = bins2[j - 1][0] - 1; 
     450                    } 
     451                     
     452                    lowerBorder2 = bins2[j][0]; 
     453                    int allInBin2 = bins2[j][1]; 
     454                     
    447455                    csvData.append(';'); 
    448456                    csvData.append(allInBin2); 
    449457 
    450458                    int count1 = countEqualities(lowerBorder1, higherBorder1, lowerBorder2, 
     459                                                 higherBorder2, TaskEquality.IDENTICAL); 
     460 
     461                    int count2 = countEqualities(lowerBorder1, higherBorder1, lowerBorder2, 
    451462                                                 higherBorder2, TaskEquality.LEXICALLY_EQUAL); 
    452463 
    453                     int count2 = countEqualities(lowerBorder1, higherBorder1, lowerBorder2, 
     464                    int count3 = countEqualities(lowerBorder1, higherBorder1, lowerBorder2, 
    454465                                                 higherBorder2, TaskEquality.SYNTACTICALLY_EQUAL); 
    455466 
    456                     int count3 = countEqualities(lowerBorder1, higherBorder1, lowerBorder2, 
     467                    int count4 = countEqualities(lowerBorder1, higherBorder1, lowerBorder2, 
    457468                                                 higherBorder2, TaskEquality.SEMANTICALLY_EQUAL); 
    458469                     
    459                     int count4 = countEqualities(lowerBorder1, higherBorder1, lowerBorder2, 
     470                    int count5 = countEqualities(lowerBorder1, higherBorder1, lowerBorder2, 
    460471                                                 higherBorder2, null); 
    461472                     
     
    464475                              " to " + higherBorder2 + " covered events", format(count1, allInBin1), 
    465476                              format(count2, allInBin1), format(count3, allInBin1), 
    466                               format(count1 + count2 + count3, allInBin1), format(count4, allInBin1), 
    467                               format(count1 + count2 + count3 + count4, allInBin1) 
     477                              format(count4, allInBin1), 
     478                              format(count1 + count2 + count3 + count4, allInBin1), 
     479                              format(count5, allInBin1), 
     480                              format(count1 + count2 + count3 + count4 + count5, allInBin1) 
    468481                            }; 
    469482                     
    470                     lexicalEqualitiesOfBin += count1; 
    471                     syntacticalEqualitiesOfBin += count2; 
    472                     semanticalEqualitiesOfBin += count3; 
    473                     similaritiesOfBin += count4; 
     483                    identicalEqualitiesOfBin += count1; 
     484                    lexicalEqualitiesOfBin += count2; 
     485                    syntacticalEqualitiesOfBin += count3; 
     486                    semanticalEqualitiesOfBin += count4; 
     487                    similaritiesOfBin += count5; 
    474488 
    475489                    csvData.append(';'); 
     
    480494                    csvData.append(percentFormat.format((double) count3 / allInBin1)); 
    481495                    csvData.append(';'); 
    482                     csvData.append(percentFormat.format 
    483                                        ((double) (count1 + count2 + count3) / allInBin1)); 
    484                     csvData.append(';'); 
    485496                    csvData.append(percentFormat.format((double) count4 / allInBin1)); 
    486497                    csvData.append(';'); 
    487498                    csvData.append(percentFormat.format 
    488499                                       ((double) (count1 + count2 + count3 + count4) / allInBin1)); 
     500                    csvData.append(';'); 
     501                    csvData.append(percentFormat.format((double) count5 / allInBin1)); 
     502                    csvData.append(';'); 
     503                    csvData.append(percentFormat.format 
     504                                       ((double) (count1 + count2 + count3 + count4 + count5) / allInBin1)); 
    489505                } 
    490506                 
    491507                outputs[outputIndex++] = new String[] 
    492                         { "--> all recalls", format(lexicalEqualitiesOfBin, allInBin1), 
     508                        { "--> all recalls", format(identicalEqualitiesOfBin, allInBin1), 
     509                          format(lexicalEqualitiesOfBin, allInBin1), 
    493510                          format(syntacticalEqualitiesOfBin, allInBin1), 
    494511                          format(semanticalEqualitiesOfBin, allInBin1), 
    495                           format(lexicalEqualitiesOfBin + syntacticalEqualitiesOfBin + 
    496                                  semanticalEqualitiesOfBin, allInBin1), 
     512                          format(identicalEqualitiesOfBin + lexicalEqualitiesOfBin + 
     513                                 syntacticalEqualitiesOfBin + semanticalEqualitiesOfBin, allInBin1), 
    497514                          format(similaritiesOfBin, allInBin1), 
    498                           format(lexicalEqualitiesOfBin + syntacticalEqualitiesOfBin + 
    499                                  semanticalEqualitiesOfBin + similaritiesOfBin, allInBin1) 
     515                          format(identicalEqualitiesOfBin + lexicalEqualitiesOfBin + 
     516                                 syntacticalEqualitiesOfBin + semanticalEqualitiesOfBin + 
     517                                 similaritiesOfBin, allInBin1) 
    500518                        }; 
    501519                 
    502520 
     521                csvData.append(';'); 
     522                csvData.append(percentFormat.format((double) identicalEqualitiesOfBin / allInBin1)); 
    503523                csvData.append(';'); 
    504524                csvData.append(percentFormat.format((double) lexicalEqualitiesOfBin / allInBin1)); 
     
    509529                csvData.append(';'); 
    510530                csvData.append(percentFormat.format 
    511                                    ((double) (lexicalEqualitiesOfBin + syntacticalEqualitiesOfBin + 
    512                                               semanticalEqualitiesOfBin) / allInBin1)); 
     531                                   ((double) (identicalEqualitiesOfBin + lexicalEqualitiesOfBin + 
     532                                              syntacticalEqualitiesOfBin + semanticalEqualitiesOfBin) 
     533                                              / allInBin1)); 
    513534                csvData.append(';'); 
    514535                csvData.append(percentFormat.format((double) similaritiesOfBin / allInBin1)); 
    515536                csvData.append(';'); 
    516537                csvData.append(percentFormat.format 
    517                                    ((double) (lexicalEqualitiesOfBin + syntacticalEqualitiesOfBin + 
    518                                               semanticalEqualitiesOfBin + similaritiesOfBin) / 
    519                                               allInBin1)); 
    520                  
    521                 allEqualities += lexicalEqualitiesOfBin + syntacticalEqualitiesOfBin + 
    522                     semanticalEqualitiesOfBin + similaritiesOfBin; 
     538                                   ((double) (identicalEqualitiesOfBin + lexicalEqualitiesOfBin + 
     539                                              syntacticalEqualitiesOfBin + semanticalEqualitiesOfBin + 
     540                                              similaritiesOfBin) / allInBin1)); 
     541                 
     542                allEqualities += identicalEqualitiesOfBin + lexicalEqualitiesOfBin + 
     543                    syntacticalEqualitiesOfBin + semanticalEqualitiesOfBin + similaritiesOfBin; 
    523544                 
    524545                outputIndex++; 
     
    597618             
    598619            int index = 0; 
    599              
    600             for (int i = 0; i < maxCoverage; i++) { 
     620            int allCoverageCounterSum = 0; 
     621             
     622            for (int i = maxCoverage; i > 0; i--) { 
    601623                if (result[index][1] > aimedBinSize) { 
    602                     // try to compensate, if the previous bin was a little too large 
    603                     aimedBinSize = averageBinSize + averageBinSize - result[index][1]; 
     624                    if ((index + 2) < result.length) { 
     625                        // try to compensate, if the previous bin was a little too large 
     626                        aimedBinSize = averageBinSize + averageBinSize - result[index][1]; 
     627                    } 
     628                    else { 
     629                        // put all remaining in the last bin 
     630                        aimedBinSize = Integer.MAX_VALUE; 
     631                    } 
    604632                    index++; 
    605633                } 
     
    610638                    result[index][0] = i; 
    611639                    result[index][1] += value; 
    612                 } 
     640                    allCoverageCounterSum += value; 
     641                } 
     642            } 
     643             
     644            if (taskCounter != allCoverageCounterSum) { 
     645                throw new RuntimeException("bins do not cover all tasks"); 
    613646            } 
    614647             
     
    666699            return counter; 
    667700        } 
    668  
    669         /** 
    670          * 
    671          */ 
    672         private int countAllInBin(int lowerBorder, int higherBorder) { 
    673             int coverageCounter = 0; 
    674              
    675             for (int i = lowerBorder; i <= higherBorder; i++) { 
    676                 Integer value = coverageCounters1.get(i); 
    677                  
    678                 if (value != null) { 
    679                     coverageCounter += value; 
    680                 } 
    681             } 
    682              
    683             return coverageCounter; 
    684         } 
    685701    } 
    686702 
     
    710726        /** */ 
    711727        private Map<ISequence, IEventTarget> firstTargetIndex; 
     728         
     729        /** */ 
     730        private Map<ISequence, TaskTraversal> traversalIndex; 
    712731 
    713732        /** */ 
     
    716735        /** */ 
    717736        private SimilarityStatistics statistics = new SimilarityStatistics(); 
    718          
     737 
     738        /** */ 
     739        private TaskEqualityRuleManager equalityRuleManager = TaskEqualityRuleManager.getInstance(); 
     740 
    719741        /** 
    720742         *  
     
    727749                                   Map<ISequence, Integer>                          lengthIndex2, 
    728750                                   Map<ISequence, IEventTarget>                     firstTargetIndex, 
     751                                   Map<ISequence, TaskTraversal>                    traversalIndex, 
    729752                                   Object                                           semaphore) 
    730753        { 
     
    736759            this.lengthIndex2 = lengthIndex2; 
    737760            this.firstTargetIndex = firstTargetIndex; 
     761            this.traversalIndex = traversalIndex; 
    738762            this.semaphore = semaphore; 
    739763        } 
     
    744768        @Override 
    745769        public void run() { 
    746             TaskEqualityRuleManager equalityRuleManager = TaskEqualityRuleManager.getInstance(); 
    747770            TaskComparator comparator = new TaskComparator(TaskEquality.SEMANTICALLY_EQUAL); 
    748771            TaskEquality mostCommonEquality; 
    749             ITask mostSimilarTask; 
     772            ISequence mostSimilarTask; 
    750773            TaskEquality equality; 
    751774            SimilarTasks similarity; 
     
    764787            watch.start("all comparisons "); 
    765788            int count = 0; 
     789            int taskNo = 0; 
     790            int nextPercent = 10; 
    766791            for (ISequence task : tasks1) { 
    767792                int length = lengthIndex2.get(task); 
     
    795820                mostSimilarTask = null; 
    796821                 
    797                 for (ITask taskToCompare : tasksToCompareWith) { 
     822                for (ISequence taskToCompare : tasksToCompareWith) { 
    798823                    count++; 
    799824                    watch.start("normal comparison"); 
     
    824849                 
    825850                if (mostCommonEquality != null) { 
    826                     statistics.store(task, model1, mostSimilarTask, model2, mostCommonEquality); 
     851                    // check, if both tasks are identical 
     852                    if (!identicalTasks(task, mostSimilarTask)) { 
     853                        statistics.store(task, model1, mostSimilarTask, model2, mostCommonEquality); 
     854                    } 
     855                    else { 
     856                        statistics.store 
     857                            (task, model1, mostSimilarTask, model2, TaskEquality.IDENTICAL); 
     858                    } 
    827859                } 
    828860                else { 
    829861                    int lowestDiffLevel = Integer.MAX_VALUE; 
    830                     for (ITask taskToCompare : tasksToCompareWith) { 
     862                    TaskTraversal traversal1 = traversalIndex.get(task); 
     863                     
     864                    for (ISequence taskToCompare : tasksToCompareWith) { 
    831865                        count++; 
    832866                        watch.start("similarity comparison"); 
    833                         similarity = SimilarTasks.compareTasks(task, taskToCompare, comparator); 
     867                        TaskTraversal traversal2 = traversalIndex.get(taskToCompare); 
     868                        similarity = 
     869                            SimilarTasks.compareTraversals(traversal1, traversal2, comparator); 
    834870                        watch.stop("similarity comparison"); 
    835871                         
     
    860896                    } 
    861897                } 
    862             } 
    863              
    864             System.out.println("performed " + count + " comparisons"); 
     898                 
     899                taskNo++; 
     900                 
     901                if (((taskNo * 100) / tasks1.size()) > nextPercent) { 
     902                    System.out.println("compared " + nextPercent + "% (" + taskNo + " tasks) of " + 
     903                                       model1 + " with " + tasks2.size() + " tasks of " + model2); 
     904                    nextPercent += 10; 
     905                } 
     906            } 
     907             
     908            System.out.println("performed " + count + " comparisons for comparing " + model1 + 
     909                               " with " + model2); 
    865910             
    866911            watch.stop("all comparisons "); 
     
    873918 
    874919        /** 
     920         * 
     921         */ 
     922        private boolean identicalTasks(ISequence sequence1, ISequence sequence2) { 
     923            if (sequence1.getChildren().size() != sequence2.getChildren().size()) { 
     924                return false; 
     925            } 
     926             
     927            for (int i = 0; i < sequence1.getChildren().size(); i++) { 
     928                if (!identicalTasks(sequence1.getChildren().get(i), sequence2.getChildren().get(i))) 
     929                { 
     930                    return false; 
     931                } 
     932            } 
     933             
     934            return true; 
     935        } 
     936 
     937        /** 
     938         * 
     939         */ 
     940        private boolean identicalTasks(IIteration iteration1, IIteration iteration2) { 
     941            return identicalTasks(iteration1.getMarkedTask(), iteration2.getMarkedTask()); 
     942        } 
     943 
     944        /** 
     945         * 
     946         */ 
     947        private boolean identicalTasks(IOptional optional1, IOptional optional2) { 
     948            return identicalTasks(optional1.getMarkedTask(), optional2.getMarkedTask()); 
     949        } 
     950 
     951        /** 
     952         * 
     953         */ 
     954        private boolean identicalTasks(ISelection selection1, ISelection selection2) { 
     955            if (selection1.getChildren().size() != selection2.getChildren().size()) { 
     956                return false; 
     957            } 
     958             
     959            FIRST_SELECTION_CHILDREN: 
     960            for (ITask child1 : selection1.getChildren()) { 
     961                for (ITask child2 : selection2.getChildren()) { 
     962                    if (identicalTasks(child1, child2)) { 
     963                        continue FIRST_SELECTION_CHILDREN; 
     964                    } 
     965                } 
     966                 
     967                return false; 
     968            } 
     969             
     970            return true; 
     971        } 
     972 
     973        /** 
     974         * 
     975         */ 
     976        private boolean identicalTasks(IEventTask eventTask1, IEventTask eventTask2) { 
     977            return equalityRuleManager.compare(eventTask1, eventTask2).isAtLeast 
     978                (TaskEquality.SEMANTICALLY_EQUAL); 
     979        } 
     980         
     981        /** 
     982         * 
     983         */ 
     984        private boolean identicalTasks(ITask task1, ITask task2) { 
     985            if ((task1 instanceof ISequence) && (task2 instanceof ISequence)) { 
     986                return identicalTasks((ISequence) task1, (ISequence) task2); 
     987            } 
     988            else if ((task1 instanceof IIteration) && (task2 instanceof IIteration)) { 
     989                return identicalTasks((IIteration) task1, (IIteration) task2); 
     990            } 
     991            else if ((task1 instanceof IOptional) && (task2 instanceof IOptional)) { 
     992                return identicalTasks((IOptional) task1, (IOptional) task2); 
     993            } 
     994            else if ((task1 instanceof ISelection) && (task2 instanceof ISelection)) { 
     995                return identicalTasks((ISelection) task1, (ISelection) task2); 
     996            } 
     997            else if ((task1 instanceof IEventTask) && (task2 instanceof IEventTask)) { 
     998                return identicalTasks((IEventTask) task1, (IEventTask) task2); 
     999            } 
     1000            else { 
     1001                return false; 
     1002            } 
     1003        } 
     1004 
     1005        /** 
    8751006         * @return the statistics 
    8761007         */ 
Note: See TracChangeset for help on using the changeset viewer.