1 | package de.ugoe.cs.eventbench.models;
|
---|
2 |
|
---|
3 | import java.util.LinkedList;
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.Random;
|
---|
6 | import java.util.Set;
|
---|
7 |
|
---|
8 | import de.ugoe.cs.eventbench.data.Event;
|
---|
9 | import de.ugoe.cs.eventbench.models.Trie.Edge;
|
---|
10 | import de.ugoe.cs.eventbench.models.Trie.TrieVertex;
|
---|
11 | import edu.uci.ics.jung.graph.Tree;
|
---|
12 |
|
---|
13 | public abstract class TrieBasedModel implements IStochasticProcess {
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Id for object serialization.
|
---|
17 | */
|
---|
18 | private static final long serialVersionUID = 1L;
|
---|
19 |
|
---|
20 | protected int trieOrder;
|
---|
21 |
|
---|
22 | protected Trie<Event<?>> trie;
|
---|
23 | protected final Random r;
|
---|
24 |
|
---|
25 |
|
---|
26 | public TrieBasedModel(int markovOrder, Random r) {
|
---|
27 | super();
|
---|
28 | this.trieOrder = markovOrder+1;
|
---|
29 | this.r = r;
|
---|
30 | }
|
---|
31 |
|
---|
32 | public void train(List<List<Event<?>>> sequences) {
|
---|
33 | trie = new Trie<Event<?>>();
|
---|
34 |
|
---|
35 | for(List<Event<?>> sequence : sequences) {
|
---|
36 | List<Event<?>> currentSequence = new LinkedList<Event<?>>(sequence); // defensive copy
|
---|
37 | currentSequence.add(0, Event.STARTEVENT);
|
---|
38 | currentSequence.add(Event.ENDEVENT);
|
---|
39 |
|
---|
40 | trie.train(currentSequence, trieOrder);
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | /* (non-Javadoc)
|
---|
45 | * @see de.ugoe.cs.eventbench.models.IStochasticProcess#randomSequence()
|
---|
46 | */
|
---|
47 | @Override
|
---|
48 | public List<? extends Event<?>> randomSequence() {
|
---|
49 | List<Event<?>> sequence = new LinkedList<Event<?>>();
|
---|
50 |
|
---|
51 | IncompleteMemory<Event<?>> context = new IncompleteMemory<Event<?>>(trieOrder-1);
|
---|
52 | context.add(Event.STARTEVENT);
|
---|
53 |
|
---|
54 | Event<?> currentState = Event.STARTEVENT;
|
---|
55 |
|
---|
56 | boolean endFound = false;
|
---|
57 |
|
---|
58 | while(!endFound) {
|
---|
59 | double randVal = r.nextDouble();
|
---|
60 | double probSum = 0.0;
|
---|
61 | List<Event<?>> currentContext = context.getLast(trieOrder);
|
---|
62 | for( Event<?> symbol : trie.getKnownSymbols() ) {
|
---|
63 | probSum += getProbability(currentContext, symbol);
|
---|
64 | if( probSum>=randVal ) {
|
---|
65 | endFound = (symbol==Event.ENDEVENT);
|
---|
66 | if( !(symbol==Event.STARTEVENT || symbol==Event.ENDEVENT) ) {
|
---|
67 | // only add the symbol the sequence if it is not START or END
|
---|
68 | context.add(symbol);
|
---|
69 | currentState = symbol;
|
---|
70 | sequence.add(currentState);
|
---|
71 | }
|
---|
72 | break;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 | return sequence;
|
---|
77 | }
|
---|
78 |
|
---|
79 | public String getTrieDotRepresentation() {
|
---|
80 | return trie.getDotRepresentation();
|
---|
81 | }
|
---|
82 |
|
---|
83 | public Tree<TrieVertex, Edge> getTrieGraph() {
|
---|
84 | return trie.getGraph();
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public String toString() {
|
---|
89 | return trie.toString();
|
---|
90 | }
|
---|
91 |
|
---|
92 | public int getNumStates() {
|
---|
93 | return trie.getNumSymbols();
|
---|
94 | }
|
---|
95 |
|
---|
96 | public String[] getStateStrings() {
|
---|
97 | String[] stateStrings = new String[getNumStates()];
|
---|
98 | int i=0;
|
---|
99 | for( Event<?> symbol : trie.getKnownSymbols() ) {
|
---|
100 | stateStrings[i] = symbol.toString();
|
---|
101 | i++;
|
---|
102 | }
|
---|
103 | return stateStrings;
|
---|
104 | }
|
---|
105 |
|
---|
106 | public Set<Event<?>> getEvents() {
|
---|
107 | return trie.getKnownSymbols();
|
---|
108 | }
|
---|
109 |
|
---|
110 | } |
---|