Changeset 1738


Ignore:
Timestamp:
09/11/14 14:03:20 (10 years ago)
Author:
rkrimmel
Message:

Fixed few parallel task scheduling bugs.

Location:
branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/RuleUtils.java

    r1734 r1738  
    2828import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory; 
    2929 
    30  
    3130/** 
    3231 * <p> 
    3332 * provides some convenience methods for rule application 
    34  * </p>. 
     33 * </p> 
     34 * . 
    3535 * 
    36  * @author Patrick Harms 
     36 * @author Patrick Harms, Ralph Krimmel 
    3737 */ 
    3838class RuleUtils { 
     
    4242         * replaces a sub sequence for a specified range of elements in the provided 
    4343         * task instances list by a sub task instance 
    44          * </p>. 
    45          * 
    46          * @param parent            the list of which the range shall be replaced 
    47          * @param startIndex            the start index of the range 
    48          * @param endIndex            the end index of the range (inclusive) 
    49          * @param model            the task model (required for instantiating the sub sequence) 
    50          * @param taskFactory            the task factory used for instantiating the sub sequence 
    51          * @param taskBuilder            the task builder to perform changes in the task structure 
     44         * </p> 
     45         * . 
     46         * 
     47         * @param parent 
     48         *            the list of which the range shall be replaced 
     49         * @param startIndex 
     50         *            the start index of the range 
     51         * @param endIndex 
     52         *            the end index of the range (inclusive) 
     53         * @param model 
     54         *            the task model (required for instantiating the sub sequence) 
     55         * @param taskFactory 
     56         *            the task factory used for instantiating the sub sequence 
     57         * @param taskBuilder 
     58         *            the task builder to perform changes in the task structure 
    5259         * @return the replacement for the range 
    5360         */ 
     
    5764                final ISequenceInstance subsequence = taskFactory 
    5865                                .createNewTaskInstance(model); 
    59  
    60                  
    61                 // TODO: This is dirty, return this in addition with the sequence instance instead 
     66                @SuppressWarnings("unused") 
     67                int modelid = model.getId(); 
     68                if(modelid == 5412) { 
     69                        System.out.println("Printing session: "); 
     70                        for (int i = 0; i < parent.size();i++) { 
     71                                System.out.println(parent.get(i)); 
     72                        } 
     73                        System.out.println("startIndex: " + startIndex + " endIndex: " + endIndex + "\n"); 
     74                        System.out.println("Printing model: "); 
     75                        for(int i = 0; i < ((ISequence)model).getChildren().size();i++) { 
     76                                System.out.println((ISequence)model.getChildren().get(i));               
     77                        } 
     78                         
     79                } 
     80                // TODO: This is dirty, return this in addition with the sequence 
     81                // instance instead 
    6282                missedOptionals = 0; 
    6383                int modelindex = 0; 
     
    89109                                        continue; 
    90110                                } 
    91                         } else if (tempTask.getType() == "selection") { 
    92                                 final ISelectionInstance selection = taskFactory 
    93                                                 .createNewTaskInstance((ISelection) tempTask); 
     111                        } else if (tempTask instanceof ISelection) { 
     112                                final ISelectionInstance selection = taskFactory.createNewTaskInstance((ISelection) tempTask); 
    94113                                final ISelection tmpSel = (ISelection) tempTask; 
    95                                 if ((tmpSel.getChildren().get(0).getType() == "sequence") 
    96                                                 && (tmpSel.getChildren().get(1).getType() == "sequence")) { 
     114                                //Check if the selection has 2 sequences as children 
     115                                if ((tmpSel.getChildren().get(0) instanceof ISequence) && (tmpSel.getChildren().get(1) instanceof ISequence)) { 
    97116                                        ISequenceInstance selseq = null; 
    98                                         // The selection I create can just have 2 children 
    99                                         if (parent.get(startIndex).getTask().getId() == ((ISequence) tmpSel 
    100                                                         .getChildren().get(0)).getChildren().get(0).getId()) { 
    101                                                 selseq = taskFactory 
    102                                                                 .createNewTaskInstance((ISequence) tmpSel 
    103                                                                                 .getChildren().get(0)); 
    104                                         } else if (parent.get(startIndex).getTask().getId() == ((ISequence) tmpSel 
    105                                                         .getChildren().get(1)).getChildren().get(0).getId()) { 
    106                                                 selseq = taskFactory 
    107                                                                 .createNewTaskInstance((ISequence) tmpSel 
    108                                                                                 .getChildren().get(1)); 
    109                                         } else if ((parent.get(startIndex).getTask().getId() == tmpSel 
    110                                                         .getChildren().get(0).getId()) 
    111                                                         || (parent.get(startIndex).getTask().getId() == tmpSel 
    112                                                                         .getChildren().get(1).getId())) { 
     117                                        // The selection I create can just have 2 children, we need to check here, to which sequence of the model this occurence belongs 
     118                                        //This if checks of the occurrence is equal to the first element of the first sequence in the model 
     119                                        if (parent.get(startIndex).getTask().equals(((ISequence) tmpSel.getChildren().get(0)).getChildren().get(0))) { 
     120                                                selseq = taskFactory.createNewTaskInstance((ISequence) tmpSel.getChildren().get(0)); 
     121                                        //This if checks of the occurrence is equal to the first element of the second sequence in the model                                             
     122                                        } else if (parent.get(startIndex).getTask().equals(((ISequence) tmpSel.getChildren().get(1)).getChildren().get(0))) { 
     123                                                selseq = taskFactory.createNewTaskInstance((ISequence) tmpSel.getChildren().get(1)); 
     124                                        //If the occurence is already a sequence we don't need to do anything, the next iteration will detect this as a sequence and add it 
     125                                        } else if ((parent.get(startIndex).getTask().equals(tmpSel.getChildren().get(0).getId())) 
     126                                                        || (parent.get(startIndex).getTask().equals(tmpSel.getChildren().get(1).getId()))) { 
     127                                                //taskBuilder.setChild(selection, parent.get(startIndex)); 
     128                                                //taskBuilder.addChild(subsequence, selection); 
     129                                                //taskBuilder.removeTaskInstance(parent, startIndex); 
     130                                                //modelindex++; 
    113131                                                continue; 
     132                                                 
    114133                                        } 
    115  
    116                                         for (int k = 0; k < selseq.getSequence().getChildren() 
    117                                                         .size(); k++) { 
     134                                        //TODO: Sometimes nullpointer exception here :( 
     135                                        for (int k = 0; k < ((ISequence)tmpSel.getChildren().get(0)).getChildren().size(); k++) { 
    118136                                                taskBuilder.addChild(selseq, parent.get(startIndex)); 
    119137                                                taskBuilder.removeTaskInstance(parent, startIndex); 
     
    124142                                        modelindex++; 
    125143                                        continue; 
     144                                //It is just a plain selection 
    126145                                } else { 
    127146                                        taskBuilder.setChild(selection, parent.get(startIndex)); 
     
    140159 
    141160                taskBuilder.addTaskInstance(parent, startIndex, subsequence); 
     161         
    142162 
    143163                return subsequence; 
     
    147167         * <p> 
    148168         * returns the next available id (uses the id counter) 
    149          * </p>. 
     169         * </p> 
     170         * . 
    150171         * 
    151172         * @return the next available id 
     
    192213         * Prints the progress percentage. 
    193214         * 
    194          * @param message the message 
    195          * @param count the count 
    196          * @param size the size 
     215         * @param message 
     216         *            the message 
     217         * @param count 
     218         *            the count 
     219         * @param size 
     220         *            the size 
    197221         */ 
    198222        static void printProgressPercentage(String message, int count, int size) { 
     
    202226                                // Thread.currentThread().getName() + ": " + Math.round((float) 
    203227                                // count/size*100))+ "%"); 
    204                                 System.out.println(message + " in thread" 
    205                                                 + Thread.currentThread().getName() + ": " 
    206                                                 + Math.round(((float) count / size) * 100) + "%"); 
     228                                // System.out.println(message + " in thread" 
     229                                // + Thread.currentThread().getName() + ": " 
     230                                // + Math.round(((float) count / size) * 100) + "%"); 
    207231                        } 
    208232                } else { 
     
    210234                        // Thread.currentThread().getName() + ": " +Math.round((float) 
    211235                        // count/size*100))+ "%"); 
    212                         System.out.println(message + " in thread" 
    213                                         + Thread.currentThread().getName() + ": " 
    214                                         + Math.round(((float) count / size) * 100) + "%"); 
     236                        // System.out.println(message + " in thread" 
     237                        // + Thread.currentThread().getName() + ": " 
     238                        // + Math.round(((float) count / size) * 100) + "%"); 
    215239 
    216240                } 
     
    230254         * <p> 
    231255         * prevent instantiation 
    232          * </p>. 
     256         * </p> 
     257         * . 
    233258         */ 
    234259        private RuleUtils() { 
  • branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java

    r1735 r1738  
    5454import de.ugoe.cs.util.console.Console; 
    5555 
    56  
    5756/** 
    5857 * <p> 
     
    6059 * of recorded user sessions. For this, it first harmonizes all tasks. This 
    6160 * eases later comparison. Then it searches the sessions for iterations and 
    62  * replaces them accordingly. Then it searches for sub sequences using alignment algorithms 
    63  * For each found sub sequence, it replaces 
    64  * the occurrences by creating appropriate {@link ISequence}s. Afterwards, again 
    65  * searches for iterations and then again for sub sequences until no more 
    66  * replacements are done. 
     61 * replaces them accordingly. Then it searches for sub sequences using alignment 
     62 * algorithms For each found sub sequence, it replaces the occurrences by 
     63 * creating appropriate {@link ISequence}s. Afterwards, again searches for 
     64 * iterations and then again for sub sequences until no more replacements are 
     65 * done. 
    6766 * </p> 
    6867 * <p> 
     
    7372public class SequenceForTaskDetectionRuleAlignment implements ISessionScopeRule { 
    7473 
    75          
    7674        /** 
    7775         * The Class RuleApplicationData. 
     
    8280                private static final long serialVersionUID = -7559657686755522960L; 
    8381 
    84                 /** The number2task HashMap. Since we align the tasks just by their integer id,  
    85                  *  we need this to convert them back to Tasks again*/ 
     82                /** 
     83                 * The number2task HashMap. Since we align the tasks just by their 
     84                 * integer id, we need this to convert them back to Tasks again 
     85                 */ 
    8686                private final HashMap<Integer, ITask> number2task; 
    8787 
    88                  
    89                 /** The unique tasks, keeps track about all unique tasks  
    90                  * TODO: We Actually just need number2task here, this structure can be 
    91                  * removed in the future.*/ 
     88                /** 
     89                 * The unique tasks, keeps track about all unique tasks TODO: We 
     90                 * Actually just need number2task here, this structure can be removed in 
     91                 * the future. 
     92                 */ 
    9293                private final HashSet<ITask> uniqueTasks; 
    9394 
    94                 /** The substitution matrix used by the alignment algorithm to be able to compare 
    95                  *  distances of tasks */ 
     95                /** 
     96                 * The substitution matrix used by the alignment algorithm to be able to 
     97                 * compare distances of tasks 
     98                 */ 
    9699                private final ObjectDistanceSubstitionMatrix submat; 
    97100 
    98                 /** HashMap for keeping track in which sequence which replacement has been performed. 
    99                  *  Neccessary for updating the indices of other occurrences accordingly */ 
     101                /** 
     102                 * HashMap for keeping track in which sequence which replacement has 
     103                 * been performed. Neccessary for updating the indices of other 
     104                 * occurrences accordingly 
     105                 */ 
    100106                private HashMap<Integer, List<MatchOccurence>> replacedOccurences; 
    101107 
     
    103109                private LinkedList<Match> matchseqs; 
    104110 
    105                 /** The generated NumberSequences.  
    106                  * This is the integer representation of the user sessions */ 
     111                /** 
     112                 * The generated NumberSequences. This is the integer representation of 
     113                 * the user sessions 
     114                 */ 
    107115                private ArrayList<NumberSequence> numberseqs; 
    108116 
     
    125133                 * Instantiates a new rule application data. 
    126134                 * 
    127                  * @param sessions The user sessions 
     135                 * @param sessions 
     136                 *            The user sessions 
    128137                 */ 
    129138                private RuleApplicationData(List<IUserSession> sessions) { 
     
    241250                 * New task created. 
    242251                 * 
    243                  * @param task the task 
     252                 * @param task 
     253                 *            the task 
    244254                 */ 
    245255                private void newTaskCreated(ITask task) { 
     
    259269                 * Sets the number sequences. 
    260270                 * 
    261                  * @param numberseqs the new number sequences 
     271                 * @param numberseqs 
     272                 *            the new number sequences 
    262273                 */ 
    263274                private void setNumberSequences(ArrayList<NumberSequence> numberseqs) { 
     
    268279                 * Sets the replaced occurences. 
    269280                 * 
    270                  * @param replacedOccurences the replaced occurences 
     281                 * @param replacedOccurences 
     282                 *            the replaced occurences 
    271283                 */ 
    272284                public void setReplacedOccurences( 
     
    291303        private int iteration = 0; 
    292304 
    293         /** <p> the task factory to be used for creating substructures for the temporal relationships identified during rul application </p>. */ 
     305        /** 
     306         * <p> 
     307         * the task factory to be used for creating substructures for the temporal 
     308         * relationships identified during rul application 
     309         * </p> 
     310         * . 
     311         */ 
    294312        private final ITaskFactory taskFactory; 
    295313 
    296         /** <p> the task builder to be used for creating substructures for the temporal relationships identified during rule application </p>. */ 
     314        /** 
     315         * <p> 
     316         * the task builder to be used for creating substructures for the temporal 
     317         * relationships identified during rule application 
     318         * </p> 
     319         * . 
     320         */ 
    297321        private final ITaskBuilder taskBuilder; 
    298322 
     
    339363        public RuleApplicationResult apply(List<IUserSession> sessions) { 
    340364                final RuleApplicationData appData = new RuleApplicationData(sessions); 
    341                  
     365 
    342366                harmonizeEventTaskInstancesModel(appData); 
    343  
    344367 
    345368                Console.traceln(Level.INFO, "generating substitution matrix from " 
     
    362385                        appData.getStopWatch().stop("whole loop"); 
    363386                        appData.getStopWatch().dumpStatistics(System.out); 
     387                        new TaskTreeValidator().validate(appData.getSessions()); 
    364388                        appData.getStopWatch().reset(); 
    365389 
     
    384408         * Creates the number sequences. 
    385409         * 
    386          * @param appData the app data 
     410         * @param appData 
     411         *            the app data 
    387412         * @return the array list 
    388413         */ 
     
    438463         * Detect and replace tasks. 
    439464         * 
    440          * @param appData            the rule application data combining all data used for applying 
     465         * @param appData 
     466         *            the rule application data combining all data used for applying 
    441467         *            this rule 
    442468         */ 
     
    451477                // appData.setMatchseqs(generatePairwiseAlignments(appData)); 
    452478                generatePairwiseAlignments(appData); 
     479                Console.traceln(Level.FINE, "Found " + appData.getMatchseqs().size() 
     480                                + " results"); 
    453481 
    454482                // Searching each match in all other sessions, counting its occurences 
     
    475503        } 
    476504 
    477          
    478505        /** 
    479506         * Generate pairwise alignments. 
    480507         * 
    481          * @param appData the app data 
     508         * @param appData 
     509         *            the app data 
    482510         */ 
    483511        private void generatePairwiseAlignments(RuleApplicationData appData) { 
     
    496524                final int interval = numberSeqSize / newThreads; 
    497525                int rest = numberSeqSize % newThreads; 
    498  
    499                 for (int i = 0; i < (numberSeqSize - interval); i += interval) { 
     526                Console.traceln(Level.FINE, "Interval: " + interval + " Rest: " + rest); 
     527                for (int i = 0; i <= (numberSeqSize - interval); i += interval) { 
    500528                        int offset = 0; 
    501529                        if (rest != 0) { 
     
    503531                                rest--; 
    504532                        } 
     533                 
    505534                        final int from = i; 
    506                         final int to = i + interval + offset; 
    507                         System.out.println("Creating thread for sessions " + from 
    508                                         + " till " + to); 
     535                        final int to = i + interval+offset-1; 
     536                        Console.traceln(Level.FINE, "Aligning: Creating thread for sessions " + from + " till " + to); 
    509537                        final ParallelPairwiseAligner aligner = new ParallelPairwiseAligner( 
    510538                                        appData, from, to); 
    511539                        executor.execute(aligner); 
     540                        i+=offset; 
    512541                } 
    513542                executor.shutdown(); 
     
    578607         * <p> 
    579608         * TODO clarify why this is done (in fact, ask Patrick Harms) 
    580          * </p>. 
     609         * </p> 
     610         * . 
    581611         * 
    582          * @param iteration the iteration 
    583          * @param iterationInstances the iteration instances 
     612         * @param iteration 
     613         *            the iteration 
     614         * @param iterationInstances 
     615         *            the iteration instances 
    584616         */ 
    585617        private void harmonizeIterationInstancesModel(IIteration iteration, 
     
    641673        } 
    642674 
    643  
    644675        /** 
    645676         * Match as sequence. 
    646677         * 
    647          * @param appData               RuleApplicationData needed to keep track of all created tasks  
    648          * @param m                             The match to be converted into a Task 
    649          * @return                              The task of the match with an ISequence as its root 
     678         * @param appData 
     679         *            RuleApplicationData needed to keep track of all created tasks 
     680         * @param m 
     681         *            The match to be converted into a Task 
     682         * @return The task of the match with an ISequence as its root 
    650683         */ 
    651684        synchronized public ISequence matchAsSequence(RuleApplicationData appData, 
     
    690723                        } 
    691724                        // Both tasks are not equal, we need to insert a selection here. 
    692                         // Now things get complicated. We first need to check  
    693                         // if the next position is not a selection. Then we can just create a selection 
     725                        // Now things get complicated. We first need to check 
     726                        // if the next position is not a selection. Then we can just create 
     727                        // a selection 
    694728                        // of the both Tasks 
    695                         // In the other case (more than one selection following this selection), we want to  
    696                         // create a selection of sequences where each sequence gets the corresponding task of  
     729                        // In the other case (more than one selection following this 
     730                        // selection), we want to 
     731                        // create a selection of sequences where each sequence gets the 
     732                        // corresponding task of 
    697733                        // the its sequence in the pattern. 
    698734                        // 
     
    728764                                        taskBuilder.addChild(selection, subsequence2); 
    729765                                        taskBuilder.addChild(sequence, selection); 
     766                                        // TODO: We run not till the end! 
    730767                                        while ((i < (first.length - 1)) && selectionfound) { 
    731768                                                selectionfound = false; 
     
    743780                                                i++; 
    744781                                        } 
    745                                         if ((i == (first.length - 1)) && selectionfound) { 
    746                                                 taskBuilder.addChild(subsequence1, appData 
    747                                                                 .getNumber2Task().get(first[i])); 
    748                                                 taskBuilder.addChild(subsequence2, appData 
    749                                                                 .getNumber2Task().get(second[i])); 
    750                                         } 
     782                                         if ((i == (first.length - 1)) && selectionfound) { 
     783                                         taskBuilder.addChild(subsequence1, appData 
     784                                         .getNumber2Task().get(first[i])); 
     785                                         taskBuilder.addChild(subsequence2, appData 
     786                                         .getNumber2Task().get(second[i])); 
     787                                         } 
    751788                                } 
    752789                        } else { 
     790                                // i = length-1 
    753791                                if ((first[i] != second[i])) { 
    754792 
     
    771809         * <p> 
    772810         * replaces all occurrences of all tasks provided in the set with iterations 
    773          * </p>. 
     811         * </p> 
     812         * . 
    774813         * 
    775          * @param iteratedTasks            the tasks to be replaced with iterations 
    776          * @param sessions            the sessions in which the tasks are to be replaced 
    777          * @param appData            the rule application data combining all data used for applying 
     814         * @param iteratedTasks 
     815         *            the tasks to be replaced with iterations 
     816         * @param sessions 
     817         *            the sessions in which the tasks are to be replaced 
     818         * @param appData 
     819         *            the rule application data combining all data used for applying 
    778820         *            this rule 
    779821         */ 
     
    841883         * Replace matches. 
    842884         * 
    843          * @param appData the app data 
     885         * @param appData 
     886         *            the app data 
    844887         */ 
    845888        private void replaceMatches(RuleApplicationData appData) { 
     
    847890 
    848891                final int matchSeqSize = appData.getMatchseqs().size(); 
    849                 int newThreads = nThreads; 
    850                 if (matchSeqSize < nThreads) { 
    851                         newThreads = matchSeqSize; 
    852                 } 
    853                 final ExecutorService executor = Executors 
    854                                 .newFixedThreadPool(newThreads); 
    855                 final int interval = matchSeqSize / newThreads; 
    856                 int rest = matchSeqSize % newThreads; 
    857  
    858                 for (int i = 0; i < (matchSeqSize - interval); i += interval) { 
    859                         int offset = 0; 
    860                         if (rest != 0) { 
    861                                 offset = 1; 
    862                                 rest--; 
    863                         } 
    864                         final int from = i; 
    865                         final int to = i + interval + offset; 
    866                         System.out 
    867                         .println("Replacement: Creating thread with matches from " 
    868                                         + from + " to " + to); 
    869                         // search each match in every other sequence 
    870                         final ParallelMatchReplacer replacer = new ParallelMatchReplacer( 
    871                                         appData, from, to); 
    872                         executor.execute(replacer); 
    873                 } 
    874                 executor.shutdown(); 
    875                 try { 
    876                         executor.awaitTermination(2, TimeUnit.HOURS); 
    877                 } catch (final InterruptedException e) { 
    878                         // TODO Auto-generated catch block 
    879                         e.printStackTrace(); 
    880                 } 
    881         } 
    882  
     892 
     893                for (int i = 0; i < matchSeqSize; i++) { 
     894 
     895                        // Every pattern consists of 2 sequences, therefore the minimum 
     896                        // occurrences here is 2. 
     897                        // We just need the sequences also occurring in other sequences 
     898                        // as well 
     899                        if (appData.getMatchseqs().get(i).occurenceCount() > 2) { 
     900 
     901                                final ISequence task = matchAsSequence(appData, appData 
     902                                                .getMatchseqs().get(i)); 
     903                                invalidOccurence: for (final Iterator<MatchOccurence> it = appData 
     904                                                .getMatchseqs().get(i).getOccurences().iterator(); it 
     905                                                .hasNext();) { 
     906                                        final MatchOccurence oc = it.next(); 
     907 
     908                                        // Check if nothing has been replaced in the sequence we 
     909                                        // want to replace now 
     910                                        if (appData.getReplacedOccurrences() 
     911                                                        .get(oc.getSequenceId()) == null) { 
     912                                                appData.getReplacedOccurrences().put( 
     913                                                                oc.getSequenceId(), 
     914                                                                new LinkedList<MatchOccurence>()); 
     915                                        } else { 
     916                                                // check if we have any replaced occurence with 
     917                                                // indexes 
     918                                                // smaller than ours. If so, we need to adjust 
     919                                                // our start 
     920                                                // and endpoints 
     921                                                // of the replacement. 
     922                                                // Also do a check if we have replaced this 
     923                                                // specific 
     924                                                // MatchOccurence in this sequence already. Jump 
     925                                                // to the 
     926                                                // next occurence if this is the case. 
     927                                                // This is no more neccessary once the matches 
     928                                                // are 
     929                                                // harmonized. 
     930 
     931                                                for (final Iterator<MatchOccurence> jt = appData 
     932                                                                .getReplacedOccurrences() 
     933                                                                .get(oc.getSequenceId()).iterator(); jt 
     934                                                                .hasNext();) { 
     935                                                        final MatchOccurence tmpOC = jt.next(); 
     936 
     937                                                        if ((oc.getStartindex() >= tmpOC.getStartindex()) 
     938                                                                        && (oc.getStartindex() <= tmpOC 
     939                                                                                        .getEndindex())) { 
     940                                                                continue invalidOccurence; 
     941                                                        } 
     942                                                        if (oc.getEndindex() >= tmpOC.getStartindex()) { 
     943                                                                continue invalidOccurence; 
     944 
     945                                                        } else if (oc.getStartindex() > tmpOC.getEndindex()) { 
     946                                                                final int diff = tmpOC.getEndindex() 
     947                                                                                - tmpOC.getStartindex(); 
     948                                                                // Just to be sure. 
     949                                                                if (diff > 0) { 
     950                                                                        oc.setStartindex((oc.getStartindex() - diff) + 1); 
     951                                                                        oc.setEndindex((oc.getEndindex() - diff) + 1); 
     952                                                                } else { 
     953                                                                        Console.traceln(Level.WARNING, 
     954                                                                                        "End index of a Match before start. This should never happen"); 
     955                                                                } 
     956                                                        } 
     957                                                } 
     958                                        } 
     959                                        appData.detectedAndReplacedTasks = true; 
     960                                        final ISequenceInstance sequenceInstances = RuleUtils 
     961                                                        .createNewSubSequenceInRange(appData.getSessions() 
     962                                                                        .get(oc.getSequenceId()), oc 
     963                                                                        .getStartindex(), oc.getEndindex(), task, 
     964                                                                        taskFactory, taskBuilder); 
     965                                        oc.setEndindex((oc.getStartindex() + sequenceInstances 
     966                                                        .size()) - RuleUtils.missedOptionals); 
     967 
     968                                        // Adjust the length of the match regarding to the 
     969                                        // length of 
     970                                        // instance. (OptionalInstances may be shorter) 
     971 
     972                                        appData.getReplacedOccurrences().get(oc.getSequenceId()) 
     973                                                        .add(oc); 
     974                                } 
     975                        } 
     976                } 
     977        } 
    883978 
    884979        /** 
     
    888983         * </p> 
    889984         * 
    890          * @param sessions the sessions 
     985         * @param sessions 
     986         *            the sessions 
    891987         * @return a set of tasks being iterated somewhere 
    892988         */ 
     
    9111007         * Search matches in all sessions. 
    9121008         * 
    913          * @param appData the app data 
     1009         * @param appData 
     1010         *            the app data 
    9141011         */ 
    9151012        private void searchMatchesInAllSessions(RuleApplicationData appData) { 
    916                 Console.traceln(Level.INFO, 
    917                                 "searching for patterns occuring most with " + nThreads 
    918                                                 + " threads"); 
     1013                 
    9191014                // Prepare parallel search of matchseqs 
    9201015                final int matchSeqSize = appData.getMatchseqs().size(); 
     1016                Console.traceln(Level.INFO, 
     1017                                "searching for patterns ("+ matchSeqSize+") occuring most with " + nThreads 
     1018                                                + " threads"); 
    9211019                int newThreads = nThreads; 
    9221020                if (matchSeqSize < nThreads) { 
     
    9261024                int rest = matchSeqSize % newThreads; 
    9271025                final ExecutorService executor = Executors.newFixedThreadPool(nThreads); 
    928  
    929                 for (int i = 0; i < (matchSeqSize - interval); i += interval) { 
     1026                Console.traceln(Level.FINE, "Interval: " + interval + " Rest: " + rest); 
     1027                for (int i = 0; i <= (matchSeqSize-interval); i += interval) { 
    9301028                        int offset = 0; 
    9311029                        if (rest != 0) { 
     
    9341032                        } 
    9351033                        final int from = i; 
    936                         final int to = i + interval + offset; 
    937                         System.out 
    938                         .println("Match finding: Creating thread with matches from " 
    939                                         + from + " to " + to); 
     1034                        final int to = i + interval + offset-1; 
     1035                        Console.traceln(Level.FINE, "Match finding: Creating thread with matches from " 
     1036                                                        + from + " to " + to); 
    9401037                        // search each match in every other sequence 
    9411038                        final ParallelMatchOcurrencesFinder finder = new ParallelMatchOcurrencesFinder( 
    9421039                                        appData, from, to); 
    9431040                        executor.execute(finder); 
     1041                        i+=offset; 
    9441042                } 
    9451043                executor.shutdown(); 
     
    9471045                        executor.awaitTermination(2, TimeUnit.HOURS); 
    9481046                } catch (final InterruptedException e) { 
    949                         // TODO Auto-generated catch block 
    9501047                        e.printStackTrace(); 
    9511048                } 
     
    9671064         */ 
    9681065        private class ParallelMatchOcurrencesFinder implements Runnable { 
    969                  
     1066 
    9701067                /** The app data. */ 
    9711068                private final RuleApplicationData appData; 
    972                  
     1069 
    9731070                /** The from. */ 
    9741071                private final int from; 
    975                  
     1072 
    9761073                /** The to. */ 
    9771074                private final int to; 
     
    9801077                 * Instantiates a new parallel match ocurrences finder. 
    9811078                 * 
    982                  * @param appData the app data 
    983                  * @param from the from 
    984                  * @param to the to 
     1079                 * @param appData 
     1080                 *            the app data 
     1081                 * @param from 
     1082                 *            the from 
     1083                 * @param to 
     1084                 *            the to 
    9851085                 */ 
    9861086                ParallelMatchOcurrencesFinder(RuleApplicationData appData, int from, 
     
    9911091                } 
    9921092 
    993                 /* (non-Javadoc) 
     1093                /* 
     1094                 * (non-Javadoc) 
     1095                 *  
    9941096                 * @see java.lang.Runnable#run() 
    9951097                 */ 
     
    10331135 
    10341136        /** 
    1035          * The Class ParallelMatchReplacer. 
    1036          */ 
    1037         private class ParallelMatchReplacer implements Runnable { 
     1137         * The Class ParallelPairwiseAligner. 
     1138         */ 
     1139        private class ParallelPairwiseAligner implements Runnable { 
    10381140 
    10391141                /** The app data. */ 
    10401142                private final RuleApplicationData appData; 
    1041                  
     1143 
    10421144                /** The from. */ 
    10431145                private final int from; 
    1044                  
     1146 
    10451147                /** The to. */ 
    10461148                private final int to; 
    10471149 
    10481150                /** 
    1049                  * Instantiates a new parallel match replacer. 
    1050                  * 
    1051                  * @param appData the app data 
    1052                  * @param from the from 
    1053                  * @param to the to 
    1054                  */ 
    1055                 ParallelMatchReplacer(RuleApplicationData appData, int from, int to) { 
    1056                         this.appData = appData; 
    1057                         this.from = from; 
    1058                         this.to = to; 
    1059                 } 
    1060  
    1061                 /* (non-Javadoc) 
    1062                  * @see java.lang.Runnable#run() 
    1063                  */ 
    1064                 @Override 
    1065                 public void run() { 
    1066                         // TODO Cleanup 
    1067                         // int count = 0; 
    1068                         // int size = to - from; 
    1069                         for (int i = from; i < to; i++) { 
    1070                                 // count++; 
    1071                                 // Every pattern consists of 2 sequences, therefore the minimum 
    1072                                 // occurrences here is 2. 
    1073                                 // We just need the sequences also occurring in other sequences 
    1074                                 // as well 
    1075                                 if (appData.getMatchseqs().get(i).occurenceCount() > 2) { 
    1076  
    1077                                         final ISequence task = matchAsSequence(appData, appData 
    1078                                                         .getMatchseqs().get(i)); 
    1079                                         invalidOccurence: for (final Iterator<MatchOccurence> it = appData 
    1080                                                         .getMatchseqs().get(i).getOccurences().iterator(); it 
    1081                                                         .hasNext();) { 
    1082                                                 final MatchOccurence oc = it.next(); 
    1083  
    1084                                                 // Check if nothing has been replaced in the sequence we 
    1085                                                 // want to replace now 
    1086  
    1087                                                 synchronized (appData.getReplacedOccurrences()) { 
    1088                                                         if (appData.getReplacedOccurrences().get( 
    1089                                                                         oc.getSequenceId()) == null) { 
    1090                                                                 appData.getReplacedOccurrences().put( 
    1091                                                                                 oc.getSequenceId(), 
    1092                                                                                 new LinkedList<MatchOccurence>()); 
    1093                                                         } else { 
    1094                                                                 // check if we have any replaced occurence with 
    1095                                                                 // indexes 
    1096                                                                 // smaller than ours. If so, we need to adjust 
    1097                                                                 // our start 
    1098                                                                 // and endpoints 
    1099                                                                 // of the replacement. 
    1100                                                                 // Also do a check if we have replaced this 
    1101                                                                 // specific 
    1102                                                                 // MatchOccurence in this sequence already. Jump 
    1103                                                                 // to the 
    1104                                                                 // next occurence if this is the case. 
    1105                                                                 // This is no more neccessary once the matches 
    1106                                                                 // are 
    1107                                                                 // harmonized. 
    1108                                                                 for (final Iterator<MatchOccurence> jt = appData 
    1109                                                                                 .getReplacedOccurrences() 
    1110                                                                                 .get(oc.getSequenceId()).iterator(); jt 
    1111                                                                                 .hasNext();) { 
    1112                                                                         final MatchOccurence tmpOC = jt.next(); 
    1113  
    1114                                                                         if ((oc.getStartindex() >= tmpOC 
    1115                                                                                         .getStartindex()) 
    1116                                                                                         && (oc.getStartindex() <= tmpOC 
    1117                                                                                         .getEndindex())) { 
    1118                                                                                 continue invalidOccurence; 
    1119                                                                         } 
    1120                                                                         if (oc.getEndindex() >= tmpOC 
    1121                                                                                         .getStartindex()) { 
    1122                                                                                 continue invalidOccurence; 
    1123  
    1124                                                                         } else if (oc.getStartindex() > tmpOC 
    1125                                                                                         .getEndindex()) { 
    1126                                                                                 final int diff = tmpOC.getEndindex() 
    1127                                                                                                 - tmpOC.getStartindex(); 
    1128                                                                                 // Just to be sure. 
    1129                                                                                 if (diff > 0) { 
    1130                                                                                         oc.setStartindex((oc 
    1131                                                                                                         .getStartindex() - diff) + 1); 
    1132                                                                                         oc.setEndindex((oc.getEndindex() - diff) + 1); 
    1133                                                                                 } else { 
    1134                                                                                         Console.traceln(Level.WARNING, 
    1135                                                                                                         "End index of a Match before start. This should never happen"); 
    1136                                                                                 } 
    1137                                                                         } 
    1138                                                                 } 
    1139                                                         } 
    1140                                                         synchronized (appData) { 
    1141                                                                 appData.detectedAndReplacedTasks = true; 
    1142                                                         } 
    1143                                                         synchronized (appData.getSessions().get( 
    1144                                                                         oc.getSequenceId())) { 
    1145                                                                 final ISequenceInstance sequenceInstances = RuleUtils 
    1146                                                                                 .createNewSubSequenceInRange( 
    1147                                                                                                 appData.getSessions().get( 
    1148                                                                                                                 oc.getSequenceId()), 
    1149                                                                                                                 oc.getStartindex(), 
    1150                                                                                                                 oc.getEndindex(), task, 
    1151                                                                                                                 taskFactory, taskBuilder); 
    1152                                                                 oc.setEndindex((oc.getStartindex() + sequenceInstances 
    1153                                                                                 .size()) - RuleUtils.missedOptionals); 
    1154                                                         } 
    1155                                                 } 
    1156                                                 // Adjust the length of the match regarding to the 
    1157                                                 // length of 
    1158                                                 // instance. (OptionalInstances may be shorter) 
    1159                                                 synchronized (appData.getReplacedOccurrences().get( 
    1160                                                                 oc.getSequenceId())) { 
    1161                                                         appData.getReplacedOccurrences() 
    1162                                                         .get(oc.getSequenceId()).add(oc); 
    1163                                                 } 
    1164                                         } 
    1165                                 } 
    1166                         } 
    1167                 } 
    1168         } 
    1169  
    1170         /** 
    1171          * The Class ParallelPairwiseAligner. 
    1172          */ 
    1173         private class ParallelPairwiseAligner implements Runnable { 
    1174                  
    1175                 /** The app data. */ 
    1176                 private final RuleApplicationData appData; 
    1177                  
    1178                 /** The from. */ 
    1179                 private final int from; 
    1180                  
    1181                 /** The to. */ 
    1182                 private final int to; 
    1183  
    1184                 /** 
    11851151                 * Instantiates a new parallel pairwise aligner. 
    11861152                 * 
    1187                  * @param appData the app data 
    1188                  * @param from the from 
    1189                  * @param to the to 
     1153                 * @param appData 
     1154                 *            the app data 
     1155                 * @param from 
     1156                 *            the from 
     1157                 * @param to 
     1158                 *            the to 
    11901159                 */ 
    11911160                ParallelPairwiseAligner(RuleApplicationData appData, int from, int to) { 
     
    11951164                } 
    11961165 
    1197                 /* (non-Javadoc) 
     1166                /* 
     1167                 * (non-Javadoc) 
     1168                 *  
    11981169                 * @see java.lang.Runnable#run() 
    11991170                 */ 
     
    12011172                public void run() { 
    12021173                        int count = 0; 
    1203                         final int size = to - from; 
     1174                        final int size = to - from;  
    12041175 
    12051176                        for (int i = from; i < to; i++) { 
     
    12231194                } 
    12241195        } 
    1225          
     1196 
    12261197} 
Note: See TracChangeset for help on using the changeset viewer.