Interface TreeRewriter<V>

  • All Known Implementing Classes:
    TreeRewriteRule, TRS
    Functional Interface:
    This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    @FunctionalInterface
    public interface TreeRewriter<V>
    Interface for rewriting a given tree. The rewrite is performed on a mutable TreeNode and is done in place.

    Description from Wikipedia: In mathematics, computer science, and logic, rewriting covers a wide range of (potentially non-deterministic) methods of replacing sub-terms of a formula with other terms. In their most basic form, they consist of a set of objects, plus relations on how to transform those objects. Rewriting can be non-deterministic. One rule to rewrite a term could be applied in many different ways to that term, or more than one rule could be applicable. Rewriting systems then do not provide an algorithm for changing one term to another, but a set of possible rule applications. When combined with an appropriate algorithm, however, rewrite systems can be viewed as computer programs, and several theorem provers and declarative programming languages are based on term rewriting.

    Since:
    5.0
    Version:
    5.0
    API Note:
    The rewriting is done in place, to a mutable TreeNode object.
    • Method Detail

      • rewrite

        int rewrite​(TreeNode<V> tree,
                    int limit)
        Applies the rewriting to the given (mutable) tree. The tree rewrite is done in place. Via the limit parameter, the termination of the tree-rewrite process can be guaranteed.
        Parameters:
        tree - the tree to be rewritten
        limit - the maximal number this rewrite rule is applied to the given tree. This guarantees the termination of the rewrite method.
        Returns:
        the number of rewrites applied to the input tree
        Throws:
        NullPointerException - if the given tree is null
        IllegalArgumentException - if the limit is smaller than one
      • rewrite

        default int rewrite​(TreeNode<V> tree)
        Applies the rewriting to the given (mutable) tree. The tree rewrite is done in place. The limit of the applied rewrites is set unlimited (Integer.MAX_VALUE).
        Parameters:
        tree - the tree to be rewritten
        Returns:
        true if the tree has been changed (rewritten) by this method, false if the tree hasn't been changed
        Throws:
        NullPointerException - if the given tree is null
        See Also:
        rewrite(TreeNode, int)
      • rewrite

        static <V> int rewrite​(TreeNode<V> tree,
                               int limit,
                               Iterable<? extends TreeRewriter<V>> rewriters)
        Rewrites the given tree by applying the given rewriters. This method to apply the all rewriters, in the order they are given in the sequence, until the tree stays unchanged.
        Type Parameters:
        V - the tree value type
        Parameters:
        tree - the tree to rewrite
        limit - the maximal number this rewrite rule is applied to the given tree. This guarantees the termination of the rewrite method.
        rewriters - the rewriters applied to the tree
        Returns:
        true if the tree has been changed (rewritten) by this method, false if the tree hasn't been changed
        Throws:
        NullPointerException - if one of the arguments is null
        IllegalArgumentException - if the limit is smaller than zero
      • rewrite

        static <V> int rewrite​(TreeNode<V> tree,
                               Iterable<? extends TreeRewriter<V>> rewriters)
        Rewrites the given tree by applying the given rewriters. This method to apply the all rewriters, in the order they are given in the sequence, until the tree stays unchanged.
        Type Parameters:
        V - the tree value type
        Parameters:
        tree - the tree to rewrite
        rewriters - the rewriters applied to the tree
        Returns:
        true if the tree has been changed (rewritten) by this method, false if the tree hasn't been changed
        Throws:
        NullPointerException - if one of the arguments is null
        See Also:
        rewrite(TreeNode, int, Iterable)