Class NodeTransformer

  • All Implemented Interfaces:
    ExprTreeVisitor, TreeVisitor

    public class NodeTransformer
    extends java.lang.Object
    implements ExprTreeVisitor
    NodeTransformer is an expression tree visitor with bottom-up substitution policy. Substitutions take place accordingly to set of rules passed to transformer before traversal.
    • Constructor Detail

      • NodeTransformer

        public NodeTransformer()
        Create new transformer instance containing no substitution rules.
      • NodeTransformer

        public NodeTransformer​(java.util.Map<java.lang.Enum<?>,​TransformerRule> rules)
        Create new transformer instance with given substitutions rules.
        Parameters:
        rules - Map of rules. See addRule() for details.
    • Method Detail

      • walk

        public void walk​(Node root)
        Traverse and apply substitutions to single expression tree. Resulting expression can be acquired via getResult().
        Parameters:
        root - Root of a tree to be traversed.
      • walk

        public void walk​(java.util.Collection<? extends Node> trees)
        Traverse and apply substitutions to expression forest. Resulting expression can be acquired via getResult().
        Parameters:
        trees - Collection of root nodes of trees to be traversed.
      • reset

        public void reset()
        Reset transformer to initial state keeping all rules.
      • addRule

        public void addRule​(java.lang.Enum<?> opId,
                            TransformerRule rule)
        Add substitution rule.

        For rule to be applied to node in expression tree several conditions needs to be hold: 1) either node is NodeOperation with opId operation or node is a Node subclass which kind is opId; 2) rule.isApplicable() should be true for node given.

        Parameters:
        opId - Target node kind identifier.
        rule - Rule to be added.
        Throws:
        java.lang.IllegalArgumentException - if any of the parameters is null.
      • clearRules

        public void clearRules​(java.lang.Enum<?> opId)
        Removed all the registered rules for the specified operation.
        Parameters:
        opId - The operation identifier.
      • getResult

        public java.util.List<Node> getResult()
        Get collection of expression trees resulting from substitutions done during traversals.
        Returns:
        List of results in order of base forest iteration.
      • getStatus

        public TreeVisitor.Status getStatus()
        Description copied from interface: TreeVisitor
        Returns the current status of the visitor. The status guides further actions of the walker.
        Specified by:
        getStatus in interface TreeVisitor
        Returns:
        Current visitor status.
      • onBegin

        public void onBegin()
        Description copied from interface: TreeVisitor
        Notifies that processing of a hierarchical structure has been started.
        Specified by:
        onBegin in interface TreeVisitor
      • onEnd

        public void onEnd()
        Description copied from interface: TreeVisitor
        Notifies that processing of a hierarchical structure has been finished.
        Specified by:
        onEnd in interface TreeVisitor
      • getOperandOrder

        public int[] getOperandOrder()
        Description copied from interface: ExprTreeVisitor
        Returns an array of operand indexes that specify in which order the operands of the currently visited operator should be visited. If the order is standard (i.e. [0..N-1]), null is returned.
        Specified by:
        getOperandOrder in interface ExprTreeVisitor
        Returns:
        Array of operand indexes or null for the standard order.
      • onOperandBegin

        public void onOperandBegin​(NodeOperation expr,
                                   Node operand,
                                   int index)
        Description copied from interface: ExprTreeVisitor
        Notifies that visiting an expression operand has started.
        Specified by:
        onOperandBegin in interface ExprTreeVisitor
        Parameters:
        expr - Operation node.
        operand - Operand node.
        index - Operand index.
      • onOperandEnd

        public void onOperandEnd​(NodeOperation expr,
                                 Node operand,
                                 int index)
        Description copied from interface: ExprTreeVisitor
        Notifies that visiting an expression operand has finished.
        Specified by:
        onOperandEnd in interface ExprTreeVisitor
        Parameters:
        expr - Operation node.
        operand - Operand node.
        index - Operand index.
      • onValue

        public void onValue​(NodeValue value)
        Description copied from interface: ExprTreeVisitor
        Notifies that a value node has been visited.
        Specified by:
        onValue in interface ExprTreeVisitor
        Parameters:
        value - Value node.
      • onBoundVariableBegin

        public void onBoundVariableBegin​(NodeBinding node,
                                         NodeVariable variable,
                                         Node value)
        Description copied from interface: ExprTreeVisitor
        Notifies that visiting a bound variable has started. Bound value expression will be visited next as general expression.

        Bound variables are not visited at all.

        Specified by:
        onBoundVariableBegin in interface ExprTreeVisitor
        Parameters:
        node - Binding node.
        variable - Bound variable reference.
        value - Bound value expression.
      • onBoundVariableEnd

        public void onBoundVariableEnd​(NodeBinding node,
                                       NodeVariable variable,
                                       Node value)
        Description copied from interface: ExprTreeVisitor
        Notifies that visiting a bound variable has finished.
        Specified by:
        onBoundVariableEnd in interface ExprTreeVisitor
        Parameters:
        node - Binding node.
        variable - Bound variable reference.
        value - Bound value expression.