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
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.
If you need to have values which start with a '$' character, you can escape
it with a '\'.
The second value,
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 aTreeMatcher 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)");
final TreePattern<String> p1 = TreePattern.compile("concat($x,\\$foo)");
$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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA sealed interface, which constitutes the nodes of a pattern tree.static final recordThis class represents a constant pattern value, which can be part of a whole subtree.static final recordRepresents a placeholder (variable) for an arbitrary subtree. -
Constructor Summary
ConstructorsConstructorDescriptionTreePattern(Tree<TreePattern.Decl<V>, ?> pattern) Create a new tree-pattern object from the given pattern tree. -
Method Summary
Modifier and TypeMethodDescriptionstatic TreePattern<String> Compiles the given tree pattern string.static <V> TreePattern<V> Compiles the given tree pattern string.booleanExpandsthispattern with the given variable mapping.inthashCode()<B> TreePattern<B> Mapsthistree-pattern from typeVto typeB.Try to match the giventreeagainstthispattern.Creates a matcher that will match the given input tree againstthispattern.booleanTests whether the given inputtreematchesthispattern.toString()vars()Return the unmodifiable set of variables, defined inthispattern.
-
Constructor Details
-
TreePattern
Create a new tree-pattern object from the given pattern tree.- Parameters:
pattern- the pattern-tree- Throws:
NullPointerException- if the givenpatternisnullIllegalArgumentException- ifTreePattern.Varnodes are not leaf nodes;Tree.isLeaf()isfalse
-
-
Method Details
-
vars
Return the unmodifiable set of variables, defined inthispattern. The variables are returned without the angle brackets.- Returns:
- the variables, defined in this pattern
-
map
Mapsthistree-pattern from typeVto typeB.- Type Parameters:
B- the target type- Parameters:
mapper- the type mapper- Returns:
- a new tree-pattern for the mapped type
-
matcher
Creates a matcher that will match the given input tree againstthispattern.- Parameters:
tree- the tree to be matched- Returns:
- a new matcher for
thispattern - Throws:
NullPointerException- if the arguments isnull
-
match
Try to match the giventreeagainstthispattern.- Parameters:
tree- the tree to be matched- Returns:
- the match result, or
Optional.empty()if the giventreedoesn't match - Throws:
NullPointerException- if the arguments isnull
-
matches
Tests whether the given inputtreematchesthispattern.- Parameters:
tree- the tree to be matched- Returns:
trueif thetreematchesthispattern,falseotherwise- Throws:
NullPointerException- if one of the arguments isnull
-
expand
Expandsthispattern with the given variable mapping.- Parameters:
vars- the variables to use for expandingthispattern- Returns:
- the expanded tree pattern
- Throws:
NullPointerException- if one of the arguments isnullIllegalArgumentException- if not all needed variables are part of thevariablesmap
-
hashCode
-
equals
-
toString
-
compile
Compiles the given tree pattern string.- Parameters:
pattern- the tree pattern string- Returns:
- the compiled pattern
- Throws:
NullPointerException- if the given pattern isnullIllegalArgumentException- 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 stringmapper- the mapper which converts the serialized string value to the desired type- Returns:
- the compiled pattern
- Throws:
NullPointerException- if the given pattern isnullIllegalArgumentException- 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
-