Class TreePattern<V>

java.lang.Object
io.jenetics.ext.rewriting.TreePattern<V>
Type Parameters:
V - the value type of the tree than can match by this pattern
All Implemented Interfaces:
Serializable

public final class TreePattern<V> extends Object implements Serializable
This class serves two purposes. Firstly, it is used as a classical pattern, which is used to find matches against a matching tree. Secondly, it can expand a given pattern to a full tree with a given pattern variable to subtree mapping.

Matching trees

A compiled representation of a tree pattern. A tree pattern, specified as a parentheses string, must first be compiled into an instance of this class. The resulting pattern can then be used to create a TreeMatcher object that can match arbitrary trees against the tree pattern. All the states involved in performing a match reside in the matcher, so many matchers can share the same pattern.

The string representation of a tree pattern is a parenthesis tree string, with a special wildcard syntax for arbitrary subtrees. The subtree variables are prefixed with a '$' and must be a valid Java identifier.

final TreePattern<String> p1 = TreePattern.compile("add($a,add($b,sin(x)))");
final TreePattern<String> p2 = TreePattern.compile("pow($x,$y)");
If you need to have values which start with a '$' character, you can escape it with a '\'.
final TreePattern<String> p1 = TreePattern.compile("concat($x,\\$foo)");
The second value, $foo, of the concat function is not treated as pattern variable.

If you want to match against trees with a different value type than String, you have to specify an additional type mapper function when compiling the pattern string.

final TreePattern<Op<Double>> p = TreePattern.compile(
    "add($a,add($b,sin(x)))",
    MathOp::toMathOp
);

Expanding trees

The second functionality of the tree pattern is to expand a pattern to a whole tree with a given pattern variable to subtree mapping.
final TreePattern<String> pattern = TreePattern.compile("add($x,$y,1)");
final Map<Var<String>, Tree<String, ?>> vars = Map.of(
    Var.of("x"), TreeNode.parse("sin(x)"),
    Var.of("y"), TreeNode.parse("sin(y)")
);

final Tree<String, ?> tree = pattern.expand(vars);
assertEquals(tree.toParenthesesString(), "add(sin(x),sin(y),1)");
Since:
5.0
Version:
7.0
See Also:
  • Constructor Details

  • Method Details

    • vars

      Return the unmodifiable set of variables, defined in this pattern. The variables are returned without the angle brackets.
      Returns:
      the variables, defined in this pattern
    • map

      public <B> TreePattern<B> map(Function<? super V,? extends B> mapper)
      Maps this tree-pattern from type V to type B.
      Type Parameters:
      B - the target type
      Parameters:
      mapper - the type mapper
      Returns:
      a new tree-pattern for the mapped type
    • matcher

      public TreeMatcher<V> matcher(Tree<V,?> tree)
      Creates a matcher that will match the given input tree against this pattern.
      Parameters:
      tree - the tree to be matched
      Returns:
      a new matcher for this pattern
      Throws:
      NullPointerException - if the arguments is null
    • match

      public Optional<TreeMatchResult<V>> match(Tree<V,?> tree)
      Try to match the given tree against this pattern.
      Parameters:
      tree - the tree to be matched
      Returns:
      the match result, or Optional.empty() if the given tree doesn't match
      Throws:
      NullPointerException - if the arguments is null
    • matches

      public boolean matches(Tree<V,?> tree)
      Tests whether the given input tree matches this pattern.
      Parameters:
      tree - the tree to be matched
      Returns:
      true if the tree matches this pattern, false otherwise
      Throws:
      NullPointerException - if one of the arguments is null
    • expand

      public TreeNode<V> expand(Map<TreePattern.Var<V>,Tree<V,?>> vars)
      Expands this pattern with the given variable mapping.
      Parameters:
      vars - the variables to use for expanding this pattern
      Returns:
      the expanded tree pattern
      Throws:
      NullPointerException - if one of the arguments is null
      IllegalArgumentException - if not all needed variables are part of the variables map
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • compile

      public static TreePattern<String> compile(String pattern)
      Compiles the given tree pattern string.
      Parameters:
      pattern - the tree pattern string
      Returns:
      the compiled pattern
      Throws:
      NullPointerException - if the given pattern is null
      IllegalArgumentException - if the given parentheses tree string doesn't represent a valid pattern tree or one of the variable names is not a valid (Java) identifier
    • compile

      public static <V> TreePattern<V> compile(String pattern, Function<? super String,? extends V> mapper)
      Compiles the given tree pattern string.
      Type Parameters:
      V - the value type of the tree than can be matched by the pattern
      Parameters:
      pattern - the tree pattern string
      mapper - the mapper which converts the serialized string value to the desired type
      Returns:
      the compiled pattern
      Throws:
      NullPointerException - if the given pattern is null
      IllegalArgumentException - if the given parentheses tree string doesn't represent a valid pattern tree or one of the variable names is not a valid (Java) identifier