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
RevLine 
[13]1package de.ugoe.cs.eventbench.models;
[1]2
[9]3import java.util.LinkedHashSet;
[1]4import java.util.LinkedList;
5import java.util.List;
[9]6import java.util.Set;
[1]7
[30]8import de.ugoe.cs.util.StringTools;
9
[5]10import edu.uci.ics.jung.graph.DelegateTree;
11import edu.uci.ics.jung.graph.Tree;
12
[30]13public class Trie<T> implements IDotCompatible {
[1]14       
[9]15        private Set<T> knownSymbols;
16       
[6]17        private final TrieNode<T> rootNode;
[1]18       
[6]19        public Trie() {
20                rootNode = new TrieNode<T>();
[9]21                knownSymbols = new LinkedHashSet<T>();
[6]22        }
23       
[9]24        public Set<T> getKnownSymbols() {
25                return new LinkedHashSet<T>(knownSymbols);
26        }
27       
[16]28        // trains the current Trie using the given sequence and adds all subsequence of length trieOrder
[9]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       
[1]46
[9]47        // increases the counters for each symbol in the subsequence
[1]48        public void add(List<T> subsequence) {
[9]49                if( subsequence!=null && !subsequence.isEmpty() ) {
50                        knownSymbols.addAll(subsequence);
[5]51                        subsequence = new LinkedList<T>(subsequence);  // defensive copy!
[1]52                        T firstSymbol = subsequence.get(0);
[6]53                        TrieNode<T> node = getChildCreate(firstSymbol);
54                        node.add(subsequence);
[1]55                }
56        }
57
58        protected TrieNode<T>  getChildCreate(T symbol) {
[6]59                return rootNode.getChildCreate(symbol);
[1]60        }
61       
62        protected TrieNode<T> getChild(T symbol) {
[6]63                return rootNode.getChild(symbol);
[1]64        }
65
[5]66        // get the count of "sequence"
[1]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       
[5]76        // get the count of "sequence,follower"
[1]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) {
[6]85                if( sequence==null || sequence.isEmpty() ) {
86                        return rootNode;
87                }
[5]88                List<T> sequenceCopy = new LinkedList<T>(sequence);
[1]89                TrieNode<T> result = null;
[6]90                TrieNode<T> node = getChild(sequenceCopy.get(0));
91                if( node!=null ) {
92                        sequenceCopy.remove(0);
93                        result = node.find(sequenceCopy);
[1]94                }
95                return result;
96        }
97       
[5]98        // returns all symbols that follow the defined sequence
[1]99        public List<T> getFollowingSymbols(List<T> sequence) {
[5]100                List<T> result = new LinkedList<T>();
[6]101                TrieNode<T> node = find(sequence);
102                if( node!=null ) {
103                        result = node.getFollowingSymbols();
[1]104                }
105                return result;
106        }
107       
[5]108        // longest suffix of context, that is contained in the tree and whose children are leaves
[6]109        // possibly already deprecated
[1]110        public List<T> getContextSuffix(List<T> context) {
[5]111                List<T> contextSuffix = new LinkedList<T>(context); // defensive copy
[1]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                                }
[5]124                                if( !suffixFound ) {
125                                        contextSuffix.remove(0);
126                                }
[1]127                        }
128                }
129               
130                return contextSuffix;
131        }
132       
[5]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       
[23]146        protected Tree<TrieVertex, Edge> getGraph() {
[5]147                DelegateTree<TrieVertex, Edge> graph = new DelegateTree<TrieVertex, Edge>();
[6]148                rootNode.getGraph(null, graph);
[5]149                return graph;
150        }
151       
[30]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       
[1]160        @Override
161        public String toString() {
[6]162                return rootNode.toString();
[1]163        }
[66]164       
165        public int getNumSymbols() {
166                return knownSymbols.size();
167        }
[1]168}
Note: See TracBrowser for help on using the repository browser.