source: trunk/EventBenchCore/src/de/ugoe/cs/eventbench/models/TrieNode.java @ 86

Last change on this file since 86 was 86, checked in by sherbold, 13 years ago
  • made stochastic models and events serializable
File size: 3.6 KB
Line 
1package de.ugoe.cs.eventbench.models;
2
3import java.io.Serializable;
4import java.security.InvalidParameterException;
5import java.util.LinkedList;
6import java.util.List;
7
8import de.ugoe.cs.eventbench.models.Trie.Edge;
9import de.ugoe.cs.eventbench.models.Trie.TrieVertex;
10import de.ugoe.cs.util.StringTools;
11import edu.uci.ics.jung.graph.DelegateTree;
12
13
14class TrieNode<T> implements Serializable {
15       
16        /**
17         * Id for object serialization.
18         */
19        private static final long serialVersionUID = 1L;
20       
21        private int count;
22        private final T symbol;
23       
24        private List<TrieNode<T>> children;
25       
26        TrieNode() {
27                this.symbol = null;
28                count = 0;
29                children = new LinkedList<TrieNode<T>>();
30        }
31       
32        public TrieNode(T symbol) {
33                if( symbol==null ) {
34                        throw new InvalidParameterException("symbol must not be null. null is reserved for root node!");
35                }
36                this.symbol = symbol;
37                count = 0;
38                children = new LinkedList<TrieNode<T>>();
39        }
40
41        public void add(List<T> subsequence) {
42                if( !subsequence.isEmpty() ) {
43                        if( !symbol.equals(subsequence.get(0)) ) { // should be guaranteed by the recursion/TrieRoot!
44                                throw new AssertionError("Invalid trie operation!");
45                        }
46                        count++;
47                        subsequence.remove(0);
48                        if( !subsequence.isEmpty() ) {
49                                T nextSymbol = subsequence.get(0);
50                                getChildCreate(nextSymbol).add(subsequence);
51                        }
52                }
53        }
54       
55        public T getSymbol() {
56                return symbol;
57        }
58       
59        public int getCount() {
60                return count;
61        }
62       
63        protected TrieNode<T>  getChildCreate(T symbol) {
64                TrieNode<T> node = getChild(symbol);
65                if( node==null ) {
66                        node = new TrieNode<T>(symbol);
67                        children.add(node);
68                }
69                return node;
70        }
71       
72        protected TrieNode<T> getChild(T symbol) {
73                for( TrieNode<T> child : children ) {
74                        if( child.getSymbol().equals(symbol) ) {
75                                return child;
76                        }
77                }
78                return null;
79        }
80       
81
82       
83        public TrieNode<T> find(List<T> subsequence) {
84                TrieNode<T> result = null;
85                if( subsequence.isEmpty() ) {
86                        result = this;
87                } else {
88                        TrieNode<T> node = getChild(subsequence.get(0));
89                        if( node!=null ) {
90                                subsequence.remove(0);
91                                result = node.find(subsequence);
92                        }
93                }
94                return result;
95        }
96       
97        // returns all symbols that follow this node
98        public List<T> getFollowingSymbols() {
99                List<T> followingSymbols = new LinkedList<T>();
100                for( TrieNode<T> child : children ) {
101                        followingSymbols.add(child.getSymbol());
102                }
103                return followingSymbols;
104        }
105       
106        @Override
107        public String toString() {
108                String str = symbol.toString()+" #"+count;
109                if( !children.isEmpty() ) {
110                        str += StringTools.ENDLINE + children.toString();
111                }
112                return str;
113        }
114
115        public void getGraph(TrieVertex parent, DelegateTree<TrieVertex, Edge> graph) {
116                TrieVertex currentVertex;
117                if( symbol==null ){
118                        currentVertex = new TrieVertex("root");
119                        graph.addVertex(currentVertex);
120                } else {
121                        currentVertex = new TrieVertex(getSymbol().toString()+"#"+getCount());
122                        graph.addChild( new Edge() , parent, currentVertex );
123                }
124                for( TrieNode<T> node : children ) {
125                        node.getGraph(currentVertex, graph);
126                }               
127        }
128       
129        void appendDotRepresentation(StringBuilder stringBuilder) {
130                String thisSaneId;
131                if( symbol==null ) {
132                        thisSaneId = "root";
133                } else {
134                        thisSaneId = symbol.toString().replace("\"", "\\\"").replaceAll("[\r\n]","")+"#"+count;
135                }
136                stringBuilder.append(" " + hashCode() + " [label=\""+thisSaneId+"\"];" + StringTools.ENDLINE);
137                for( TrieNode<T> childNode : children ) {
138                        stringBuilder.append(" "+hashCode()+" -> " + childNode.hashCode() + ";" + StringTools.ENDLINE);
139                }
140                for( TrieNode<T> childNode : children ) {
141                        childNode.appendDotRepresentation(stringBuilder);
142                }
143        }
144}
Note: See TracBrowser for help on using the repository browser.