Class TreeAnalyzer


  • public class TreeAnalyzer
    extends PostfixTreeWalker
    Analyze an expression, counting the number of nodes of each type, and recording which variables, functions and operators occurred in the expression.
    • Constructor Summary

      Constructors 
      Constructor Description
      TreeAnalyzer()
      Creates an empty tree analyser.
      TreeAnalyzer​(Node node)
      Create a new instance and Analyze the node.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void analyze​(Node node)
      Analyze a node, results will be added to the current total.
      java.util.Map<java.lang.Object,​java.lang.Integer> getConstants()
      A map with each constant and the number of times it occurs
      java.util.Map<java.lang.String,​java.lang.Integer> getFunctions()
      A map with each function used and the number of times it occurred
      int getMaxDepth()
      The depth of the tree
      int getNumConst()  
      int getNumFunctions()  
      int getNumNodes()  
      int getNumOperators()  
      int getNumUniqueVariables()  
      int getNumVariables()  
      java.util.Map<Operator,​java.lang.Integer> getOperators()
      A map with each operator used and the number of times it occurred
      java.lang.String[] getVariableNames()
      Return the names of the variables, sorted alphabetically.
      java.util.Map<Variable,​java.lang.Integer> getVariables()
      A map with each variable used and the number of times it occurred
      void merge​(TreeAnalyzer ta)
      Merge the result with the argument, producing total for all trees.
      java.lang.String summary()
      A short summary string
      java.lang.String toString()
      Reports statistics about the tree, number of nodes, depth, and the number of varibles, functions, operators and constants with the number of times each occurs.
      protected void visit​(ASTConstant node, int nchildren, int depth)
      Visit a constant node
      protected void visit​(ASTFunNode node, int nchildren, int depth)
      Visit a function node
      protected void visit​(ASTOpNode node, int nchildren, int depth)
      Visit an operator node
      protected void visit​(ASTVarNode node, int nchildren, int depth)
      Visit a variable node
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • TreeAnalyzer

        public TreeAnalyzer​(Node node)
                     throws JepException
        Create a new instance and Analyze the node.
        Parameters:
        node - node to analyze
        Throws:
        JepException
      • TreeAnalyzer

        public TreeAnalyzer()
        Creates an empty tree analyser. Useful for producing combined statistics via the merge(TreeAnalyzer) method.
    • Method Detail

      • analyze

        public void analyze​(Node node)
                     throws JepException
        Analyze a node, results will be added to the current total.
        Parameters:
        node -
        Throws:
        JepException
      • toString

        public java.lang.String toString()
        Reports statistics about the tree, number of nodes, depth, and the number of varibles, functions, operators and constants with the number of times each occurs.
        Overrides:
        toString in class java.lang.Object
      • summary

        public java.lang.String summary()
        A short summary string
        Returns:
        a one line summary
      • visit

        protected void visit​(ASTFunNode node,
                             int nchildren,
                             int depth)
        Description copied from class: PostfixTreeWalker
        Visit a function node
        Specified by:
        visit in class PostfixTreeWalker
        Parameters:
        node - the current node being visited
        nchildren - number of children of the node
        depth - depth of tree, root node is depth 1.
      • visit

        protected void visit​(ASTOpNode node,
                             int nchildren,
                             int depth)
        Description copied from class: PostfixTreeWalker
        Visit an operator node
        Specified by:
        visit in class PostfixTreeWalker
        Parameters:
        node - the current node being visited
        nchildren - number of children of the node
        depth - depth of tree, root node is depth 1.
      • visit

        protected void visit​(ASTVarNode node,
                             int nchildren,
                             int depth)
        Description copied from class: PostfixTreeWalker
        Visit a variable node
        Specified by:
        visit in class PostfixTreeWalker
        Parameters:
        node - the current node being visited
        nchildren - number of children of the node
        depth - depth of tree, root node is depth 1.
      • visit

        protected void visit​(ASTConstant node,
                             int nchildren,
                             int depth)
        Description copied from class: PostfixTreeWalker
        Visit a constant node
        Specified by:
        visit in class PostfixTreeWalker
        Parameters:
        node - the current node being visited
        nchildren - number of children of the node
        depth - depth of tree, root node is depth 1.
      • getMaxDepth

        public int getMaxDepth()
        The depth of the tree
        Returns:
        depth, depth of root node is 1
      • getNumConst

        public int getNumConst()
      • getNumFunctions

        public int getNumFunctions()
      • getNumNodes

        public int getNumNodes()
      • getNumOperators

        public int getNumOperators()
      • getNumVariables

        public int getNumVariables()
      • getNumUniqueVariables

        public int getNumUniqueVariables()
      • getOperators

        public java.util.Map<Operator,​java.lang.Integer> getOperators()
        A map with each operator used and the number of times it occurred
        Returns:
        a map whose keys are the operators and the values are the number of occurrences
      • getVariables

        public java.util.Map<Variable,​java.lang.Integer> getVariables()
        A map with each variable used and the number of times it occurred
        Returns:
        a map whose keys are the variables and the values are the number of occurrences
      • getVariableNames

        public java.lang.String[] getVariableNames()
        Return the names of the variables, sorted alphabetically.
        Returns:
        an array of variable names
      • getFunctions

        public java.util.Map<java.lang.String,​java.lang.Integer> getFunctions()
        A map with each function used and the number of times it occurred
      • getConstants

        public java.util.Map<java.lang.Object,​java.lang.Integer> getConstants()
        A map with each constant and the number of times it occurs
      • merge

        public void merge​(TreeAnalyzer ta)
        Merge the result with the argument, producing total for all trees.
        Parameters:
        ta -