Ignore:
Timestamp:
07/05/11 15:18:56 (13 years ago)
Author:
sherbold
Message:
  • code documentation
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/EventBenchCore/src/de/ugoe/cs/eventbench/coverage/CoverageCalculator.java

    r102 r106  
    1212import de.ugoe.cs.eventbench.models.IStochasticProcess; 
    1313 
     14/** 
     15 * <p> 
     16 * This class calculates various types of sequence coverage. 
     17 * </p> 
     18 *  
     19 * @author Steffen Herbold 
     20 * @version 1.0 
     21 */ 
    1422public class CoverageCalculator { 
    15          
     23 
     24        /** 
     25         * <p> 
     26         * Stochastic process that is the foundation for probabilistic coverages and 
     27         * coverages with reference to all possible sequences. 
     28         * </p> 
     29         */ 
    1630        private final IStochasticProcess process; 
     31 
     32        /** 
     33         * <p> 
     34         * Sequences for which the coverage is calculated. 
     35         * </p> 
     36         */ 
    1737        private final Collection<List<? extends Event<?>>> sequences; 
     38 
     39        /** 
     40         * <p> 
     41         * Length of the subsequences in relation to which the covarage is 
     42         * calculated. 
     43         * </p> 
     44         */ 
    1845        private final int length; 
    19          
     46 
     47        /** 
     48         * <p> 
     49         * All subsequences of {@link #length} of {@link #sequences}. 
     50         * </p> 
     51         */ 
    2052        private Collection<List<? extends Event<?>>> containedSubSeqs = null; 
     53 
     54        /** 
     55         * <p> 
     56         * All subsequences of {@link #length} that can be generated by 
     57         * {@link #process}. 
     58         * </p> 
     59         */ 
    2160        private Collection<List<? extends Event<?>>> allPossibleSubSeqs = null; 
     61 
     62        /** 
     63         * <p> 
     64         * The probabilities of al subsequences of {@link #length} according to 
     65         * {@link #process}. 
     66         * </p> 
     67         */ 
    2268        private Map<List<? extends Event<?>>, Double> subSeqWeights = null; 
    23          
    24          
    25         public CoverageCalculator(IStochasticProcess process, Collection<List<? extends Event<?>>> sequences, int length) { 
     69 
     70        /** 
     71         * <p> 
     72         * Constructor. Creates a new CoverageCalculator for a given stochastic 
     73         * process and generated sequences. 
     74         * </p> 
     75         *  
     76         * @param process 
     77         *            stochastic process used for coverage calculations; if it is 
     78         *            zero, not all calculations are possible 
     79         * @param sequences 
     80         *            sequences for which the coverage is calculated; must not be 
     81         *            null. 
     82         * @param length 
     83         *            length of the subsequences for which the coverage is analyzed; 
     84         *            must be >0 
     85         */ 
     86        public CoverageCalculator(IStochasticProcess process, 
     87                        Collection<List<? extends Event<?>>> sequences, int length) { 
     88                // TODO check parameters 
    2689                this.process = process; 
    2790                this.sequences = sequences; 
    2891                this.length = length; 
    2992        } 
    30          
    31  
     93 
     94        /** 
     95         * <p> 
     96         * Calculates the percentage of subsequences of length k that exist occur, 
     97         * including those that cannot be generated by {@link #process}. 
     98         * </p> 
     99         *  
     100         * @return coverage percentage 
     101         */ 
    32102        public double getCoverageAllNoWeight() { 
    33                 if( containedSubSeqs==null ) { 
     103                if (containedSubSeqs == null) { 
    34104                        containedSubSeqs = containedSubSequences(sequences, length); 
    35105                } 
    36                 return((double) containedSubSeqs.size())/numSequences(process, length); 
    37         } 
    38          
     106                return ((double) containedSubSeqs.size()) 
     107                                / numSequences(process, length); 
     108        } 
     109 
     110        /** 
     111         * <p> 
     112         * Calculates the percentage of subsequences of length k that occur and can 
     113         * generated by {@link #process}. 
     114         * </p> 
     115         *  
     116         * @return coverage percentage 
     117         */ 
    39118        public double getCoveragePossibleNoWeight() { 
    40                 if( containedSubSeqs==null ) { 
     119                if (containedSubSeqs == null) { 
    41120                        containedSubSeqs = containedSubSequences(sequences, length); 
    42121                } 
    43                 if( allPossibleSubSeqs==null ) { 
     122                if (allPossibleSubSeqs == null) { 
    44123                        allPossibleSubSeqs = process.generateSequences(length); 
    45124                } 
    46                 return((double) containedSubSeqs.size())/allPossibleSubSeqs.size(); 
    47         } 
    48          
     125                return ((double) containedSubSeqs.size()) / allPossibleSubSeqs.size(); 
     126        } 
     127 
     128        /** 
     129         * <p> 
     130         * Calculates the weight of the subsequences that occur with relation to 
     131         * {@link #process}, i.e., the mass of the subsequence probability covered 
     132         * by the subsequences. 
     133         * </p> 
     134         *  
     135         * @return coverage weight 
     136         */ 
    49137        public double getCoveragePossibleWeight() { 
    50                 if( containedSubSeqs==null ) { 
     138                if (containedSubSeqs == null) { 
    51139                        containedSubSeqs = containedSubSequences(sequences, length); 
    52140                } 
    53                 if( allPossibleSubSeqs==null ) { 
     141                if (allPossibleSubSeqs == null) { 
    54142                        allPossibleSubSeqs = process.generateSequences(length); 
    55143                } 
    56                 if( subSeqWeights==null ) { 
     144                if (subSeqWeights == null) { 
    57145                        subSeqWeights = generateWeights(process, allPossibleSubSeqs); 
    58146                } 
    59147                double weight = 0.0; 
    60                 for( List<? extends Event<?>> subSeq : containedSubSeqs ) { 
     148                for (List<? extends Event<?>> subSeq : containedSubSeqs) { 
    61149                        weight += subSeqWeights.get(subSeq); 
    62150                } 
    63151                return weight; 
    64152        } 
    65          
    66         private Map<List<? extends Event<?>>, Double> generateWeights(IStochasticProcess process, Collection<List<? extends Event<?>>> sequences) { 
     153 
     154        /** 
     155         * <p> 
     156         * Calculates the weights for all sequences passed to this function as 
     157         * defined by the stochastic process and stores them in a {@link Map}. 
     158         * </p> 
     159         *  
     160         * @param process 
     161         *            process used for weight calculation 
     162         * @param sequences 
     163         *            sequences for which the weights are calculated 
     164         * @return {@link Map} of weights 
     165         */ 
     166        private static Map<List<? extends Event<?>>, Double> generateWeights( 
     167                        IStochasticProcess process, 
     168                        Collection<List<? extends Event<?>>> sequences) { 
    67169                Map<List<? extends Event<?>>, Double> subSeqWeights = new LinkedHashMap<List<? extends Event<?>>, Double>(); 
    68170                double sum = 0.0; 
    69                 for( List<? extends Event<?>> sequence : sequences ) { 
     171                for (List<? extends Event<?>> sequence : sequences) { 
    70172                        double prob = 1.0; 
    71173                        List<Event<?>> context = new LinkedList<Event<?>>(); 
    72                         for( Event<?> event : sequence ) { 
     174                        for (Event<?> event : sequence) { 
    73175                                prob *= process.getProbability(context, event); 
    74176                                context.add(event); 
     
    77179                        sum += prob; 
    78180                } 
    79                 if( sum<1.0 ) { 
    80                         for( Map.Entry<List<? extends Event<?>>, Double> entry : subSeqWeights.entrySet() ) { 
    81                                 entry.setValue(entry.getValue()/sum); 
     181                if (sum < 1.0) { 
     182                        for (Map.Entry<List<? extends Event<?>>, Double> entry : subSeqWeights 
     183                                        .entrySet()) { 
     184                                entry.setValue(entry.getValue() / sum); 
    82185                        } 
    83186                } 
    84187                return subSeqWeights; 
    85188        } 
    86          
    87         private long numSequences(IStochasticProcess process, int length) { 
     189 
     190        /** 
     191         * <p> 
     192         * Calculates the number of all existing sequences of a given length, 
     193         * regardless whether they are possible or not. 
     194         * </p> 
     195         *  
     196         * @param process 
     197         *            stochastic process whose symbols are the basis for this 
     198         *            calculation 
     199         * @param length 
     200         *            lenght of the sequences 
     201         * @return numStates^length 
     202         */ 
     203        private static long numSequences(IStochasticProcess process, int length) { 
    88204                return (long) Math.pow(process.getNumStates(), length); 
    89205        } 
    90          
    91         // O(numSeq*lenSeq)      
    92         private Set<List<? extends Event<?>>> containedSubSequences(Collection<List<? extends Event<?>>> sequences, int length) { 
     206 
     207        /** 
     208         * <p> 
     209         * Creates a {@link Set} of all subsequences of a given length that are 
     210         * contained in a sequence collection. 
     211         * </p> 
     212         *  
     213         * @param sequences 
     214         *            sequences from which the subsequences are extracted 
     215         * @param length 
     216         *            length of the subsequences 
     217         * @return {@link Set} of all subsequences 
     218         */ 
     219        private static Set<List<? extends Event<?>>> containedSubSequences( 
     220                        Collection<List<? extends Event<?>>> sequences, int length) { 
    93221                Set<List<? extends Event<?>>> containedSubSeqs = new LinkedHashSet<List<? extends Event<?>>>(); 
    94222                List<Event<?>> subSeq = new LinkedList<Event<?>>(); 
    95223                boolean minLengthReached = false; 
    96                 for( List<? extends Event<?>> sequence : sequences ) { 
    97                         for( Event<?> event : sequence ) { 
     224                for (List<? extends Event<?>> sequence : sequences) { 
     225                        for (Event<?> event : sequence) { 
    98226                                subSeq.add(event); 
    99                                 if( !minLengthReached ) { 
    100                                         if( subSeq.size()==length ) { 
    101                                                 minLengthReached=true; 
     227                                if (!minLengthReached) { 
     228                                        if (subSeq.size() == length) { 
     229                                                minLengthReached = true; 
    102230                                        } 
    103231                                } else { 
    104232                                        subSeq.remove(0); 
    105233                                } 
    106                                 if( minLengthReached ) { 
    107                                         if( !containedSubSeqs.contains(subSeq) ) { 
     234                                if (minLengthReached) { 
     235                                        if (!containedSubSeqs.contains(subSeq)) { 
    108236                                                containedSubSeqs.add(new LinkedList<Event<?>>(subSeq)); 
    109237                                        } 
     
    113241                return containedSubSeqs; 
    114242        } 
    115          
     243 
    116244} 
Note: See TracChangeset for help on using the changeset viewer.