Class TreeRewriteRule<V>

  • All Implemented Interfaces:
    TreeRewriter<V>, Serializable

    public final class TreeRewriteRule<V>
    extends Object
    implements TreeRewriter<V>, Serializable
    Represents a tree rewrite rule. A rewrite rule consists of a match pattern, which must be matched, and a substitution pattern, which is expanded and replaces the variables in the pattern. Some simple arithmetic rewrite rules.
         add($x,0) -> $x
         mul($x,1) -> $x
    The substitution pattern may only use variables, already defined in the match pattern. So, the creation of the following rewrite rule s would lead to an IllegalArgumentException:
         add($x,0) -> $y
         mul(0,1) -> mul($x,1)
    See Also:
    Tree rewriting systems, Serialized Form
    • Constructor Detail

      • TreeRewriteRule

        public TreeRewriteRule​(TreePattern<V> left,
                               TreePattern<V> right)
        Create a new rewrite rule from the given matching (left) and replacement (right) pattern.
        left - the matching pattern of the rule
        right - the substitution pattern
        NullPointerException - if one of the arguments is null
        IllegalArgumentException - if the template pattern uses variables not defined in the matcher pattern
    • Method Detail

      • left

        public TreePattern<Vleft()
        Return the rule matching pattern.
        the rule matching pattern
      • right

        public TreePattern<Vright()
        Return the replacement pattern of the rule.
        the replacement pattern of the rule
      • map

        public <B> TreeRewriteRule<B> map​(Function<? super V,​? extends B> mapper)
        Maps this rewrite rule from type V to type B.
        Type Parameters:
        B - the target type
        mapper - the type mapper
        a new rewrite rule for the mapped type
        NullPointerException - if the mapper is null
      • rewrite

        public int rewrite​(TreeNode<V> tree,
                           int limit)
        Description copied from interface: TreeRewriter
        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.
        Specified by:
        rewrite in interface TreeRewriter<V>
        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.
        the number of rewrites applied to the input tree
      • parse

        public static <V> TreeRewriteRule<V> parse​(String rule,
                                                   Function<? super String,​? extends V> mapper)
        Compiles the string representation of a rewrite rule:
             add($x,0) -> $x
             mul($x,1) -> $x
        Type Parameters:
        V - the tree node type
        rule - the rewrite rule
        mapper - the mapper function which converts the node value into the actual type V
        a new rewrite rule, compiled from the given rule string
        IllegalArgumentException - if the rewrite rule is invalid
        NullPointerException - if on of the arguments is null