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