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

Last change on this file since 19 was 19, checked in by sherbold, 13 years ago
File size: 5.4 KB
Line 
1package de.ugoe.cs.eventbench.models;
2
3import java.awt.Dimension;
4import java.awt.Rectangle;
5import java.awt.Shape;
6import java.util.LinkedHashSet;
7import java.util.LinkedList;
8import java.util.List;
9import java.util.Set;
10
11import javax.swing.JFrame;
12
13import org.apache.commons.collections15.Transformer;
14
15
16import edu.uci.ics.jung.algorithms.layout.Layout;
17import edu.uci.ics.jung.algorithms.layout.TreeLayout;
18import edu.uci.ics.jung.graph.DelegateTree;
19import edu.uci.ics.jung.graph.Tree;
20import edu.uci.ics.jung.visualization.BasicVisualizationServer;
21import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
22import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;
23
24public class Trie<T> {
25       
26        private Set<T> knownSymbols;
27       
28        private final TrieNode<T> rootNode;
29       
30        public Trie() {
31                rootNode = new TrieNode<T>();
32                knownSymbols = new LinkedHashSet<T>();
33        }
34       
35        public Set<T> getKnownSymbols() {
36                return new LinkedHashSet<T>(knownSymbols);
37        }
38       
39        // trains the current Trie using the given sequence and adds all subsequence of length trieOrder
40        public void train(List<T> sequence, int maxOrder) {
41                IncompleteMemory<T> latestActions = new IncompleteMemory<T>(maxOrder);
42                int i=0;
43                for(T currentEvent : sequence) {
44                        latestActions.add(currentEvent);
45                        knownSymbols.add(currentEvent);
46                        i++;
47                        if( i>=maxOrder ) {
48                                add(latestActions.getLast(maxOrder));
49                        }
50                }
51                int sequenceLength = sequence.size();
52                for( int j=maxOrder-1 ; j>0 ; j-- ) {
53                        add(sequence.subList(sequenceLength-j, sequenceLength));
54                }
55        }
56       
57
58        // increases the counters for each symbol in the subsequence
59        public void add(List<T> subsequence) {
60                if( subsequence!=null && !subsequence.isEmpty() ) {
61                        knownSymbols.addAll(subsequence);
62                        subsequence = new LinkedList<T>(subsequence);  // defensive copy!
63                        T firstSymbol = subsequence.get(0);
64                        TrieNode<T> node = getChildCreate(firstSymbol);
65                        node.add(subsequence);
66                }
67        }
68
69        protected TrieNode<T>  getChildCreate(T symbol) {
70                return rootNode.getChildCreate(symbol);
71        }
72       
73        protected TrieNode<T> getChild(T symbol) {
74                return rootNode.getChild(symbol);
75        }
76
77        // get the count of "sequence"
78        public int getCount(List<T> sequence) {
79                int count = 0;
80                TrieNode<T> node = find(sequence);
81                if( node!=null ) {
82                        count = node.getCount();
83                }
84                return count;
85        }
86       
87        // get the count of "sequence,follower"
88        public int getCount(List<T> sequence, T follower) {
89                List<T> tmpSequence = new LinkedList<T>(sequence);
90                tmpSequence.add(follower);
91                return getCount(tmpSequence);
92               
93        }
94       
95        public TrieNode<T> find(List<T> sequence) {
96                if( sequence==null || sequence.isEmpty() ) {
97                        return rootNode;
98                }
99                List<T> sequenceCopy = new LinkedList<T>(sequence);
100                TrieNode<T> result = null;
101                TrieNode<T> node = getChild(sequenceCopy.get(0));
102                if( node!=null ) {
103                        sequenceCopy.remove(0);
104                        result = node.find(sequenceCopy);
105                }
106                return result;
107        }
108       
109        // returns all symbols that follow the defined sequence
110        public List<T> getFollowingSymbols(List<T> sequence) {
111                List<T> result = new LinkedList<T>();
112                TrieNode<T> node = find(sequence);
113                if( node!=null ) {
114                        result = node.getFollowingSymbols();
115                }
116                return result;
117        }
118       
119        // longest suffix of context, that is contained in the tree and whose children are leaves
120        // possibly already deprecated
121        public List<T> getContextSuffix(List<T> context) {
122                List<T> contextSuffix = new LinkedList<T>(context); // defensive copy
123                boolean suffixFound = false;
124               
125                while(!suffixFound) {
126                        if( contextSuffix.isEmpty() ) {
127                                suffixFound = true; // suffix is the empty word
128                        } else {
129                                TrieNode<T> node = find(contextSuffix);
130                                if( node!=null ) {
131                                        if( !node.getFollowingSymbols().isEmpty() ) {
132                                                suffixFound = true;
133                                        }
134                                }
135                                if( !suffixFound ) {
136                                        contextSuffix.remove(0);
137                                }
138                        }
139                }
140               
141                return contextSuffix;
142        }
143       
144       
145        static public class Edge{}
146       
147        static public class TrieVertex {
148                private String id;
149                protected TrieVertex(String id) {
150                        this.id = id;
151                }
152                public String toString() {
153                        return id;
154                }
155        }
156       
157        private Tree<TrieVertex, Edge> getGraph() {
158                DelegateTree<TrieVertex, Edge> graph = new DelegateTree<TrieVertex, Edge>();
159                rootNode.getGraph(null, graph);
160                return graph;
161        }
162       
163        public void display() {
164                Tree<TrieVertex, Edge> graph = this.getGraph();
165                Layout<TrieVertex, Edge> layout = new TreeLayout<TrieVertex, Edge>(graph, 60);
166                // The BasicVisualizationServer<V,E> is parameterized by the edge types
167                BasicVisualizationServer<TrieVertex,Edge> vv =
168                new BasicVisualizationServer<TrieVertex,Edge>(layout);
169                vv.setPreferredSize(new Dimension(1100,850)); //Sets the viewing area size
170               
171               
172                final Rectangle rect = new Rectangle(40, 20);
173                       
174                Transformer<TrieVertex, Shape> vertexShapeTransformer =
175                        new Transformer<TrieVertex, Shape>() {
176                                public Shape transform(TrieVertex s) {
177                                        return rect;
178                                }
179                };
180                vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
181                vv.getRenderContext().setVertexShapeTransformer(vertexShapeTransformer);
182                vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<TrieVertex>());
183               
184                JFrame frame = new JFrame("Trie");
185                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
186                frame.getContentPane().add(vv);
187                frame.pack();
188                frame.setVisible(true);
189        }
190       
191        @Override
192        public String toString() {
193                return rootNode.toString();
194        }
195}
Note: See TracBrowser for help on using the repository browser.