Changeset 1741


Ignore:
Timestamp:
09/12/14 11:59:02 (10 years ago)
Author:
rkrimmel
Message:

Commit before parallelizing replacment

Location:
branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees
Files:
5 edited
1 moved

Legend:

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

    r1734 r1741  
    66import java.io.Serializable; 
    77import java.util.ArrayList; 
     8import java.util.Iterator; 
    89import java.util.LinkedList; 
    910 
     
    1213 * The Class Match. 
    1314 */ 
    14 public class Match implements Serializable { 
     15public class Match implements Comparable<Match>,Serializable { 
    1516         
    1617        /** The Constant serialVersionUID. */ 
     
    2122 
    2223        /** The occurences. */ 
    23         private LinkedList<MatchOccurence> occurences; 
     24        private LinkedList<MatchOccurrence> occurrences; 
    2425 
    2526        /** 
     
    2829        public Match() { 
    2930                matchseqs = new ArrayList<NumberSequence>(2); 
    30                 occurences = new LinkedList<MatchOccurence>(); 
     31                occurrences = new LinkedList<MatchOccurrence>(); 
    3132                matchseqs.add(null); 
    3233                matchseqs.add(null); 
    33         } 
     34        }               // TODO Auto-generated method stub 
    3435 
    3536        /** 
     
    3839         * @param occurence the occurence 
    3940         */ 
    40         public void addOccurence(MatchOccurence occurence) { 
    41                 occurences.add(occurence); 
     41        public void addOccurence(MatchOccurrence occurence) { 
     42                occurrences.add(occurence); 
    4243        } 
    4344 
    4445        /** 
    45          * Adds the occurences of. 
     46         * Adds the occurrences of given match to this match . 
    4647         * 
    47          * @param m the m 
     48         * @param m the match the occurrences should be merged with this one 
    4849         */ 
    4950        public void addOccurencesOf(Match m) { 
    50                 occurences.addAll(m.getOccurences()); 
     51                occurrences.addAll(m.getOccurences()); 
    5152        } 
    5253 
     
    5455         * Equals. 
    5556         * 
    56          * @param m the m 
    57          * @return true, if successful 
     57         * @param m the Match the equality should be checked against 
     58         * @return true, if both Matches are equal 
    5859         */ 
    5960        public boolean equals(Match m) { 
     
    7778 
    7879        /** 
    79          * Gets the occurences. 
     80         * Gets the occurrences. 
    8081         * 
    81          * @return the occurences 
     82         * @return the occurrences 
    8283         */ 
    83         public LinkedList<MatchOccurence> getOccurences() { 
    84                 return occurences; 
     84        public LinkedList<MatchOccurrence> getOccurences() { 
     85                return occurrences; 
    8586        } 
    8687 
     
    9596 
    9697        /** 
    97          * Occurence count. 
     98         * Occurrence count. 
    9899         * 
    99          * @return the int 
     100         * @return the number of occurrences of this match 
    100101         */ 
    101102        public int occurenceCount() { 
    102                 return occurences.size(); 
     103                return occurrences.size(); 
    103104        } 
    104105 
     
    113114 
    114115        /** 
    115          * Sets the occurences. 
     116         * Sets the occurrences. 
    116117         * 
    117          * @param occurences the new occurences 
     118         * @param occurences the new occurrences 
    118119         */ 
    119         public void setOccurences(LinkedList<MatchOccurence> occurences) { 
    120                 this.occurences = occurences; 
     120        public void setOccurences(LinkedList<MatchOccurrence> occurences) { 
     121                this.occurrences = occurences; 
    121122        } 
    122123 
     
    130131        } 
    131132 
     133 
     134        /** 
     135         * Ocurrence id sum. Used for comparing and sorting matches 
     136         * 
     137         * @return th 
     138         */ 
     139        public int ocurrenceIDSum() { 
     140                int sum = 0; 
     141                for(Iterator<MatchOccurrence> it = occurrences.iterator();it.hasNext();) { 
     142                        MatchOccurrence mo = it.next(); 
     143                        sum+=mo.getSequenceId(); 
     144                } 
     145                return sum; 
     146        } 
     147         
     148        /** 
     149         * Task id sum. Used for comparing and sorting matches 
     150         * 
     151         * @return  
     152         */ 
     153        public int taskIdSum() { 
     154                int sum = 0; 
     155                int[] first = this.getFirstSequence().getSequence(); 
     156                int[] second = this.getSecondSequence().getSequence(); 
     157                for(int i = 0;i < this.getFirstSequence().size();i++) { 
     158                        sum= first[i]+second[i]; 
     159                } 
     160                return sum; 
     161        } 
     162         
     163        public Match cloneWithoutOccurences() throws CloneNotSupportedException  { 
     164                Match result; 
     165                result = (Match) this.clone(); 
     166                result.occurrences.clear(); 
     167                return result; 
     168        } 
     169         
    132170        /** 
    133171         * Size. 
    134172         * 
    135          * @return the int 
     173         * @return the size (number of aligned tasks) 
    136174         */ 
    137175        public int size() { 
     
    140178        } 
    141179 
     180        /* (non-Javadoc) 
     181         * @see java.lang.Comparable#compareTo(java.lang.Object) 
     182         */ 
     183        @Override 
     184        public int compareTo(Match arg0) { 
     185                 
     186                return 0; 
     187        } 
     188 
    142189} 
  • branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/MatchOccurrence.java

    r1734 r1741  
    1010 * The Class MatchOccurence. 
    1111 */ 
    12 public class MatchOccurence implements Serializable { 
     12public class MatchOccurrence implements Serializable { 
    1313         
    1414        /** The Constant serialVersionUID. */ 
     
    3131         * @param sequenceId the sequence id 
    3232         */ 
    33         public MatchOccurence(int startindex, int endindex, int sequenceId) { 
     33        public MatchOccurrence(int startindex, int endindex, int sequenceId) { 
    3434                this.startindex = startindex; 
    3535                this.endindex = endindex; 
  • branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/SmithWatermanRepeated.java

    r1734 r1741  
    262262                                tmpal.setFirstSequence(tmpns1); 
    263263                                tmpal.setSecondSequence(tmpns2); 
    264                                 // tmpal.addOccurence(new 
    265                                 // MatchOccurence(start,alignment.get(0).getId())); 
    266                                 // tmpal.addOccurence(new 
    267                                 // MatchOccurence(start,alignment.get(1).getId())); 
    268264                                result.add(tmpal); 
    269265                        } 
  • branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/matrix/ObjectDistanceSubstitionMatrix.java

    r1740 r1741  
    220220                matrix.initialize(0); 
    221221 
    222                 int count = 0; 
    223                 final int size = uniqueTasks.size(); 
     222                //int count = 0; 
     223                //final int size = uniqueTasks.size(); 
    224224                for (final Iterator<ITask> it = uniqueTasks.iterator(); it.hasNext();) { 
    225225                        final ITask task1 = it.next(); 
    226                         count++; 
    227                         if (((count % (size / 100)) == 0)) { 
    228                                 Console.traceln(Level.INFO, 
    229                                                 (Math.round(((float) count / size) * 100)) + "%"); 
    230                         } 
     226                        //count++; 
     227                        //if (((count % (size / 100)) == 0)) { 
     228                        //      Console.traceln(Level.INFO, 
     229                        //                      (Math.round(((float) count / size) * 100)) + "%"); 
     230                        //} 
    231231                        for (final Iterator<ITask> jt = uniqueTasks.iterator(); jt 
    232232                                        .hasNext();) { 
  • branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/RuleUtils.java

    r1738 r1741  
    6464                final ISequenceInstance subsequence = taskFactory 
    6565                                .createNewTaskInstance(model); 
    66                 @SuppressWarnings("unused") 
     66                // TODO: Debugging output 
     67                /* 
    6768                int modelid = model.getId(); 
    6869                if(modelid == 5412) { 
     
    7778                        } 
    7879                         
    79                 } 
     80                }*/ 
    8081                // TODO: This is dirty, return this in addition with the sequence 
    8182                // instance instead 
     
    125126                                        } else if ((parent.get(startIndex).getTask().equals(tmpSel.getChildren().get(0).getId())) 
    126127                                                        || (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++; 
     128                                                taskBuilder.setChild(selection, parent.get(startIndex)); 
     129                                                taskBuilder.addChild(subsequence, selection); 
     130                                                taskBuilder.removeTaskInstance(parent, startIndex); 
     131                                                modelindex++; 
    131132                                                continue; 
    132133                                                 
     
    226227                                // Thread.currentThread().getName() + ": " + Math.round((float) 
    227228                                // count/size*100))+ "%"); 
    228                                 // System.out.println(message + " in thread" 
    229                                 // + Thread.currentThread().getName() + ": " 
    230                                 // + Math.round(((float) count / size) * 100) + "%"); 
     229                                 System.out.println(message + " in thread " 
     230                                + Thread.currentThread().getName() + ": " 
     231                                + Math.round(((float) count / size) * 100) + "%"); 
    231232                        } 
    232233                } else { 
     
    234235                        // Thread.currentThread().getName() + ": " +Math.round((float) 
    235236                        // count/size*100))+ "%"); 
    236                         // System.out.println(message + " in thread" 
    237                         // + Thread.currentThread().getName() + ": " 
    238                         // + Math.round(((float) count / size) * 100) + "%"); 
     237                         System.out.println(message + " in thread " 
     238                        + Thread.currentThread().getName() + ": " 
     239                        + Math.round(((float) count / size) * 100) + "%"); 
    239240 
    240241                } 
  • branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java

    r1740 r1741  
    2525import java.util.List; 
    2626import java.util.Map; 
     27import java.util.PriorityQueue; 
     28import java.util.Queue; 
    2729import java.util.Set; 
    2830import java.util.concurrent.ExecutorService; 
     
    3436import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.AlignmentAlgorithmFactory; 
    3537import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.Match; 
    36 import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.MatchOccurence; 
     38import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.MatchOccurrence; 
    3739import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.NumberSequence; 
    3840import de.ugoe.cs.autoquest.tasktrees.alignment.matrix.ObjectDistanceSubstitionMatrix; 
     
    7274public class SequenceForTaskDetectionRuleAlignment implements ISessionScopeRule { 
    7375 
    74         /** 
    75          * The Class RuleApplicationData. 
    76          */ 
    77         private static class RuleApplicationData implements Serializable { 
    78  
    79                 /** The Constant serialVersionUID. */ 
    80                 private static final long serialVersionUID = -7559657686755522960L; 
    81  
    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                  */ 
    86                 private final HashMap<Integer, ITask> number2task; 
    87  
    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                  */ 
    93                 private final HashSet<ITask> uniqueTasks; 
    94  
    95                 /** 
    96                  * The substitution matrix used by the alignment algorithm to be able to 
    97                  * compare distances of tasks 
    98                  */ 
    99                 private final ObjectDistanceSubstitionMatrix submat; 
    100  
    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                  */ 
    106                 private HashMap<Integer, List<MatchOccurence>> replacedOccurences; 
    107  
    108                 /** The list of all found matches */ 
    109                 private LinkedList<Match> matchseqs; 
    110  
    111                 /** 
    112                  * The generated NumberSequences. This is the integer representation of 
    113                  * the user sessions 
    114                  */ 
    115                 private ArrayList<NumberSequence> numberseqs; 
    116  
    117                 /** The list of newly created tasks (of one iteration). */ 
    118                 private final LinkedList<ITask> newTasks; 
    119  
    120                 /** The user sessions containing all EventTasks/Instances */ 
    121                 private final List<IUserSession> sessions; 
    122  
    123                 /** True if we replaced something in the user sessions in one iteration. */ 
    124                 private boolean detectedAndReplacedTasks; 
    125  
    126                 /** The result we give autoquest back */ 
    127                 private final RuleApplicationResult result; 
    128  
    129                 /** Stop Watch to measure performance */ 
    130                 private final StopWatch stopWatch; 
    131  
    132                 /** 
    133                  * Instantiates a new rule application data. 
    134                  * 
    135                  * @param sessions 
    136                  *            The user sessions 
    137                  */ 
    138                 private RuleApplicationData(List<IUserSession> sessions) { 
    139                         this.sessions = sessions; 
    140                         numberseqs = new ArrayList<NumberSequence>(); 
    141                         uniqueTasks = new HashSet<ITask>(); 
    142                         number2task = new HashMap<Integer, ITask>(); 
    143                         stopWatch = new StopWatch(); 
    144                         result = new RuleApplicationResult(); 
    145                         submat = new ObjectDistanceSubstitionMatrix(6, -3, false); 
    146                         newTasks = new LinkedList<ITask>(); 
    147                         this.detectedAndReplacedTasks = true; 
    148                 } 
    149  
    150                 /** 
    151                  * Detected and replaced tasks. 
    152                  * 
    153                  * @return true, if successful 
    154                  */ 
    155                 private boolean detectedAndReplacedTasks() { 
    156                         return detectedAndReplacedTasks; 
    157                 } 
    158  
    159                 /** 
    160                  * Gets the matchseqs. 
    161                  * 
    162                  * @return the matchseqs 
    163                  */ 
    164                 public LinkedList<Match> getMatchseqs() { 
    165                         return matchseqs; 
    166                 } 
    167  
    168                 /** 
    169                  * Gets the new tasks. 
    170                  * 
    171                  * @return the new tasks 
    172                  */ 
    173                 public LinkedList<ITask> getNewTasks() { 
    174                         return newTasks; 
    175                 } 
    176  
    177                 /** 
    178                  * Gets the number2task. 
    179                  * 
    180                  * @return the number2task HashMap 
    181                  */ 
    182                 private HashMap<Integer, ITask> getNumber2Task() { 
    183                         return number2task; 
    184                 } 
    185  
    186                 /** 
    187                  * Gets the number sequences. 
    188                  * 
    189                  * @return the number sequences 
    190                  */ 
    191                 private ArrayList<NumberSequence> getNumberSequences() { 
    192                         return numberseqs; 
    193                 } 
    194  
    195                 /** 
    196                  * Gets the replaced occurrences. 
    197                  * 
    198                  * @return the replaced occurences 
    199                  */ 
    200                 public HashMap<Integer, List<MatchOccurence>> getReplacedOccurrences() { 
    201                         return replacedOccurences; 
    202                 } 
    203  
    204                 /** 
    205                  * Gets the result. 
    206                  * 
    207                  * @return the result 
    208                  */ 
    209                 private RuleApplicationResult getResult() { 
    210                         return result; 
    211                 } 
    212  
    213                 /** 
    214                  * Gets the sessions. 
    215                  * 
    216                  * @return the UserSessions as List. 
    217                  */ 
    218                 private List<IUserSession> getSessions() { 
    219                         return sessions; 
    220                 } 
    221  
    222                 /** 
    223                  * Gets the stop watch. 
    224                  * 
    225                  * @return the stopWatch 
    226                  */ 
    227                 private StopWatch getStopWatch() { 
    228                         return stopWatch; 
    229                 } 
    230  
    231                 /** 
    232                  * Gets the submat. 
    233                  * 
    234                  * @return the submat 
    235                  */ 
    236                 private ObjectDistanceSubstitionMatrix getSubmat() { 
    237                         return submat; 
    238                 } 
    239  
    240                 /** 
    241                  * Gets the unique tasks. 
    242                  * 
    243                  * @return the unique tasks 
    244                  */ 
    245                 private HashSet<ITask> getUniqueTasks() { 
    246                         return uniqueTasks; 
    247                 } 
    248  
    249                 /** 
    250                  * New task created. 
    251                  * 
    252                  * @param task 
    253                  *            the task 
    254                  */ 
    255                 private void newTaskCreated(ITask task) { 
    256                         number2task.put(task.getId(), task); 
    257                         newTasks.add(task); 
    258                 } 
    259  
    260                 /** 
    261                  * Reset newly created tasks. 
    262                  */ 
    263                 synchronized private void resetNewlyCreatedTasks() { 
    264                         uniqueTasks.addAll(newTasks); 
    265                         newTasks.clear(); 
    266                 } 
    267  
    268                 /** 
    269                  * Sets the number sequences. 
    270                  * 
    271                  * @param numberseqs 
    272                  *            the new number sequences 
    273                  */ 
    274                 private void setNumberSequences(ArrayList<NumberSequence> numberseqs) { 
    275                         this.numberseqs = numberseqs; 
    276                 } 
    277  
    278                 /** 
    279                  * Sets the replaced occurences. 
    280                  * 
    281                  * @param replacedOccurences 
    282                  *            the replaced occurences 
    283                  */ 
    284                 public void setReplacedOccurences( 
    285                                 HashMap<Integer, List<MatchOccurence>> replacedOccurences) { 
    286                         this.replacedOccurences = replacedOccurences; 
    287                 } 
    288  
    289                 /** 
    290                  * Update substitution matrix. 
    291                  */ 
    292                 private void updateSubstitutionMatrix() { 
    293                         submat.update(getNewTasks()); 
    294                         resetNewlyCreatedTasks(); 
    295                 } 
    296  
    297         } 
    298  
     76         
    29977        /** The n threads. */ 
    30078        public static int nThreads = Runtime.getRuntime().availableProcessors() - 1; 
     
    380158                        detectAndReplaceIterations(appData); 
    381159                        appData.getStopWatch().start("task replacement"); 
     160                        //Just does anything if the substitution matrix is created with the option to do so 
    382161                        appData.updateSubstitutionMatrix(); 
    383162                        detectAndReplaceTasks(appData); // 
    384163                        appData.getStopWatch().stop("task replacement"); 
    385164                        appData.getStopWatch().stop("whole loop"); 
    386                         appData.getStopWatch().dumpStatistics(System.out); 
     165                        //appData.getStopWatch().dumpStatistics(System.out); 
    387166                        appData.getStopWatch().reset(); 
    388167 
     
    400179                                        RuleApplicationStatus.FINISHED); 
    401180                } 
    402                 new TaskTreeValidator().validate(appData.getSessions()); 
     181                //new TaskTreeValidator().validate(appData.getSessions()); 
    403182                return appData.getResult(); 
    404183        } 
     
    488267                        @Override 
    489268                        public int compare(Match m1, Match m2) { 
    490                                 return m2.occurenceCount() - m1.occurenceCount(); 
    491  
     269                                int cmp = m2.occurenceCount() - m1.occurenceCount(); 
     270                                if(cmp != 0) { 
     271                                        return cmp; 
     272                                } 
     273                                else { 
     274                                        cmp = m2.size()-m1.size(); 
     275                                        if(cmp != 0) { 
     276                                                return cmp; 
     277                                        } 
     278                                        else { 
     279                                                //This should rarely happen 
     280                                                cmp = m2.ocurrenceIDSum()-m1.ocurrenceIDSum(); 
     281                                                if(cmp !=0) { 
     282                                                        return cmp; 
     283                                                } 
     284                                                else { 
     285                                                        cmp = m2.taskIdSum()-m1.taskIdSum(); 
     286                                                         
     287                                                        return cmp; 
     288                                                } 
     289                                        } 
     290                                } 
    492291                        } 
    493292                }; 
     293                 
    494294                Collections.sort(appData.getMatchseqs(), comparator); 
    495295                appData.getStopWatch().stop("detecting tasks"); 
     296         
    496297 
    497298                // Replace matches in the sessions 
     
    502303        } 
    503304 
    504         /** 
    505          * Generate pairwise alignments. 
    506          * 
    507          * @param appData 
    508          *            the app data 
    509          */ 
    510         private void generatePairwiseAlignments(RuleApplicationData appData) { 
    511                 final int numberSeqSize = appData.getNumberSequences().size(); 
    512                 appData.matchseqs = new LinkedList<Match>(); 
    513                 Console.traceln(Level.INFO, "generating pairwise alignments from " 
    514                                 + numberSeqSize + " sessions with " + nThreads + " threads"); 
    515  
    516                 int newThreads = nThreads; 
    517                 if (numberSeqSize < nThreads) { 
    518                         newThreads = numberSeqSize; 
    519                 } 
    520  
    521                 final ExecutorService executor = Executors 
    522                                 .newFixedThreadPool(newThreads); 
    523                 final int interval = numberSeqSize / newThreads; 
    524                 int rest = numberSeqSize % newThreads; 
    525                 Console.traceln(Level.FINE, "Interval: " + interval + " Rest: " + rest); 
    526                 for (int i = 0; i <= (numberSeqSize - interval); i += interval) { 
    527                         int offset = 0; 
    528                         if (rest != 0) { 
    529                                 offset = 1; 
    530                                 rest--; 
    531                         } 
    532                  
    533                         final int from = i; 
    534                         final int to = i + interval+offset-1; 
    535                         Console.traceln(Level.FINER, "Aligning: Creating thread for sessions " + from + " till " + to); 
    536                         final ParallelPairwiseAligner aligner = new ParallelPairwiseAligner( 
    537                                         appData, from, to); 
    538                         executor.execute(aligner); 
    539                         i+=offset; 
    540                 } 
    541                 executor.shutdown(); 
    542                 try { 
    543                         executor.awaitTermination(2, TimeUnit.HOURS); 
    544                 } catch (final InterruptedException e) { 
    545                         e.printStackTrace(); 
    546                 } 
    547         } 
    548  
     305         
     306        //TODO: DEBUG METHOD 
     307        @SuppressWarnings("unused") 
     308        private void printMatches(RuleApplicationData appData) { 
     309                LinkedList<Match> matchseqs = appData.getMatchseqs(); 
     310                if(iteration>1) { 
     311                System.out.println("PRINTING MATCHES"); 
     312                for (Iterator<Match> it = matchseqs.iterator(); it.hasNext();) { 
     313                        Match m = it.next(); 
     314                        m.getFirstSequence().printSequence(); 
     315                        m.getSecondSequence().printSequence(); 
     316                        for(Iterator<MatchOccurrence> jt = m.getOccurences().iterator();jt.hasNext();) { 
     317                                MatchOccurrence mo = jt.next(); 
     318                                System.out.print(mo.getSequenceId() + " "); 
     319                        } 
     320                        System.out.println(); 
     321                        System.out.println(); 
     322                } 
     323                } 
     324        } 
     325 
     326         
    549327        /** 
    550328         * <p> 
     
    879657        } 
    880658 
     659        private void prepareReplacements(RuleApplicationData appData) { 
     660                appData.initializeQueues(appData.getSessions().size()); 
     661                final int matchSeqSize = appData.getMatchseqs().size(); 
     662                int count = 0; 
     663                for (Iterator<Match> it = appData.getMatchseqs().iterator(); it.hasNext();) { 
     664                        Match m = it.next(); 
     665                        for(Iterator<MatchOccurrence> jt=m.getOccurences().iterator();jt.hasNext();) { 
     666                                MatchOccurrence mo = jt.next(); 
     667                                 
     668                                Match emptyMatch = null; 
     669                                try { 
     670                                        emptyMatch = m.cloneWithoutOccurences(); 
     671                                } catch (CloneNotSupportedException e) { 
     672                                        e.printStackTrace(); 
     673                                } 
     674                                emptyMatch.addOccurence(mo); 
     675                                appData.getPlannedReplacements()[mo.getSequenceId()].add(m); 
     676                        } 
     677                        count++; 
     678                         
     679                } 
     680        } 
     681         
    881682        /** 
    882683         * Replace matches. 
     
    886687         */ 
    887688        private void replaceMatches(RuleApplicationData appData) { 
    888                 appData.setReplacedOccurences(new HashMap<Integer, List<MatchOccurence>>()); 
     689                appData.setReplacedOccurences(new HashMap<Integer, List<MatchOccurrence>>()); 
    889690 
    890691                final int matchSeqSize = appData.getMatchseqs().size(); 
    891  
     692                int count = 0; 
    892693                for (int i = 0; i < matchSeqSize; i++) { 
    893  
     694                        count++; 
    894695                        // Every pattern consists of 2 sequences, therefore the minimum 
    895696                        // occurrences here is 2. 
    896697                        // We just need the sequences also occurring in other sequences 
    897698                        // as well 
     699                        RuleUtils.printProgressPercentage("Replacement progress", count, matchSeqSize); 
    898700                        if (appData.getMatchseqs().get(i).occurenceCount() > 2) { 
    899701 
    900702                                final ISequence task = matchAsSequence(appData, appData 
    901703                                                .getMatchseqs().get(i)); 
    902                                 invalidOccurence: for (final Iterator<MatchOccurence> it = appData 
     704                                invalidOccurence: for (final Iterator<MatchOccurrence> it = appData 
    903705                                                .getMatchseqs().get(i).getOccurences().iterator(); it 
    904706                                                .hasNext();) { 
    905                                         final MatchOccurence oc = it.next(); 
     707                                        final MatchOccurrence oc = it.next(); 
    906708 
    907709                                        // Check if nothing has been replaced in the sequence we 
     
    911713                                                appData.getReplacedOccurrences().put( 
    912714                                                                oc.getSequenceId(), 
    913                                                                 new LinkedList<MatchOccurence>()); 
     715                                                                new LinkedList<MatchOccurrence>()); 
    914716                                        } else { 
    915717                                                // check if we have any replaced occurence with 
     
    928730                                                // harmonized. 
    929731 
    930                                                 for (final Iterator<MatchOccurence> jt = appData 
     732                                                for (final Iterator<MatchOccurrence> jt = appData 
    931733                                                                .getReplacedOccurrences() 
    932734                                                                .get(oc.getSequenceId()).iterator(); jt 
    933735                                                                .hasNext();) { 
    934                                                         final MatchOccurence tmpOC = jt.next(); 
     736                                                        final MatchOccurrence tmpOC = jt.next(); 
    935737 
    936738                                                        if ((oc.getStartindex() >= tmpOC.getStartindex()) 
     
    1004806 
    1005807        /** 
     808         * Generate pairwise alignments. 
     809         * 
     810         * @param appData 
     811         *            the app data 
     812         */ 
     813        private void generatePairwiseAlignments(RuleApplicationData appData) { 
     814                final int numberSeqSize = appData.getNumberSequences().size(); 
     815                appData.matchseqs = new LinkedList<Match>(); 
     816                Console.traceln(Level.INFO, "generating pairwise alignments from " 
     817                                + numberSeqSize + " sessions with " + nThreads + " threads"); 
     818 
     819                int newThreads = nThreads; 
     820                if (numberSeqSize < nThreads) { 
     821                        newThreads = numberSeqSize; 
     822                } 
     823 
     824                final ExecutorService executor = Executors 
     825                                .newFixedThreadPool(newThreads); 
     826                final int interval = numberSeqSize / newThreads; 
     827                int rest = numberSeqSize % newThreads; 
     828                Console.traceln(Level.FINE, "Interval: " + interval + " Rest: " + rest); 
     829                for (int i = 0; i <= (numberSeqSize - interval); i += interval) { 
     830                        int offset = 0; 
     831                        if (rest != 0) { 
     832                                offset = 1; 
     833                                rest--; 
     834                        } 
     835                 
     836                        final int from = i; 
     837                        final int to = i + interval+offset-1; 
     838                        Console.traceln(Level.FINER, "Aligning: Creating thread for sessions " + from + " till " + to); 
     839                        final ParallelPairwiseAligner aligner = new ParallelPairwiseAligner( 
     840                                        appData, from, to); 
     841                        executor.execute(aligner); 
     842                        i+=offset; 
     843                } 
     844                executor.shutdown(); 
     845                try { 
     846                        executor.awaitTermination(2, TimeUnit.HOURS); 
     847                } catch (final InterruptedException e) { 
     848                        e.printStackTrace(); 
     849                } 
     850        } 
     851 
     852         
     853         
     854         
     855        /** 
    1006856         * Search matches in all sessions. 
    1007857         * 
     
    1089939                        this.to = to; 
    1090940                } 
    1091  
    1092941                /* 
    1093942                 * (non-Javadoc) 
     
    1124973                                                                .iterator(); jt.hasNext();) { 
    1125974                                                        final int start = jt.next(); 
    1126                                                         pattern.addOccurence(new MatchOccurence(start, 
     975                                                        pattern.addOccurence(new MatchOccurrence(start, 
    1127976                                                                        start + pattern.size(), j)); 
    1128977                                                } 
     
    1132981                } 
    1133982        } 
     983         
     984         
    1134985 
    1135986        /** 
     
    11931044                } 
    11941045        } 
     1046         
     1047        /** 
     1048         * The Class RuleApplicationData. 
     1049         */ 
     1050        private static class RuleApplicationData implements Serializable { 
     1051 
     1052                /** The Constant serialVersionUID. */ 
     1053                private static final long serialVersionUID = -7559657686755522960L; 
     1054 
     1055                /** 
     1056                 * The number2task HashMap. Since we align the tasks just by their 
     1057                 * integer id, we need this to convert them back to Tasks again 
     1058                 */ 
     1059                private final HashMap<Integer, ITask> number2task; 
     1060 
     1061                /** 
     1062                 * The unique tasks, keeps track about all unique tasks TODO: We 
     1063                 * Actually just need number2task here, this structure can be removed in 
     1064                 * the future. 
     1065                 */ 
     1066                private final HashSet<ITask> uniqueTasks; 
     1067 
     1068                /** 
     1069                 * The substitution matrix used by the alignment algorithm to be able to 
     1070                 * compare distances of tasks 
     1071                 */ 
     1072                private final ObjectDistanceSubstitionMatrix submat; 
     1073 
     1074                /** 
     1075                 * HashMap for keeping track in which sequence which replacement has 
     1076                 * been performed. Neccessary for updating the indices of other 
     1077                 * occurrences accordingly 
     1078                 */ 
     1079                private HashMap<Integer, List<MatchOccurrence>> replacedOccurences; 
     1080                 
     1081                private Queue<Match>[] plannedReplacements; 
     1082                 
     1083 
     1084                /** The list of all found matches */ 
     1085                private LinkedList<Match> matchseqs; 
     1086 
     1087                /** 
     1088                 * The generated NumberSequences. This is the integer representation of 
     1089                 * the user sessions 
     1090                 */ 
     1091                private ArrayList<NumberSequence> numberseqs; 
     1092 
     1093                /** The list of newly created tasks (of one iteration). */ 
     1094                private final LinkedList<ITask> newTasks; 
     1095 
     1096                /** The user sessions containing all EventTasks/Instances */ 
     1097                private final List<IUserSession> sessions; 
     1098 
     1099                /** True if we replaced something in the user sessions in one iteration. */ 
     1100                private boolean detectedAndReplacedTasks; 
     1101 
     1102                /** The result we return from this rule */ 
     1103                private final RuleApplicationResult result; 
     1104 
     1105                /** Stop Watch to measure performance */ 
     1106                private final StopWatch stopWatch; 
     1107                 
     1108                 
     1109 
     1110                /** 
     1111                 * Instantiates a new rule application data. 
     1112                 * 
     1113                 * @param sessions 
     1114                 *            The user sessions 
     1115                 */ 
     1116                private RuleApplicationData(List<IUserSession> sessions) { 
     1117                        this.sessions = sessions; 
     1118                        numberseqs = new ArrayList<NumberSequence>(); 
     1119                        uniqueTasks = new HashSet<ITask>(); 
     1120                        number2task = new HashMap<Integer, ITask>(); 
     1121                        stopWatch = new StopWatch(); 
     1122                        result = new RuleApplicationResult(); 
     1123                        submat = new ObjectDistanceSubstitionMatrix(6, -3, false); 
     1124                        newTasks = new LinkedList<ITask>(); 
     1125                        this.detectedAndReplacedTasks = true; 
     1126                } 
     1127                 
     1128                private void initializeQueues(int size) { 
     1129                        plannedReplacements = new Queue[size]; 
     1130                        for(int i=0;i<size;i++) { 
     1131                                plannedReplacements[i] = new PriorityQueue<Match>(); 
     1132                        } 
     1133                } 
     1134 
     1135                public Queue<Match>[] getPlannedReplacements() { 
     1136                        return plannedReplacements; 
     1137                } 
     1138 
     1139                public void setPlannedReplacements(Queue<Match>[] plannedReplacements) { 
     1140                        this.plannedReplacements = plannedReplacements; 
     1141                } 
     1142 
     1143                /** 
     1144                 * Detected and replaced tasks. 
     1145                 * 
     1146                 * @return true, if successful 
     1147                 */ 
     1148                private boolean detectedAndReplacedTasks() { 
     1149                        return detectedAndReplacedTasks; 
     1150                } 
     1151 
     1152                /** 
     1153                 * Gets the matchseqs. 
     1154                 * 
     1155                 * @return the matchseqs 
     1156                 */ 
     1157                public LinkedList<Match> getMatchseqs() { 
     1158                        return matchseqs; 
     1159                } 
     1160 
     1161                /** 
     1162                 * Gets the new tasks. 
     1163                 * 
     1164                 * @return the new tasks 
     1165                 */ 
     1166                public LinkedList<ITask> getNewTasks() { 
     1167                        return newTasks; 
     1168                } 
     1169 
     1170                /** 
     1171                 * Gets the number2task. 
     1172                 * 
     1173                 * @return the number2task HashMap 
     1174                 */ 
     1175                private HashMap<Integer, ITask> getNumber2Task() { 
     1176                        return number2task; 
     1177                } 
     1178 
     1179                /** 
     1180                 * Gets the number sequences. 
     1181                 * 
     1182                 * @return the number sequences 
     1183                 */ 
     1184                private ArrayList<NumberSequence> getNumberSequences() { 
     1185                        return numberseqs; 
     1186                } 
     1187 
     1188                /** 
     1189                 * Gets the replaced occurrences. 
     1190                 * 
     1191                 * @return the replaced occurences 
     1192                 */ 
     1193                public HashMap<Integer, List<MatchOccurrence>> getReplacedOccurrences() { 
     1194                        return replacedOccurences; 
     1195                } 
     1196 
     1197                /** 
     1198                 * Gets the result. 
     1199                 * 
     1200                 * @return the result 
     1201                 */ 
     1202                private RuleApplicationResult getResult() { 
     1203                        return result; 
     1204                } 
     1205 
     1206                /** 
     1207                 * Gets the sessions. 
     1208                 * 
     1209                 * @return the UserSessions as List. 
     1210                 */ 
     1211                private List<IUserSession> getSessions() { 
     1212                        return sessions; 
     1213                } 
     1214 
     1215                /** 
     1216                 * Gets the stop watch. 
     1217                 * 
     1218                 * @return the stopWatch 
     1219                 */ 
     1220                private StopWatch getStopWatch() { 
     1221                        return stopWatch; 
     1222                } 
     1223 
     1224                /** 
     1225                 * Gets the submat. 
     1226                 * 
     1227                 * @return the submat 
     1228                 */ 
     1229                private ObjectDistanceSubstitionMatrix getSubmat() { 
     1230                        return submat; 
     1231                } 
     1232 
     1233                /** 
     1234                 * Gets the unique tasks. 
     1235                 * 
     1236                 * @return the unique tasks 
     1237                 */ 
     1238                private HashSet<ITask> getUniqueTasks() { 
     1239                        return uniqueTasks; 
     1240                } 
     1241 
     1242                /** 
     1243                 * New task created. 
     1244                 * 
     1245                 * @param task 
     1246                 *            can be called when new tasks are created to keep track of all newly created tasks 
     1247                 */ 
     1248                private void newTaskCreated(ITask task) { 
     1249                        number2task.put(task.getId(), task); 
     1250                        newTasks.add(task); 
     1251                } 
     1252 
     1253                /** 
     1254                 * Reset newly created tasks. 
     1255                 */ 
     1256                synchronized private void resetNewlyCreatedTasks() { 
     1257                        uniqueTasks.addAll(newTasks); 
     1258                        newTasks.clear(); 
     1259                } 
     1260 
     1261                /** 
     1262                 * Sets the number sequences. 
     1263                 * 
     1264                 * @param numberseqs 
     1265                 *            the new number sequences 
     1266                 */ 
     1267                private void setNumberSequences(ArrayList<NumberSequence> numberseqs) { 
     1268                        this.numberseqs = numberseqs; 
     1269                } 
     1270 
     1271                /** 
     1272                 * Sets the replaced occurences. 
     1273                 * 
     1274                 * @param replacedOccurences 
     1275                 *            the replaced occurences 
     1276                 */ 
     1277                public void setReplacedOccurences( 
     1278                                HashMap<Integer, List<MatchOccurrence>> replacedOccurences) { 
     1279                        this.replacedOccurences = replacedOccurences; 
     1280                } 
     1281 
     1282                /** 
     1283                 * Update substitution matrix. 
     1284                 */ 
     1285                private void updateSubstitutionMatrix() { 
     1286                        submat.update(getNewTasks()); 
     1287                        resetNewlyCreatedTasks(); 
     1288                } 
     1289 
     1290        } 
     1291 
     1292         
    11951293 
    11961294} 
Note: See TracChangeset for help on using the changeset viewer.