Ignore:
Timestamp:
07/05/11 15:18:56 (13 years ago)
Author:
sherbold
Message:
  • code documentation
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/EventBenchCore/src/de/ugoe/cs/eventbench/models/TrieNode.java

    r102 r106  
    1111import de.ugoe.cs.util.StringTools; 
    1212import edu.uci.ics.jung.graph.DelegateTree; 
    13  
    14  
     13import edu.uci.ics.jung.graph.Tree; 
     14 
     15/** 
     16 * <p> 
     17 * This class implements a node of a trie. Each node is associated with a symbol 
     18 * and has a counter. The counter marks the number of occurences of the sequence 
     19 * defined by the path from the root of the trie to this node. 
     20 * </p> 
     21 *  
     22 * @author Steffen Herbold 
     23 *  
     24 * @param <T> 
     25 *            Type of the symbols that are stored in the trie. 
     26 * @see Trie 
     27 */ 
    1528class TrieNode<T> implements Serializable { 
    16          
    17         /** 
     29 
     30        /** 
     31         * <p> 
    1832         * Id for object serialization. 
     33         * </p> 
    1934         */ 
    2035        private static final long serialVersionUID = 1L; 
    21          
     36 
     37        /** 
     38         * <p> 
     39         * Counter for the number of occurences of the sequence. 
     40         * </p> 
     41         */ 
    2242        private int count; 
     43 
     44        /** 
     45         * <p> 
     46         * Symbol associated with the node. 
     47         * </p> 
     48         */ 
    2349        private final T symbol; 
    24          
     50 
     51        /** 
     52         * <p> 
     53         * Child nodes of this node. If the node is a leaf this collection is empty. 
     54         * </p> 
     55         */ 
    2556        private Collection<TrieNode<T>> children; 
    26          
     57 
     58        /** 
     59         * <p> 
     60         * Constructor. Creates a new TrieNode without a symbol associated.<br> 
     61         * <b>This constructor should only be used to create the root node of the 
     62         * trie!</b> 
     63         * </p> 
     64         */ 
    2765        TrieNode() { 
    2866                this.symbol = null; 
     
    3068                children = new LinkedList<TrieNode<T>>(); 
    3169        } 
    32          
     70 
     71        /** 
     72         * <p> 
     73         * Constructor. Creates a new TrieNode. The symbol must not be null. 
     74         * </p> 
     75         *  
     76         * @param symbol 
     77         *            symbol associated with the trie node 
     78         */ 
    3379        public TrieNode(T symbol) { 
    34                 if( symbol==null ) { 
    35                         throw new InvalidParameterException("symbol must not be null. null is reserved for root node!"); 
     80                if (symbol == null) { 
     81                        throw new InvalidParameterException( 
     82                                        "symbol must not be null. null is reserved for root node!"); 
    3683                } 
    3784                this.symbol = symbol; 
     
    4087        } 
    4188 
     89        /** 
     90         * <p> 
     91         * Adds a given subsequence to the trie and increases the counters 
     92         * accordingly. 
     93         * </p> 
     94         *  
     95         * @param subsequence 
     96         *            subsequence whose counters are increased 
     97         * @see Trie#add(List) 
     98         */ 
    4299        public void add(List<T> subsequence) { 
    43                 if( !subsequence.isEmpty() ) { 
    44                         if( !symbol.equals(subsequence.get(0)) ) { // should be guaranteed by the recursion/TrieRoot! 
     100                if (!subsequence.isEmpty()) { 
     101                        if (!symbol.equals(subsequence.get(0))) { // should be guaranteed by 
     102                                                                                                                // the 
     103                                                                                                                // recursion/TrieRoot! 
    45104                                throw new AssertionError("Invalid trie operation!"); 
    46105                        } 
    47106                        count++; 
    48107                        subsequence.remove(0); 
    49                         if( !subsequence.isEmpty() ) { 
     108                        if (!subsequence.isEmpty()) { 
    50109                                T nextSymbol = subsequence.get(0); 
    51110                                getChildCreate(nextSymbol).add(subsequence); 
     
    53112                } 
    54113        } 
    55          
     114 
     115        /** 
     116         * <p> 
     117         * Returns the symbol associated with the node. 
     118         * </p> 
     119         *  
     120         * @return symbol associated with the node 
     121         */ 
    56122        public T getSymbol() { 
    57123                return symbol; 
    58124        } 
    59          
     125 
     126        /** 
     127         * <p> 
     128         * Returns the number of occurences of the sequence represented by the node. 
     129         * </p> 
     130         *  
     131         * @return number of occurences of the sequence represented by the node 
     132         */ 
    60133        public int getCount() { 
    61134                return count; 
    62135        } 
    63          
    64         protected TrieNode<T>  getChildCreate(T symbol) { 
     136 
     137        /** 
     138         * <p> 
     139         * Returns the child of the node associated with the given symbol or creates 
     140         * it if it does not exist yet. 
     141         * </p> 
     142         *  
     143         * @param symbol 
     144         *            symbol whose node is required 
     145         * @return node associated with the symbol 
     146         * @see Trie#getChildCreate(Object) 
     147         */ 
     148        protected TrieNode<T> getChildCreate(T symbol) { 
    65149                TrieNode<T> node = getChild(symbol); 
    66                 if( node==null ) { 
     150                if (node == null) { 
    67151                        node = new TrieNode<T>(symbol); 
    68152                        children.add(node); 
     
    70154                return node; 
    71155        } 
    72          
     156 
     157        /** 
     158         * <p> 
     159         * Returns the child of the node associated with the given symbol or null if 
     160         * it does not exist. 
     161         * </p> 
     162         *  
     163         * @param symbol 
     164         *            symbol whose node is required 
     165         * @return node associated with the symbol; null if no such node exists 
     166         * @see Trie#getChild(Object) 
     167         */ 
    73168        protected TrieNode<T> getChild(T symbol) { 
    74                 for( TrieNode<T> child : children ) { 
    75                         if( child.getSymbol().equals(symbol) ) { 
     169                for (TrieNode<T> child : children) { 
     170                        if (child.getSymbol().equals(symbol)) { 
    76171                                return child; 
    77172                        } 
     
    79174                return null; 
    80175        } 
    81          
    82  
    83          
     176 
     177        /** 
     178         * <p> 
     179         * Searches the sub-trie of this trie node for a given sequence and returns 
     180         * the node associated with the sequence or null if no such node is found. 
     181         * </p> 
     182         *  
     183         * @param sequence 
     184         *            sequence that is searched for 
     185         * @return node associated with the sequence 
     186         * @see Trie#find(List) 
     187         */ 
    84188        public TrieNode<T> find(List<T> subsequence) { 
    85189                TrieNode<T> result = null; 
    86                 if( subsequence.isEmpty() ) { 
     190                if (subsequence.isEmpty()) { 
    87191                        result = this; 
    88192                } else { 
    89193                        TrieNode<T> node = getChild(subsequence.get(0)); 
    90                         if( node!=null ) { 
     194                        if (node != null) { 
    91195                                subsequence.remove(0); 
    92196                                result = node.find(subsequence); 
     
    95199                return result; 
    96200        } 
    97          
    98         // returns all symbols that follow this node 
     201 
     202        /** 
     203         * <p> 
     204         * Returns a collection of all symbols that follow a this node, i.e., the 
     205         * symbols associated with the children of this node. 
     206         * </p> 
     207         *  
     208         * @return symbols follow this node 
     209         * @see TrieNode#getFollowingSymbols() 
     210         */ 
    99211        public Collection<T> getFollowingSymbols() { 
    100212                Collection<T> followingSymbols = new LinkedList<T>(); 
    101                 for( TrieNode<T> child : children ) { 
     213                for (TrieNode<T> child : children) { 
    102214                        followingSymbols.add(child.getSymbol()); 
    103215                } 
    104216                return followingSymbols; 
    105217        } 
    106          
     218 
     219        /** 
     220         * <p> 
     221         * The string representation of a node is {@code symbol.toString()#count} 
     222         * </p> 
     223         *  
     224         * @see java.lang.Object#toString() 
     225         */ 
    107226        @Override 
    108227        public String toString() { 
    109                 String str = symbol.toString()+" #"+count; 
    110                 if( !children.isEmpty() ) { 
     228                String str = symbol.toString() + " #" + count; 
     229                if (!children.isEmpty()) { 
    111230                        str += StringTools.ENDLINE + children.toString(); 
    112231                } 
    113                 return str;  
    114         } 
    115  
    116         public void getGraph(TrieVertex parent, DelegateTree<TrieVertex, Edge> graph) { 
     232                return str; 
     233        } 
     234 
     235        /** 
     236         * <p> 
     237         * Generates a {@link Tree} represenation of the trie. 
     238         * </p> 
     239         *  
     240         * @param parent 
     241         *            parent vertex in the generated tree 
     242         * @param graph 
     243         *            complete tree 
     244         */ 
     245        void getGraph(TrieVertex parent, DelegateTree<TrieVertex, Edge> graph) { 
    117246                TrieVertex currentVertex; 
    118                 if( symbol==null ){ 
     247                if (symbol == null) { 
    119248                        currentVertex = new TrieVertex("root"); 
    120249                        graph.addVertex(currentVertex); 
    121250                } else { 
    122                         currentVertex = new TrieVertex(getSymbol().toString()+"#"+getCount()); 
    123                         graph.addChild( new Edge() , parent, currentVertex ); 
    124                 } 
    125                 for( TrieNode<T> node : children ) { 
     251                        currentVertex = new TrieVertex(getSymbol().toString() + "#" 
     252                                        + getCount()); 
     253                        graph.addChild(new Edge(), parent, currentVertex); 
     254                } 
     255                for (TrieNode<T> node : children) { 
    126256                        node.getGraph(currentVertex, graph); 
    127                 }                
    128         } 
    129          
     257                } 
     258        } 
     259 
     260        /** 
     261         * <p> 
     262         * Appends the current node to the dot representation of the trie. 
     263         * </p> 
     264         *  
     265         * @param stringBuilder 
     266         *            {@link StringBuilder} to which the dot representation is 
     267         *            appended 
     268         */ 
    130269        void appendDotRepresentation(StringBuilder stringBuilder) { 
    131270                String thisSaneId; 
    132                 if( symbol==null ) { 
     271                if (symbol == null) { 
    133272                        thisSaneId = "root"; 
    134273                } else { 
    135                         thisSaneId = symbol.toString().replace("\"", "\\\"").replaceAll("[\r\n]","")+"#"+count; 
    136                 } 
    137                 stringBuilder.append(" " + hashCode() + " [label=\""+thisSaneId+"\"];" + StringTools.ENDLINE); 
    138                 for( TrieNode<T> childNode : children ) { 
    139                         stringBuilder.append(" "+hashCode()+" -> " + childNode.hashCode() + ";" + StringTools.ENDLINE); 
    140                 } 
    141                 for( TrieNode<T> childNode : children ) { 
     274                        thisSaneId = symbol.toString().replace("\"", "\\\"") 
     275                                        .replaceAll("[\r\n]", "") 
     276                                        + "#" + count; 
     277                } 
     278                stringBuilder.append(" " + hashCode() + " [label=\"" + thisSaneId 
     279                                + "\"];" + StringTools.ENDLINE); 
     280                for (TrieNode<T> childNode : children) { 
     281                        stringBuilder.append(" " + hashCode() + " -> " 
     282                                        + childNode.hashCode() + ";" + StringTools.ENDLINE); 
     283                } 
     284                for (TrieNode<T> childNode : children) { 
    142285                        childNode.appendDotRepresentation(stringBuilder); 
    143286                } 
Note: See TracChangeset for help on using the changeset viewer.