Ignore:
Timestamp:
04/11/11 14:43:03 (13 years ago)
Author:
sherbold
Message:
  • major debugging of PPM and Trie. Results are now correct, but both classes need major refactorings
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/EventBenchCore/src/de/ugoe/cs/eventbench/ppm/Trie.java

    r1 r5  
    11package de.ugoe.cs.eventbench.ppm; 
    22 
     3import java.awt.Dimension; 
     4import java.awt.Rectangle; 
     5import java.awt.Shape; 
    36import java.util.LinkedList; 
    47import java.util.List; 
    58 
     9import javax.swing.JFrame; 
     10 
     11import org.apache.commons.collections15.Transformer; 
     12 
     13import edu.uci.ics.jung.algorithms.layout.Layout; 
     14import edu.uci.ics.jung.algorithms.layout.TreeLayout; 
     15import edu.uci.ics.jung.graph.DelegateTree; 
     16import edu.uci.ics.jung.graph.Tree; 
     17import edu.uci.ics.jung.visualization.BasicVisualizationServer; 
     18import edu.uci.ics.jung.visualization.decorators.ToStringLabeller; 
     19import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position; 
     20 
    621public class Trie<T> { 
    722         
     23        // Children of the Trie root 
     24        // should contain counts of all elements 
    825        private List<TrieNode<T>> children = new LinkedList<TrieNode<T>>(); 
    926         
     
    1128        public void add(List<T> subsequence) { 
    1229                if( !subsequence.isEmpty() ) { 
    13                         subsequence = new LinkedList<T>(subsequence);  // copy list! 
     30                        subsequence = new LinkedList<T>(subsequence);  // defensive copy! 
    1431                        T firstSymbol = subsequence.get(0); 
    1532                        getChildCreate(firstSymbol).add(subsequence); 
     
    3754        } 
    3855 
     56        // get the count of "sequence" 
    3957        public int getCount(List<T> sequence) { 
    4058                int count = 0; 
     
    4664        } 
    4765         
     66        // get the count of "sequence,follower" 
    4867        public int getCount(List<T> sequence, T follower) { 
    4968                List<T> tmpSequence = new LinkedList<T>(sequence); 
     
    5473         
    5574        public TrieNode<T> find(List<T> sequence) { 
     75                List<T> sequenceCopy = new LinkedList<T>(sequence); 
    5676                TrieNode<T> result = null; 
    57                 if( !sequence.isEmpty() ) { 
    58                         TrieNode<T> node = getChild(sequence.get(0)); 
     77                if( !sequenceCopy.isEmpty() ) { 
     78                        TrieNode<T> node = getChild(sequenceCopy.get(0)); 
    5979                        if( node!=null ) { 
    60                                 result = node.find(sequence); 
     80                                sequenceCopy.remove(0); 
     81                                result = node.find(sequenceCopy); 
    6182                        } 
    6283                } 
     
    6485        } 
    6586         
     87        // returns all symbols that follow the defined sequence 
    6688        public List<T> getFollowingSymbols(List<T> sequence) { 
    67                 List<T> result = null; 
    68                 TrieNode<T> node = find(sequence); 
    69                 if( node!=null ) { 
    70                         result = node.getFollowingSymbols(); 
     89                List<T> result = new LinkedList<T>(); 
     90                if( sequence==null || sequence.isEmpty() ) { 
     91                        for( TrieNode<T> child : children ) { 
     92                                result.add(child.getSymbol()); 
     93                        } 
     94                } else { 
     95                        TrieNode<T> node = find(sequence); 
     96                        if( node!=null ) { 
     97                                result = node.getFollowingSymbols(); 
     98                        } 
    7199                } 
    72100                return result; 
    73101        } 
    74102         
     103        // longest suffix of context, that is contained in the tree and whose children are leaves 
    75104        public List<T> getContextSuffix(List<T> context) { 
    76                 List<T> contextSuffix = new LinkedList<T>(context); 
     105                List<T> contextSuffix = new LinkedList<T>(context); // defensive copy 
    77106                boolean suffixFound = false; 
    78107                 
     
    87116                                        } 
    88117                                } 
    89                                 contextSuffix.remove(0); 
     118                                if( !suffixFound ) { 
     119                                        contextSuffix.remove(0); 
     120                                } 
    90121                        } 
    91122                } 
    92123                 
    93124                return contextSuffix; 
     125        } 
     126         
     127         
     128        static public class Edge{} 
     129         
     130        static public class TrieVertex { 
     131                private String id; 
     132                protected TrieVertex(String id) { 
     133                        this.id = id; 
     134                } 
     135                public String toString() { 
     136                        return id; 
     137                } 
     138        } 
     139         
     140        private Tree<TrieVertex, Edge> getGraph() { 
     141                DelegateTree<TrieVertex, Edge> graph = new DelegateTree<TrieVertex, Edge>(); 
     142                 
     143                TrieVertex root = new TrieVertex("root"); 
     144                graph.addVertex(root); 
     145                                 
     146                for( TrieNode<T> node : children ) { 
     147                        node.getGraph(root, graph); 
     148                } 
     149                 
     150                return graph; 
     151        } 
     152         
     153        public void display() { 
     154                Tree<TrieVertex, Edge> graph = this.getGraph(); 
     155                Layout<TrieVertex, Edge> layout = new TreeLayout<TrieVertex, Edge>(graph, 60); 
     156                // The BasicVisualizationServer<V,E> is parameterized by the edge types 
     157                BasicVisualizationServer<TrieVertex,Edge> vv = 
     158                new BasicVisualizationServer<TrieVertex,Edge>(layout); 
     159                vv.setPreferredSize(new Dimension(1100,850)); //Sets the viewing area size 
     160                 
     161                 
     162                final Rectangle rect = new Rectangle(40, 20); 
     163                         
     164                Transformer<TrieVertex, Shape> vertexShapeTransformer = 
     165                        new Transformer<TrieVertex, Shape>() { 
     166                                public Shape transform(TrieVertex s) { 
     167                                        return rect; 
     168                                } 
     169                }; 
     170                vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR); 
     171                vv.getRenderContext().setVertexShapeTransformer(vertexShapeTransformer); 
     172                vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<TrieVertex>()); 
     173                 
     174                JFrame frame = new JFrame("Trie"); 
     175                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     176                frame.getContentPane().add(vv); 
     177                frame.pack(); 
     178                frame.setVisible(true); 
    94179        } 
    95180         
Note: See TracChangeset for help on using the changeset viewer.