Ignore:
Timestamp:
07/30/13 09:44:39 (11 years ago)
Author:
pharms
Message:
  • improved performance of task instance trie generation by using different symbol management strategies while creating the trie. This performance improvement is significant and allows to detect tasks now in a much faster manner.
File:
1 edited

Legend:

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

    r1256 r1285  
    1515package de.ugoe.cs.autoquest.tasktrees.temporalrelation; 
    1616 
     17import java.util.HashMap; 
     18import java.util.HashSet; 
    1719import java.util.Iterator; 
    1820import java.util.LinkedList; 
    1921import java.util.List; 
     22import java.util.Map; 
     23import java.util.Set; 
     24import java.util.logging.Level; 
    2025 
    2126import de.ugoe.cs.autoquest.tasktrees.taskequality.TaskEquality; 
     
    3237import de.ugoe.cs.autoquest.usageprofiles.TrieProcessor; 
    3338import de.ugoe.cs.util.StopWatch; 
     39import de.ugoe.cs.util.console.Console; 
    3440 
    3541/** 
     
    5965    /** 
    6066     * <p> 
    61      * the task comparator to be used for comparing tasks 
     67     * the task comparator to be used for comparing tasks for preparation 
    6268     * </p> 
    6369     */ 
    64     private TaskComparator taskComparator; 
    65  
     70    private TaskHandlingStrategy preparationTaskHandlingStrategy; 
     71     
     72    /** 
     73     * <p> 
     74     * the task comparator to be used for comparing tasks during iteration detection an trie 
     75     * generation 
     76     * </p> 
     77     */ 
     78    private TaskHandlingStrategy identityTaskHandlingStrategy;; 
     79     
    6680    /** 
    6781     * <p> 
     
    7791        this.taskBuilder = taskBuilder; 
    7892         
    79         this.taskComparator = new TaskComparator(minimalTaskEquality); 
     93        this.preparationTaskHandlingStrategy = new TaskHandlingStrategy(minimalTaskEquality); 
     94        this.identityTaskHandlingStrategy = new TaskHandlingStrategy(TaskEquality.IDENTICAL); 
    8095    } 
    8196 
     
    139154     */ 
    140155    private void harmonizeEventTaskInstancesModel(RuleApplicationData appData) { 
    141         System.out.print("harmonizing task model of event task instances"); 
     156        Console.traceln(Level.INFO, "harmonizing task model of event task instances"); 
    142157        appData.getStopWatch().start("harmonizing event tasks"); 
    143          
    144         SymbolMap<ITaskInstance, ITask> unifiedTaskMap = 
    145             new SymbolMap<ITaskInstance, ITask>(taskComparator); 
     158 
     159        SymbolMap<ITaskInstance, ITask> uniqueTasks = 
     160            preparationTaskHandlingStrategy.createSymbolMap(); 
     161        TaskComparator comparator = preparationTaskHandlingStrategy.getTaskComparator(); 
    146162         
    147163        int unifiedTasks = 0; 
    148          
     164        ITask task; 
    149165        List<IUserSession> sessions = appData.getSessions(); 
    150166        for (IUserSession session : sessions) { 
     167            Console.traceln(Level.FINE, "handling session " + session); 
    151168            for (ITaskInstance taskInstance : session) { 
    152                 ITask task = unifiedTaskMap.getValue(taskInstance); 
     169                task = uniqueTasks.getValue(taskInstance); 
    153170                 
    154171                if (task == null) { 
    155                     unifiedTaskMap.addSymbol(taskInstance, taskInstance.getTask()); 
     172                    uniqueTasks.addSymbol(taskInstance, taskInstance.getTask()); 
    156173                } 
    157174                else { 
     
    160177                } 
    161178            } 
    162         } 
    163          
     179             
     180            comparator.clearBuffers(); 
     181        } 
    164182         
    165183        appData.getStopWatch().stop("harmonizing event tasks"); 
    166         System.out.println(" --> harmonized " + unifiedTasks + " task occurrences (still " + 
    167                            unifiedTaskMap.size() + " different tasks)"); 
    168          
     184        Console.traceln(Level.INFO, "harmonized " + unifiedTasks + " task occurrences (still " + 
     185                        uniqueTasks.size() + " different tasks)"); 
     186 
    169187        appData.getStopWatch().dumpStatistics(System.out); 
     188        appData.getStopWatch().reset(); 
    170189    } 
    171190 
     
    174193     */ 
    175194    private void detectAndReplaceIterations(RuleApplicationData appData) { 
    176         System.out.print("detecting iterations"); 
     195        Console.traceln(Level.FINE, "detecting iterations"); 
    177196        appData.getStopWatch().start("detecting iterations"); 
    178197         
    179198        List<IUserSession> sessions = appData.getSessions(); 
    180         int iteratedTasks = 0; 
    181          
    182         ITask iteratedTask = null; 
    183          
    184         do { 
    185             iteratedTask = searchIteratedTask(sessions); 
    186              
    187             if (iteratedTask != null) { 
    188                 replaceIterationsOf(iteratedTask, sessions, appData); 
    189                 iteratedTasks++; 
    190             } 
    191         } 
    192         while (iteratedTask != null); 
     199         
     200        Set<ITask> iteratedTasks = searchIteratedTasks(sessions); 
     201         
     202        if (iteratedTasks.size() > 0) { 
     203            replaceIterationsOf(iteratedTasks, sessions, appData); 
     204        } 
    193205         
    194206        appData.getStopWatch().stop("detecting iterations"); 
    195         System.out.println(" --> found " + iteratedTasks + " iterated tasks"); 
     207        Console.traceln(Level.INFO, "replaced " + iteratedTasks.size() + " iterated tasks"); 
    196208    } 
    197209 
     
    199211     * 
    200212     */ 
    201     private ITask searchIteratedTask(List<IUserSession> sessions) { 
     213    private Set<ITask> searchIteratedTasks(List<IUserSession> sessions) { 
     214        Set<ITask> iteratedTasks = new HashSet<ITask>(); 
    202215        for (IUserSession session : sessions) { 
    203216            for (int i = 0; i < (session.size() - 1); i++) { 
    204                 if (taskComparator.equals(session.get(i), session.get(i + 1))) { 
    205                     return session.get(i).getTask(); 
    206                 } 
    207             } 
    208         } 
    209          
    210         return null; 
     217                // we prepared the task instances to refer to unique tasks, if they are treated 
     218                // as equal. Therefore, we just compare the identity of the tasks of the task 
     219                // instances 
     220                if (session.get(i).getTask() == session.get(i + 1).getTask()) { 
     221                    iteratedTasks.add(session.get(i).getTask()); 
     222                } 
     223            } 
     224        } 
     225         
     226        return iteratedTasks; 
    211227    } 
    212228 
     
    214230     * 
    215231     */ 
    216 /*    private ITask searchIteratedTask(List<IUserSession> sessions) { 
    217         int minNoOfRepetitions = 2; 
    218         int minNoOfIterationOccurrences = 1; 
    219          
    220         Map<ITask, Integer> iterationsCounter = new HashMap<ITask, Integer>(); 
    221          
    222         for (IUserSession session : sessions) { 
    223             for (int i = 0; i < (session.size() - minNoOfRepetitions + 1); i++) { 
    224                 ITask task = session.get(i).getTask(); 
    225  
    226                 // check if the task is iterated 
    227                 boolean isIterated = true; 
    228                 for (int j = i + 1; j < i + minNoOfRepetitions; j++) { 
    229                     if (!taskComparator.equals(task, session.get(j).getTask())) { 
    230                         isIterated = false; 
    231                         break; 
    232                     } 
    233                 } 
    234                  
    235                 if (isIterated) { 
    236                     Integer currentCounter = null; 
    237                      
    238                     for (Map.Entry<ITask, Integer> entry : iterationsCounter.entrySet()) { 
    239                         if (taskComparator.equals(task, entry.getKey())) { 
    240                             currentCounter = entry.getValue(); 
    241                             break; 
    242                         } 
    243                     } 
    244                      
    245                     if (currentCounter == null) { 
    246                         currentCounter = 1; 
    247                         iterationsCounter.put(task, currentCounter); 
    248                     } 
    249                     else { 
    250                         currentCounter++; 
    251                         iterationsCounter.put(task, currentCounter); 
    252                     } 
    253                      
    254                     if (currentCounter >= minNoOfIterationOccurrences) { 
    255                         return task; 
    256                     } 
    257                 } 
    258             } 
    259         } 
    260          
    261         return null; 
    262     }*/ 
    263  
    264     /** 
    265      * 
    266      */ 
    267     private void replaceIterationsOf(ITask               iteratedTask, 
     232    private void replaceIterationsOf(Set<ITask>          iteratedTasks, 
    268233                                     List<IUserSession>  sessions, 
    269234                                     RuleApplicationData appData) 
    270235    { 
    271         IIteration iteration = taskFactory.createNewIteration(); 
     236        Map<ITask, IIteration> iterations = new HashMap<ITask, IIteration>(); 
     237        Map<IIteration, List<ITaskInstance>> iterationInstances = 
     238                new HashMap<IIteration, List<ITaskInstance>>(); 
     239             
     240        for (ITask iteratedTask : iteratedTasks) { 
     241            IIteration iteration = taskFactory.createNewIteration(); 
     242            iterations.put(iteratedTask, iteration); 
     243            iterationInstances.put(iteration, new LinkedList<ITaskInstance>()); 
     244        } 
     245         
    272246        ITaskInstance iterationInstance = null; 
    273          
    274         List<ITaskInstance> iterationInstances = new LinkedList<ITaskInstance>(); 
    275247         
    276248        for (IUserSession session : sessions) { 
    277249            int index = 0; 
    278250            while (index < session.size()) { 
    279                 if (taskComparator.equals(iteratedTask, session.get(index).getTask())) { 
    280                     if (iterationInstance == null) { 
     251                // we prepared the task instances to refer to unique tasks, if they are treated 
     252                // as equal. Therefore, we just compare the identity of the tasks of the task 
     253                // instances 
     254                ITask currentTask = session.get(index).getTask(); 
     255                IIteration iteration = iterations.get(currentTask); 
     256                if (iteration != null) { 
     257                    if ((iterationInstance == null) || (iterationInstance.getTask() != iteration)) 
     258                    { 
    281259                        iterationInstance = taskFactory.createNewTaskInstance(iteration); 
    282                         iterationInstances.add(iterationInstance); 
     260                        iterationInstances.get(iteration).add(iterationInstance); 
    283261                        taskBuilder.addTaskInstance(session, index, iterationInstance); 
    284262                        index++; 
     
    297275        } 
    298276         
    299         harmonizeIterationInstancesModel(iteration, iterationInstances); 
     277        for (Map.Entry<IIteration, List<ITaskInstance>> entry : iterationInstances.entrySet()) { 
     278            harmonizeIterationInstancesModel(entry.getKey(), entry.getValue()); 
     279        } 
    300280    } 
    301281 
     
    307287    { 
    308288        List<ITask> iteratedTaskVariants = new LinkedList<ITask>(); 
     289        TaskComparator comparator = preparationTaskHandlingStrategy.getTaskComparator(); 
    309290         
    310291        // merge the lexically different variants of iterated task to a unique list  
     
    315296                boolean found = false; 
    316297                for (ITask taskVariant : iteratedTaskVariants) { 
    317                     if (taskComparator.areLexicallyEqual(taskVariant, candidate)) { 
     298                    if (comparator.areLexicallyEqual(taskVariant, candidate)) { 
    318299                        taskBuilder.setTask(executionVariant, taskVariant); 
    319300                        found = true; 
     
    359340     */ 
    360341    private void detectAndReplaceTasks(RuleApplicationData appData) { 
    361         System.out.println("detecting and replacing tasks"); 
     342        Console.traceln(Level.FINE, "detecting and replacing tasks"); 
    362343        appData.getStopWatch().start("detecting tasks"); 
    363344         
     
    370351         
    371352        appData.getStopWatch().stop("replacing tasks"); 
    372         System.out.println("detected and replaced " + appData.getLastFoundTasks().size() + " tasks"); 
     353        Console.traceln(Level.INFO, "detected and replaced " + appData.getLastFoundTasks().size() + 
     354                        " tasks occuring " + appData.getLastFoundTasks().getOccurrenceCount() + 
     355                        "times"); 
    373356    } 
    374357 
     
    377360     */ 
    378361    private void getSequencesOccuringMostOften(RuleApplicationData appData) { 
    379         System.out.println("determining most prominent tasks"); 
     362        Console.traceln(Level.FINE, "determining most prominent tasks"); 
    380363 
    381364        Tasks tasks; 
     
    411394         
    412395        appData.getStopWatch().start("testtrie"); 
    413         Trie<ITaskInstance> trie = new Trie<ITaskInstance>(taskComparator); 
     396        Trie<ITaskInstance> trie = new Trie<ITaskInstance>(identityTaskComparator); 
    414397         
    415398        for (IUserSession session : appData.getSessions()) { 
     
    433416                    tasksEqual = true; 
    434417                    for (int i = 0; i < task1.size(); i++) { 
    435                         if (!taskComparator.equals(task1.get(i).getTask(), task2.get(i).getTask())) 
     418                        if (!identityTaskComparator.equals(task1.get(i).getTask(), task2.get(i).getTask())) 
    436419                        { 
    437420                            tasksEqual = false; 
     
    458441             
    459442            throw new IllegalArgumentException("both tries calculated different subsequences"); 
    460         }*/ 
     443        } 
    461444         
    462445        /*TrieStatisticsDumper dumper = new TrieStatisticsDumper(); 
     
    466449        appData.setLastFoundTasks(tasks); 
    467450 
    468         System.out.println("found " + appData.getLastFoundTasks().size() + " tasks occurring " + 
    469                           appData.getLastFoundTasks().getOccurrenceCount() + " times"); 
     451        Console.traceln(Level.FINE, "found " + appData.getLastFoundTasks().size() + " tasks " + 
     452                        "occurring " + appData.getLastFoundTasks().getOccurrenceCount() + " times"); 
    470453    } 
    471454 
     
    475458     */ 
    476459    private void createNewTrie(RuleApplicationData appData) { 
    477         System.out.println("training trie with a maximum sequence length of " + 
    478                            appData.getTrainedSequenceLength()); 
     460        Console.traceln(Level.FINER, "training trie with a maximum sequence length of " + 
     461                        appData.getTrainedSequenceLength()); 
    479462 
    480463        appData.getStopWatch().start("training trie"); 
    481         appData.setLastTrie(new TaskInstanceTrie(taskComparator)); 
     464         
     465        // we prepared the task instances to refer to unique tasks, if they are treated 
     466        // as equal. Therefore, we just compare the identity of the tasks of the task 
     467        // instances 
     468        appData.setLastTrie(new TaskInstanceTrie(identityTaskHandlingStrategy)); 
    482469     
    483470        appData.getLastTrie().trainSessions 
     
    496483            (appData.getLastFoundTasks().getOccurrenceCount() > 1)) 
    497484        { 
    498             System.out.println("replacing tasks occurrences"); 
     485            Console.traceln(Level.FINER, "replacing tasks occurrences"); 
    499486 
    500487            for (List<ITaskInstance> task : appData.getLastFoundTasks()) { 
    501488                ISequence sequence = taskFactory.createNewSequence(); 
    502489                 
    503                 System.out.println("replacing " + sequence.getId() + ": " + task); 
     490                Console.traceln(Level.FINEST, "replacing " + sequence.getId() + ": " + task); 
    504491 
    505492                List<ITaskInstance> sequenceInstances = 
     
    507494                 
    508495                harmonizeSequenceInstancesModel(sequence, sequenceInstances,task.size()); 
    509                 appData.detectedAndReplacedTasks(sequenceInstances.size() > 0); 
     496                appData.detectedAndReplacedTasks 
     497                    (appData.detectedAndReplacedTasks() || (sequenceInstances.size() > 0)); 
    510498 
    511499                if (sequenceInstances.size() < appData.getLastFoundTasks().getOccurrenceCount()) { 
    512                     System.out.println(sequence.getId() + ": replaced task only " + 
    513                                        sequenceInstances.size() + " times instead of expected " + 
    514                                        appData.getLastFoundTasks().getOccurrenceCount()); 
     500                    Console.traceln(Level.FINE, sequence.getId() + ": replaced task only " + 
     501                                    sequenceInstances.size() + " times instead of expected " + 
     502                                    appData.getLastFoundTasks().getOccurrenceCount()); 
    515503                } 
    516504            } 
     
    526514                                                 int                 sequenceLength) 
    527515    { 
     516        TaskComparator comparator = preparationTaskHandlingStrategy.getTaskComparator(); 
    528517         
    529518        // ensure for each subtask that lexically different variants are preserved 
     
    537526                 
    538527                for (int i = 0; i < subTaskVariants.size(); i++) { 
    539                     if (taskComparator.areLexicallyEqual(subTaskVariants.get(i), candidate)) { 
     528                    if (comparator.areLexicallyEqual(subTaskVariants.get(i), candidate)) { 
    540529                        taskBuilder.setTask 
    541530                            (sequenceInstance.get(subTaskIndex), subTaskVariants.get(i)); 
     
    624613             
    625614            for (int j = 0; j < subList.size(); j++) { 
    626                 if (!taskComparator.equals(list.get(i + j), subList.get(j))) { 
     615                // we prepared the task instances to refer to unique tasks, if they are treated 
     616                // as equal. Therefore, we just compare the identity of the tasks of the task 
     617                // instances 
     618                if (list.get(i + j).getTask() != subList.get(j).getTask()) { 
    627619                    matchFound = false; 
    628620                    break; 
     
    655647             
    656648            for (int j = 0; j < subList.size(); j++) { 
    657                 if (!taskComparator.equals(list.get(i + j), subList.get(j))) { 
     649                // we prepared the task instances to refer to unique tasks, if they are treated 
     650                // as equal. Therefore, we just compare the identity of the tasks of the task 
     651                // instances 
     652                if (list.get(i + j) != subList.get(j)) { 
    658653                    matchFound = false; 
    659654                    break; 
Note: See TracChangeset for help on using the changeset viewer.