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