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