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
Modifier and TypeClassDescriptionstatic interface
A sealed interface, which constitutes the nodes of a pattern tree.static final record
This class represents a constant pattern value, which can be part of a whole subtree.static final record
Represents a placeholder (variable) for an arbitrary subtree. -
Constructor Summary
ConstructorDescriptionTreePattern
(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.boolean
Expandsthis
pattern with the given variable mapping.int
hashCode()
<B> TreePattern
<B> Mapsthis
tree-pattern from typeV
to typeB
.Try to match the giventree
againstthis
pattern.Creates a matcher that will match the given input tree againstthis
pattern.boolean
Tests whether the given inputtree
matchesthis
pattern.toString()
vars()
Return the unmodifiable set of variables, defined inthis
pattern.
-
Constructor Details
-
TreePattern
Create a new tree-pattern object from the given pattern tree.- Parameters:
pattern
- the pattern-tree- Throws:
NullPointerException
- if the givenpattern
isnull
IllegalArgumentException
- ifTreePattern.Var
nodes are not leaf nodes;Tree.isLeaf()
isfalse
-
-
Method Details
-
vars
Return the unmodifiable set of variables, defined inthis
pattern. The variables are returned without the angle brackets.- Returns:
- the variables, defined in this pattern
-
map
Mapsthis
tree-pattern from typeV
to 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 againstthis
pattern.- Parameters:
tree
- the tree to be matched- Returns:
- a new matcher for
this
pattern - Throws:
NullPointerException
- if the arguments isnull
-
match
Try to match the giventree
againstthis
pattern.- Parameters:
tree
- the tree to be matched- Returns:
- the match result, or
Optional.empty()
if the giventree
doesn't match - Throws:
NullPointerException
- if the arguments isnull
-
matches
Tests whether the given inputtree
matchesthis
pattern.- Parameters:
tree
- the tree to be matched- Returns:
true
if thetree
matchesthis
pattern,false
otherwise- Throws:
NullPointerException
- if one of the arguments isnull
-
expand
Expandsthis
pattern with the given variable mapping.- Parameters:
vars
- the variables to use for expandingthis
pattern- Returns:
- the expanded tree pattern
- Throws:
NullPointerException
- if one of the arguments isnull
IllegalArgumentException
- if not all needed variables are part of thevariables
map
-
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 isnull
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 stringmapper
- the mapper which converts the serialized string value to the desired type- Returns:
- the compiled pattern
- Throws:
NullPointerException
- if the given pattern isnull
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
-