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

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