Ignore:
Timestamp:
08/17/12 09:05:19 (12 years ago)
Author:
sherbold
Message:
  • adapted to quest coding style
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/quest-core-coverage/src/main/java/de/ugoe/cs/quest/coverage/SequenceTools.java

    r547 r559  
     1 
    12package de.ugoe.cs.quest.coverage; 
    23 
     
    2223 */ 
    2324public class SequenceTools { 
    24          
    25         /** 
    26          * <p> 
    27          * Private constructor to prevent initializing of the class. 
    28          * </p> 
    29          */ 
    30         private SequenceTools() { 
    31                  
    32         } 
    3325 
    34         /** 
    35          * <p> 
    36          * Calculates the weights for all sequences passed to this function as 
    37          * defined by the stochastic process and stores them in a {@link Map}. The 
    38          * weights are normalized, i.e., the sum of all weights contained in this 
    39          * map is 1. 
    40          * </p> 
    41          *  
    42          * @param process 
    43          *            process used for weight calculation 
    44          * @param sequences 
    45          *            sequences for which the weights are calculated 
    46          * @return {@link Map} of weights 
    47          */ 
    48         public static Map<List<Event>, Double> generateWeights( 
    49                         IStochasticProcess process, 
    50                         Collection<List<Event>> sequences) { 
    51                 Map<List<Event>, Double> subSeqWeights = new LinkedHashMap<List<Event>, Double>(); 
    52                 if (sequences != null && !sequences.isEmpty()) { 
    53                         if (process != null) { 
    54                                 double sum = 0.0; 
    55                                 for (List<Event> sequence : sequences) { 
    56                                         double prob = process.getProbability(sequence); 
    57                                         subSeqWeights.put(sequence, prob); 
    58                                         sum += prob; 
    59                                 } 
    60                                 if (sum < 1.0) { 
    61                                         for (Map.Entry<List<Event>, Double> entry : subSeqWeights 
    62                                                         .entrySet()) { 
    63                                                 entry.setValue(entry.getValue() / sum); 
    64                                         } 
    65                                 } 
    66                         } else { 
    67                                 for( List<Event> sequence : sequences ) { 
    68                                         subSeqWeights.put(sequence, 0.0d); 
    69                                 } 
    70                         } 
    71                 } 
    72                 return subSeqWeights; 
    73         } 
     26    /** 
     27     * <p> 
     28     * Private constructor to prevent initializing of the class. 
     29     * </p> 
     30     */ 
     31    private SequenceTools() { 
    7432 
    75         /** 
    76          * <p> 
    77          * Calculates the number of all existing sequences of a given length, 
    78          * regardless whether they are possible or not. 
    79          * </p> 
    80          *  
    81          * @param process 
    82          *            stochastic process whose symbols are the basis for this 
    83          *            calculation 
    84          * @param length 
    85          *            lenght of the sequences 
    86          * @return numStates^length 
    87          * @throws InvalidParameterException 
    88          *             thrown if length less or equal to 0 
    89          */ 
    90         public static long numSequences(IStochasticProcess process, int length) { 
    91                 if (length <= 0) { 
    92                         throw new InvalidParameterException( 
    93                                         "length must be a positive integer"); 
    94                 } 
    95                 long result = 0; 
    96                 if (process != null) { 
    97                         result = (long) Math.pow(process.getNumSymbols(), length); 
    98                 } 
    99                 return result; 
    100         } 
     33    } 
    10134 
    102         /** 
    103          * <p> 
    104          * Creates a {@link Set} of all subsequences of a given length that are 
    105          * contained in a sequence collection. 
    106          * </p> 
    107          *  
    108          * @param sequences 
    109          *            sequences from which the subsequences are extracted 
    110          * @param length 
    111          *            length of the subsequences 
    112          * @return {@link Set} of all subsequences 
    113          * @throws InvalidParameterException 
    114          *             thrown if length less or equal to 0 
    115          */ 
    116         public static Set<List<Event>> containedSubSequences( 
    117                         Collection<List<Event>> sequences, int length) { 
    118                 if (length <= 0) { 
    119                         throw new InvalidParameterException( 
    120                                         "length must be a positive integer"); 
    121                 } 
    122                 Set<List<Event>> containedSubSeqs = new LinkedHashSet<List<Event>>(); 
    123                 if (sequences != null) { 
    124                         for (List<Event> sequence : sequences) { 
    125                                 List<Event> subSeq = new LinkedList<Event>(); 
    126                                 boolean minLengthReached = false; 
    127                                 for (Event event : sequence) { 
    128                                         subSeq.add(event); 
    129                                         if (!minLengthReached) { 
    130                                                 if (subSeq.size() == length) { 
    131                                                         minLengthReached = true; 
    132                                                 } 
    133                                         } else { 
    134                                                 subSeq.remove(0); 
    135                                         } 
    136                                         if (minLengthReached) { 
    137                                                 if (!containedSubSeqs.contains(subSeq)) { 
    138                                                         containedSubSeqs.add(new LinkedList<Event>( 
    139                                                                         subSeq)); 
    140                                                 } 
    141                                         } 
    142                                 } 
    143                         } 
    144                 } 
    145                 return containedSubSeqs; 
    146         } 
     35    /** 
     36     * <p> 
     37     * Calculates the weights for all sequences passed to this function as defined by the stochastic 
     38     * process and stores them in a {@link Map}. The weights are normalized, i.e., the sum of all 
     39     * weights contained in this map is 1. 
     40     * </p> 
     41     *  
     42     * @param process 
     43     *            process used for weight calculation 
     44     * @param sequences 
     45     *            sequences for which the weights are calculated 
     46     * @return {@link Map} of weights 
     47     */ 
     48    public static Map<List<Event>, Double> generateWeights(IStochasticProcess process, 
     49                                                           Collection<List<Event>> sequences) 
     50    { 
     51        Map<List<Event>, Double> subSeqWeights = new LinkedHashMap<List<Event>, Double>(); 
     52        if (sequences != null && !sequences.isEmpty()) { 
     53            if (process != null) { 
     54                double sum = 0.0; 
     55                for (List<Event> sequence : sequences) { 
     56                    double prob = process.getProbability(sequence); 
     57                    subSeqWeights.put(sequence, prob); 
     58                    sum += prob; 
     59                } 
     60                if (sum < 1.0) { 
     61                    for (Map.Entry<List<Event>, Double> entry : subSeqWeights.entrySet()) { 
     62                        entry.setValue(entry.getValue() / sum); 
     63                    } 
     64                } 
     65            } 
     66            else { 
     67                for (List<Event> sequence : sequences) { 
     68                    subSeqWeights.put(sequence, 0.0d); 
     69                } 
     70            } 
     71        } 
     72        return subSeqWeights; 
     73    } 
     74 
     75    /** 
     76     * <p> 
     77     * Calculates the number of all existing sequences of a given length, regardless whether they 
     78     * are possible or not. 
     79     * </p> 
     80     *  
     81     * @param process 
     82     *            stochastic process whose symbols are the basis for this calculation 
     83     * @param length 
     84     *            lenght of the sequences 
     85     * @return numStates^length 
     86     * @throws InvalidParameterException 
     87     *             thrown if length less or equal to 0 
     88     */ 
     89    public static long numSequences(IStochasticProcess process, int length) { 
     90        if (length <= 0) { 
     91            throw new InvalidParameterException("length must be a positive integer"); 
     92        } 
     93        long result = 0; 
     94        if (process != null) { 
     95            result = (long) Math.pow(process.getNumSymbols(), length); 
     96        } 
     97        return result; 
     98    } 
     99 
     100    /** 
     101     * <p> 
     102     * Creates a {@link Set} of all subsequences of a given length that are contained in a sequence 
     103     * collection. 
     104     * </p> 
     105     *  
     106     * @param sequences 
     107     *            sequences from which the subsequences are extracted 
     108     * @param length 
     109     *            length of the subsequences 
     110     * @return {@link Set} of all subsequences 
     111     * @throws InvalidParameterException 
     112     *             thrown if length less or equal to 0 
     113     */ 
     114    public static Set<List<Event>> containedSubSequences(Collection<List<Event>> sequences, 
     115                                                         int length) 
     116    { 
     117        if (length <= 0) { 
     118            throw new InvalidParameterException("length must be a positive integer"); 
     119        } 
     120        Set<List<Event>> containedSubSeqs = new LinkedHashSet<List<Event>>(); 
     121        if (sequences != null) { 
     122            for (List<Event> sequence : sequences) { 
     123                List<Event> subSeq = new LinkedList<Event>(); 
     124                boolean minLengthReached = false; 
     125                for (Event event : sequence) { 
     126                    subSeq.add(event); 
     127                    if (!minLengthReached) { 
     128                        if (subSeq.size() == length) { 
     129                            minLengthReached = true; 
     130                        } 
     131                    } 
     132                    else { 
     133                        subSeq.remove(0); 
     134                    } 
     135                    if (minLengthReached) { 
     136                        if (!containedSubSeqs.contains(subSeq)) { 
     137                            containedSubSeqs.add(new LinkedList<Event>(subSeq)); 
     138                        } 
     139                    } 
     140                } 
     141            } 
     142        } 
     143        return containedSubSeqs; 
     144    } 
    147145} 
Note: See TracChangeset for help on using the changeset viewer.