source: trunk/EventBenchCore/src/de/ugoe/cs/eventbench/models/Trie.java @ 66

Last change on this file since 66 was 66, checked in by sherbold, 13 years ago
  • added getNumStates() to interface de.ugoe.cs.eventbench.models.IStochasticProcess and implementing classes
File size: 4.3 KB
Line 
1package de.ugoe.cs.eventbench.models;
2
3import java.util.LinkedHashSet;
4import java.util.LinkedList;
5import java.util.List;
6import java.util.Set;
7
8import de.ugoe.cs.util.StringTools;
9
10import edu.uci.ics.jung.graph.DelegateTree;
11import edu.uci.ics.jung.graph.Tree;
12
13public class Trie<T> implements IDotCompatible {
14       
15        private Set<T> knownSymbols;
16       
17        private final TrieNode<T> rootNode;
18       
19        public Trie() {
20                rootNode = new TrieNode<T>();
21                knownSymbols = new LinkedHashSet<T>();
22        }
23       
24        public Set<T> getKnownSymbols() {
25                return new LinkedHashSet<T>(knownSymbols);
26        }
27       
28        // trains the current Trie using the given sequence and adds all subsequence of length trieOrder
29        public void train(List<T> sequence, int maxOrder) {
30                IncompleteMemory<T> latestActions = new IncompleteMemory<T>(maxOrder);
31                int i=0;
32                for(T currentEvent : sequence) {
33                        latestActions.add(currentEvent);
34                        knownSymbols.add(currentEvent);
35                        i++;
36                        if( i>=maxOrder ) {
37                                add(latestActions.getLast(maxOrder));
38                        }
39                }
40                int sequenceLength = sequence.size();
41                for( int j=maxOrder-1 ; j>0 ; j-- ) {
42                        add(sequence.subList(sequenceLength-j, sequenceLength));
43                }
44        }
45       
46
47        // increases the counters for each symbol in the subsequence
48        public void add(List<T> subsequence) {
49                if( subsequence!=null && !subsequence.isEmpty() ) {
50                        knownSymbols.addAll(subsequence);
51                        subsequence = new LinkedList<T>(subsequence);  // defensive copy!
52                        T firstSymbol = subsequence.get(0);
53                        TrieNode<T> node = getChildCreate(firstSymbol);
54                        node.add(subsequence);
55                }
56        }
57
58        protected TrieNode<T>  getChildCreate(T symbol) {
59                return rootNode.getChildCreate(symbol);
60        }
61       
62        protected TrieNode<T> getChild(T symbol) {
63                return rootNode.getChild(symbol);
64        }
65
66        // get the count of "sequence"
67        public int getCount(List<T> sequence) {
68                int count = 0;
69                TrieNode<T> node = find(sequence);
70                if( node!=null ) {
71                        count = node.getCount();
72                }
73                return count;
74        }
75       
76        // get the count of "sequence,follower"
77        public int getCount(List<T> sequence, T follower) {
78                List<T> tmpSequence = new LinkedList<T>(sequence);
79                tmpSequence.add(follower);
80                return getCount(tmpSequence);
81               
82        }
83       
84        public TrieNode<T> find(List<T> sequence) {
85                if( sequence==null || sequence.isEmpty() ) {
86                        return rootNode;
87                }
88                List<T> sequenceCopy = new LinkedList<T>(sequence);
89                TrieNode<T> result = null;
90                TrieNode<T> node = getChild(sequenceCopy.get(0));
91                if( node!=null ) {
92                        sequenceCopy.remove(0);
93                        result = node.find(sequenceCopy);
94                }
95                return result;
96        }
97       
98        // returns all symbols that follow the defined sequence
99        public List<T> getFollowingSymbols(List<T> sequence) {
100                List<T> result = new LinkedList<T>();
101                TrieNode<T> node = find(sequence);
102                if( node!=null ) {
103                        result = node.getFollowingSymbols();
104                }
105                return result;
106        }
107       
108        // longest suffix of context, that is contained in the tree and whose children are leaves
109        // possibly already deprecated
110        public List<T> getContextSuffix(List<T> context) {
111                List<T> contextSuffix = new LinkedList<T>(context); // defensive copy
112                boolean suffixFound = false;
113               
114                while(!suffixFound) {
115                        if( contextSuffix.isEmpty() ) {
116                                suffixFound = true; // suffix is the empty word
117                        } else {
118                                TrieNode<T> node = find(contextSuffix);
119                                if( node!=null ) {
120                                        if( !node.getFollowingSymbols().isEmpty() ) {
121                                                suffixFound = true;
122                                        }
123                                }
124                                if( !suffixFound ) {
125                                        contextSuffix.remove(0);
126                                }
127                        }
128                }
129               
130                return contextSuffix;
131        }
132       
133       
134        static public class Edge{}
135       
136        static public class TrieVertex {
137                private String id;
138                protected TrieVertex(String id) {
139                        this.id = id;
140                }
141                public String toString() {
142                        return id;
143                }
144        }
145       
146        protected Tree<TrieVertex, Edge> getGraph() {
147                DelegateTree<TrieVertex, Edge> graph = new DelegateTree<TrieVertex, Edge>();
148                rootNode.getGraph(null, graph);
149                return graph;
150        }
151       
152        public String getDotRepresentation() {
153                StringBuilder stringBuilder = new StringBuilder();
154                stringBuilder.append("digraph model {" + StringTools.ENDLINE);
155                rootNode.appendDotRepresentation(stringBuilder);
156                stringBuilder.append('}' + StringTools.ENDLINE);
157                return stringBuilder.toString();
158        }
159       
160        @Override
161        public String toString() {
162                return rootNode.toString();
163        }
164       
165        public int getNumSymbols() {
166                return knownSymbols.size();
167        }
168}
Note: See TracBrowser for help on using the repository browser.