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-testgeneration/src/main/java/de/ugoe/cs/quest/testgeneration/RandomWalkGenerator.java

    r547 r559  
     1 
    12package de.ugoe.cs.quest.testgeneration; 
    23 
     
    2021public class RandomWalkGenerator { 
    2122 
    22         /** 
    23         * <p> 
    24         * Number of sequences in the test suite. 
    25         * </p> 
    26         */ 
    27         private final int numSequences; 
     23    /** 
     24    * <p> 
     25    * Number of sequences in the test suite. 
     26    * </p> 
     27    */ 
     28    private final int numSequences; 
    2829 
    29         /** 
    30         * <p> 
    31         * Minimal length of a test sequence. 
    32         * </p> 
    33         */ 
    34         private final int minLength; 
     30    /** 
     31    * <p> 
     32    * Minimal length of a test sequence. 
     33    * </p> 
     34    */ 
     35    private final int minLength; 
    3536 
    36         /** 
    37         * <p> 
    38         * Maximal length of a test sequence. 
    39         * </p> 
    40         */ 
    41         private final int maxLength; 
     37    /** 
     38    * <p> 
     39    * Maximal length of a test sequence. 
     40    * </p> 
     41    */ 
     42    private final int maxLength; 
    4243 
    43         /** 
    44          * <p> 
    45          * In case this member is true, only test cases that end in the global end 
    46          * event {@link Event#ENDEVENT} are generated. If it is false, the end event 
    47          * can be any event. 
    48          * </p> 
    49          */ 
    50         private final boolean validEnd; 
     44    /** 
     45     * <p> 
     46     * In case this member is true, only test cases that end in the global end event 
     47     * {@link Event#ENDEVENT} are generated. If it is false, the end event can be any event. 
     48     * </p> 
     49     */ 
     50    private final boolean validEnd; 
    5151 
    52         /** 
    53          * <p> 
    54          * Maximal number of random walks performed before aborting the test case 
    55          * generation and returning a test suite with less than 
    56          * {@link #numSequences} test cases. This can happen if too many generated 
    57          * random walks have to be discarded because their length is not between 
    58          * {@link #minLength} and {@link #maxLength}. 
    59          * </p> 
    60          */ 
    61         private final long maxIter; 
     52    /** 
     53     * <p> 
     54     * Maximal number of random walks performed before aborting the test case generation and 
     55     * returning a test suite with less than {@link #numSequences} test cases. This can happen if 
     56     * too many generated random walks have to be discarded because their length is not between 
     57     * {@link #minLength} and {@link #maxLength}. 
     58     * </p> 
     59     */ 
     60    private final long maxIter; 
    6261 
    63         /** 
    64         * <p> 
    65         * Actual number of random walks performed to generate the test suite. 
    66         * </p> 
    67         */ 
    68         private long actualIter = -1; 
     62    /** 
     63    * <p> 
     64    * Actual number of random walks performed to generate the test suite. 
     65    * </p> 
     66    */ 
     67    private long actualIter = -1; 
    6968 
    70         /** 
    71          * <p> 
    72          * Constructor. Creates a new RandomWalkGenerator and ensures the validity 
    73          * of the parameters: 
    74          * <ul> 
    75          * <li>numSequences must at least be 1 
    76          * <li>maxLength must at least be 1 
    77          * <li>minLength must be less than or equal to maxLength 
    78          * <li>maxIter must be greater than or equal to numSequences 
    79          * </ul> 
    80          * If one of these conditions is violated an 
    81          * {@link InvalidParameterException} is thrown. 
    82          * </p> 
    83          *  
    84          * @param numSequences 
    85          *            number of sequences desired for the test suite 
    86          * @param minLength 
    87          *            minimal length of a test sequence 
    88          * @param maxLength 
    89          *            maximal length of a test sequence 
    90          * @param validEnd 
    91          *            defines if test cases have to end with the global end event 
    92          *            {@link Event#ENDEVENT} (see {@link #validEnd}) 
    93          * @param maxIter 
    94          *            maximal number of random walks before aborting the test case 
    95          *            generation (see {@link #maxIter}) 
    96          */ 
    97         public RandomWalkGenerator(int numSequences, int minLength, int maxLength, 
    98                         boolean validEnd, long maxIter) { 
    99                 // check validity of the parameters 
    100                 if (numSequences < 1) { 
    101                         throw new InvalidParameterException( 
    102                                         "number of sequences must be at least 1 but is " 
    103                                                         + numSequences); 
    104                 } 
    105                 if (maxLength < 1) { 
    106                         throw new InvalidParameterException( 
    107                                         "maximal allowed length of test cases must be at least 1 but is " 
    108                                                         + maxLength); 
    109                 } 
    110                 if (minLength > maxLength) { 
    111                         throw new InvalidParameterException( 
    112                                         "minimal allowed length of test cases must be less than or equal to the maximal allowed length (min length: " 
    113                                                         + minLength + " ; max length: " + maxLength + ")"); 
    114                 } 
    115                 if (maxIter < numSequences) { 
    116                         throw new InvalidParameterException( 
    117                                         "maximal number of iterations must greater than or equal to the number of sequences (number of sequences: " 
    118                                                         + numSequences 
    119                                                         + " ; max iterations: " 
    120                                                         + maxIter 
    121                                                         + ")"); 
    122                 } 
    123                 this.numSequences = numSequences; 
    124                 this.minLength = minLength; 
    125                 this.maxLength = maxLength; 
    126                 this.validEnd = validEnd; 
    127                 this.maxIter = maxIter; 
    128         } 
     69    /** 
     70     * <p> 
     71     * Constructor. Creates a new RandomWalkGenerator and ensures the validity of the parameters: 
     72     * <ul> 
     73     * <li>numSequences must at least be 1 
     74     * <li>maxLength must at least be 1 
     75     * <li>minLength must be less than or equal to maxLength 
     76     * <li>maxIter must be greater than or equal to numSequences 
     77     * </ul> 
     78     * If one of these conditions is violated an {@link InvalidParameterException} is thrown. 
     79     * </p> 
     80     *  
     81     * @param numSequences 
     82     *            number of sequences desired for the test suite 
     83     * @param minLength 
     84     *            minimal length of a test sequence 
     85     * @param maxLength 
     86     *            maximal length of a test sequence 
     87     * @param validEnd 
     88     *            defines if test cases have to end with the global end event {@link Event#ENDEVENT} 
     89     *            (see {@link #validEnd}) 
     90     * @param maxIter 
     91     *            maximal number of random walks before aborting the test case generation (see 
     92     *            {@link #maxIter}) 
     93     */ 
     94    public RandomWalkGenerator(int numSequences, 
     95                               int minLength, 
     96                               int maxLength, 
     97                               boolean validEnd, 
     98                               long maxIter) 
     99    { 
     100        // check validity of the parameters 
     101        if (numSequences < 1) { 
     102            throw new InvalidParameterException("number of sequences must be at least 1 but is " + 
     103                numSequences); 
     104        } 
     105        if (maxLength < 1) { 
     106            throw new InvalidParameterException( 
     107                                                "maximal allowed length of test cases must be at least 1 but is " + 
     108                                                    maxLength); 
     109        } 
     110        if (minLength > maxLength) { 
     111            throw new InvalidParameterException( 
     112                                                "minimal allowed length of test cases must be less than or equal to the maximal allowed length (min length: " + 
     113                                                    minLength + " ; max length: " + maxLength + ")"); 
     114        } 
     115        if (maxIter < numSequences) { 
     116            throw new InvalidParameterException( 
     117                                                "maximal number of iterations must greater than or equal to the number of sequences (number of sequences: " + 
     118                                                    numSequences + 
     119                                                    " ; max iterations: " + 
     120                                                    maxIter + 
     121                                                    ")"); 
     122        } 
     123        this.numSequences = numSequences; 
     124        this.minLength = minLength; 
     125        this.maxLength = maxLength; 
     126        this.validEnd = validEnd; 
     127        this.maxIter = maxIter; 
     128    } 
    129129 
    130         /** 
    131          * <p> 
    132          * Generates a test suite by repeatedly randomly walking a stochastic 
    133          * process. 
    134          * </p> 
    135          *  
    136          * @param model 
    137          *            stochastic process which performs the random walks 
    138          * @return the test suite 
    139          */ 
    140         public Collection<List<Event>> generateTestSuite( 
    141                         IStochasticProcess model) { 
    142                 if (model == null) { 
    143                         throw new InvalidParameterException("model must not be null!"); 
    144                 } 
     130    /** 
     131     * <p> 
     132     * Generates a test suite by repeatedly randomly walking a stochastic process. 
     133     * </p> 
     134     *  
     135     * @param model 
     136     *            stochastic process which performs the random walks 
     137     * @return the test suite 
     138     */ 
     139    public Collection<List<Event>> generateTestSuite(IStochasticProcess model) { 
     140        if (model == null) { 
     141            throw new InvalidParameterException("model must not be null!"); 
     142        } 
    145143 
    146                 Set<List<Event>> sequences = new HashSet<List<Event>>( 
    147                                 numSequences); 
    148                 actualIter = 0; 
    149                 while (sequences.size() < numSequences && actualIter < maxIter) { 
    150                         List<Event> generatedSequence = model.randomSequence( 
    151                                         maxLength, validEnd); 
    152                         if (generatedSequence.size() >= minLength 
    153                                         && generatedSequence.size() <= maxLength) { 
    154                                 ((List<Event>) generatedSequence).add(0, Event.STARTEVENT); 
    155                                 if (validEnd) { 
    156                                         ((List<Event>) generatedSequence).add(Event.ENDEVENT); 
    157                                 } 
    158                                 sequences.add(generatedSequence); 
    159                         } 
    160                         actualIter++; 
    161                 } 
     144        Set<List<Event>> sequences = new HashSet<List<Event>>(numSequences); 
     145        actualIter = 0; 
     146        while (sequences.size() < numSequences && actualIter < maxIter) { 
     147            List<Event> generatedSequence = model.randomSequence(maxLength, validEnd); 
     148            if (generatedSequence.size() >= minLength && generatedSequence.size() <= maxLength) { 
     149                ((List<Event>) generatedSequence).add(0, Event.STARTEVENT); 
     150                if (validEnd) { 
     151                    ((List<Event>) generatedSequence).add(Event.ENDEVENT); 
     152                } 
     153                sequences.add(generatedSequence); 
     154            } 
     155            actualIter++; 
     156        } 
    162157 
    163                 return sequences; 
    164         } 
     158        return sequences; 
     159    } 
    165160 
    166         /** 
    167          * <p> 
    168          * Returns the actual number of random walks performed during the last call 
    169          * of {@link #generateTestSuite(IStochasticProcess)} or -1 if 
    170          * {@link #generateTestSuite(IStochasticProcess)} has not been called yet. 
    171          * </p> 
    172          *  
    173          * @return actual number of random walks or -1 if 
    174          *         {@link #generateTestSuite(IStochasticProcess)} has not been 
    175          *         called 
    176          */ 
    177         public long getActualIter() { 
    178                 return actualIter; 
    179         } 
     161    /** 
     162     * <p> 
     163     * Returns the actual number of random walks performed during the last call of 
     164     * {@link #generateTestSuite(IStochasticProcess)} or -1 if 
     165     * {@link #generateTestSuite(IStochasticProcess)} has not been called yet. 
     166     * </p> 
     167     *  
     168     * @return actual number of random walks or -1 if {@link #generateTestSuite(IStochasticProcess)} 
     169     *         has not been called 
     170     */ 
     171    public long getActualIter() { 
     172        return actualIter; 
     173    } 
    180174} 
Note: See TracChangeset for help on using the changeset viewer.