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

Last change on this file since 23 was 23, checked in by sherbold, 13 years ago

+ added functionality to get a tree represenation of the Trie

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