[559] | 1 |
|
---|
[518] | 2 | package de.ugoe.cs.quest.usageprofiles;
|
---|
| 3 |
|
---|
| 4 | import java.security.InvalidParameterException;
|
---|
| 5 | import java.util.Collection;
|
---|
| 6 | import java.util.LinkedList;
|
---|
| 7 | import java.util.List;
|
---|
| 8 | import java.util.Random;
|
---|
| 9 |
|
---|
| 10 | import de.ugoe.cs.quest.eventcore.Event;
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | * <p>
|
---|
[559] | 14 | * Implements a Deterministic Finite Automata (DFA) capable of random session generation. It is a
|
---|
| 15 | * special case of a first-order Markov model, where the transition probability is equally high for
|
---|
| 16 | * all following states.
|
---|
[518] | 17 | * </p>
|
---|
| 18 | *
|
---|
| 19 | * @author Steffen Herbold
|
---|
| 20 | * @version 1.0
|
---|
| 21 | */
|
---|
| 22 | public class DeterministicFiniteAutomaton extends FirstOrderMarkovModel {
|
---|
| 23 |
|
---|
[559] | 24 | /**
|
---|
| 25 | * <p>
|
---|
| 26 | * Id for object serialization.
|
---|
| 27 | * </p>
|
---|
| 28 | */
|
---|
| 29 | private static final long serialVersionUID = 1L;
|
---|
[518] | 30 |
|
---|
[559] | 31 | /**
|
---|
| 32 | * <p>
|
---|
| 33 | * Constructor. Creates a new DeterministicFiniteAutomaton.
|
---|
| 34 | * </p>
|
---|
| 35 | *
|
---|
| 36 | * @param r
|
---|
| 37 | * random number generator used by probabilistic methods of the class
|
---|
| 38 | */
|
---|
| 39 | public DeterministicFiniteAutomaton(Random r) {
|
---|
| 40 | super(r);
|
---|
| 41 | }
|
---|
[518] | 42 |
|
---|
[559] | 43 | /**
|
---|
| 44 | * <p>
|
---|
| 45 | * Calculates the proability of the next state. Each of the following states in the automaton is
|
---|
| 46 | * equally probable.
|
---|
| 47 | * </p>
|
---|
| 48 | *
|
---|
| 49 | * @see de.ugoe.cs.quest.usageprofiles.IStochasticProcess#getProbability(java.util.List,
|
---|
| 50 | * de.ugoe.cs.quest.eventcore.Event)
|
---|
| 51 | */
|
---|
| 52 | @Override
|
---|
| 53 | public double getProbability(List<Event> context, Event symbol) {
|
---|
| 54 | if (context == null) {
|
---|
| 55 | throw new InvalidParameterException("context must not be null");
|
---|
| 56 | }
|
---|
| 57 | if (symbol == null) {
|
---|
| 58 | throw new InvalidParameterException("symbol must not be null");
|
---|
| 59 | }
|
---|
| 60 | double result = 0.0d;
|
---|
[518] | 61 |
|
---|
[559] | 62 | List<Event> contextCopy;
|
---|
| 63 | if (context.size() >= trieOrder) {
|
---|
| 64 | contextCopy =
|
---|
| 65 | new LinkedList<Event>(context.subList(context.size() - trieOrder + 1,
|
---|
| 66 | context.size()));
|
---|
| 67 | }
|
---|
| 68 | else {
|
---|
| 69 | contextCopy = new LinkedList<Event>(context);
|
---|
| 70 | }
|
---|
[518] | 71 |
|
---|
[559] | 72 | Collection<Event> followers = trie.getFollowingSymbols(contextCopy);
|
---|
[518] | 73 |
|
---|
[559] | 74 | if (followers.size() != 0 && followers.contains(symbol)) {
|
---|
| 75 | result = 1.0d / followers.size();
|
---|
| 76 | }
|
---|
[518] | 77 |
|
---|
[559] | 78 | return result;
|
---|
| 79 | }
|
---|
[518] | 80 |
|
---|
| 81 | }
|
---|