package de.ugoe.cs.eventbench.coverage; import java.security.InvalidParameterException; import java.util.Collection; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import de.ugoe.cs.eventbench.data.Event; import de.ugoe.cs.eventbench.models.IStochasticProcess; /** *

* This class gathers some helper functions to work with sequences. *

* * @author Steffen Herbold * @version 1.0 */ public class SequenceTools { /** *

* Private constructor to prevent initializing of the class. *

*/ private SequenceTools() { } /** *

* Calculates the weights for all sequences passed to this function as * defined by the stochastic process and stores them in a {@link Map}. The * weights are normalized, i.e., the sum of all weights contained in this * map is 1. *

* * @param process * process used for weight calculation * @param sequences * sequences for which the weights are calculated * @return {@link Map} of weights */ public static Map>, Double> generateWeights( IStochasticProcess process, Collection>> sequences) { Map>, Double> subSeqWeights = new LinkedHashMap>, Double>(); if (sequences != null && !sequences.isEmpty()) { if (process != null) { double sum = 0.0; for (List> sequence : sequences) { double prob = process.getProbability(sequence); subSeqWeights.put(sequence, prob); sum += prob; } if (sum < 1.0) { for (Map.Entry>, Double> entry : subSeqWeights .entrySet()) { entry.setValue(entry.getValue() / sum); } } } else { for( List> sequence : sequences ) { subSeqWeights.put(sequence, 0.0d); } } } return subSeqWeights; } /** *

* Calculates the number of all existing sequences of a given length, * regardless whether they are possible or not. *

* * @param process * stochastic process whose symbols are the basis for this * calculation * @param length * lenght of the sequences * @return numStates^length * @throws InvalidParameterException * thrown if length less or equal to 0 */ public static long numSequences(IStochasticProcess process, int length) { if (length <= 0) { throw new InvalidParameterException( "length must be a positive integer"); } long result = 0; if (process != null) { result = (long) Math.pow(process.getNumSymbols(), length); } return result; } /** *

* Creates a {@link Set} of all subsequences of a given length that are * contained in a sequence collection. *

* * @param sequences * sequences from which the subsequences are extracted * @param length * length of the subsequences * @return {@link Set} of all subsequences * @throws InvalidParameterException * thrown if length less or equal to 0 */ public static Set>> containedSubSequences( Collection>> sequences, int length) { if (length <= 0) { throw new InvalidParameterException( "length must be a positive integer"); } Set>> containedSubSeqs = new LinkedHashSet>>(); if (sequences != null) { for (List> sequence : sequences) { List> subSeq = new LinkedList>(); boolean minLengthReached = false; for (Event event : sequence) { subSeq.add(event); if (!minLengthReached) { if (subSeq.size() == length) { minLengthReached = true; } } else { subSeq.remove(0); } if (minLengthReached) { if (!containedSubSeqs.contains(subSeq)) { containedSubSeqs.add(new LinkedList>( subSeq)); } } } } } return containedSubSeqs; } }