[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;
|
---|
[80] | 6 | import java.util.Set;
|
---|
[12] | 7 |
|
---|
| 8 | import de.ugoe.cs.eventbench.data.Event;
|
---|
[23] | 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] | 12 |
|
---|
[17] | 13 | public abstract class TrieBasedModel implements IStochasticProcess {
|
---|
[12] | 14 |
|
---|
[86] | 15 | /**
|
---|
| 16 | * Id for object serialization.
|
---|
| 17 | */
|
---|
| 18 | private static final long serialVersionUID = 1L;
|
---|
| 19 |
|
---|
[16] | 20 | protected int trieOrder;
|
---|
[12] | 21 |
|
---|
| 22 | protected Trie<Event<?>> trie;
|
---|
| 23 | protected final Random r;
|
---|
| 24 |
|
---|
| 25 |
|
---|
[16] | 26 | public TrieBasedModel(int markovOrder, Random r) {
|
---|
[12] | 27 | super();
|
---|
[16] | 28 | this.trieOrder = markovOrder+1;
|
---|
[12] | 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 |
|
---|
[16] | 40 | trie.train(currentSequence, trieOrder);
|
---|
[12] | 41 | }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[17] | 44 | /* (non-Javadoc)
|
---|
| 45 | * @see de.ugoe.cs.eventbench.models.IStochasticProcess#randomSequence()
|
---|
| 46 | */
|
---|
| 47 | @Override
|
---|
[12] | 48 | public List<? extends Event<?>> randomSequence() {
|
---|
| 49 | List<Event<?>> sequence = new LinkedList<Event<?>>();
|
---|
| 50 |
|
---|
[16] | 51 | IncompleteMemory<Event<?>> context = new IncompleteMemory<Event<?>>(trieOrder-1);
|
---|
[12] | 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;
|
---|
[16] | 61 | List<Event<?>> currentContext = context.getLast(trieOrder);
|
---|
[12] | 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 | }
|
---|
[23] | 78 |
|
---|
[30] | 79 | public String getTrieDotRepresentation() {
|
---|
| 80 | return trie.getDotRepresentation();
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[23] | 83 | public Tree<TrieVertex, Edge> getTrieGraph() {
|
---|
| 84 | return trie.getGraph();
|
---|
| 85 | }
|
---|
[12] | 86 |
|
---|
| 87 | @Override
|
---|
| 88 | public String toString() {
|
---|
| 89 | return trie.toString();
|
---|
| 90 | }
|
---|
[66] | 91 |
|
---|
| 92 | public int getNumStates() {
|
---|
| 93 | return trie.getNumSymbols();
|
---|
| 94 | }
|
---|
[70] | 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 | }
|
---|
[80] | 105 |
|
---|
| 106 | public Set<Event<?>> getEvents() {
|
---|
| 107 | return trie.getKnownSymbols();
|
---|
| 108 | }
|
---|
[12] | 109 |
|
---|
| 110 | } |
---|