Package io.jenetics.ext.rewriting
Class TreePattern<V>
- java.lang.Object
-
- io.jenetics.ext.rewriting.TreePattern<V>
-
- Type Parameters:
V
- the value type of the tree than can be matched 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 sub-tree 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 aTreeMatcher
object that can match arbitrary trees against the tree pattern. All of the state involved in performing a match resides 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 sub-trees. The sub-trees variables are prefixed with a '$' and must be a valid Java identifier.
If you need to have values which starts with a '$' character, you can escape it with a '\'.final TreePattern<String> p1 = TreePattern.compile("add($a,add($b,sin(x)))"); final TreePattern<String> p2 = TreePattern.compile("pow($x,$y)");
The second value,final TreePattern<String> p1 = TreePattern.compile("concat($x,\\$foo)");
$foo
, of theconcat
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 sub-tree 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:
- 5.0
- See Also:
TreeRewriteRule
,Tree.toParenthesesString()
,TreeMatcher
, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TreePattern.Decl<V>
A sealed class, which constitutes the nodes of a pattern tree.static class
TreePattern.Val<V>
This class represents a constant pattern value, which can be part of a whole sub-tree.static class
TreePattern.Var<V>
Represents a placeholder (variable) for an arbitrary sub-tree.
-
Constructor Summary
Constructors Constructor Description TreePattern(Tree<TreePattern.Decl<V>,?> pattern)
Create a new tree-pattern object from the given pattern tree.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static TreePattern<String>
compile(String pattern)
Compiles the given tree pattern string.static <V> TreePattern<V>
compile(String pattern, Function<? super String,? extends V> mapper)
Compiles the given tree pattern string.boolean
equals(Object obj)
TreeNode<V>
expand(Map<TreePattern.Var<V>,Tree<V,?>> vars)
Expandsthis
pattern with the given variable mapping.int
hashCode()
<B> TreePattern<B>
map(Function<? super V,? extends B> mapper)
Mapsthis
tree-pattern from typeV
to typeB
.Optional<TreeMatchResult<V>>
match(Tree<V,?> tree)
Try to match the giventree
againstthis
pattern.TreeMatcher<V>
matcher(Tree<V,?> tree)
Creates a matcher that will match the given input tree againstthis
pattern.boolean
matches(Tree<V,?> tree)
Tests whether the given inputtree
matchesthis
pattern.String
toString()
SortedSet<TreePattern.Var<V>>
vars()
Return the unmodifiable set of variables, defined inthis
pattern.
-
-
-
Constructor Detail
-
TreePattern
public TreePattern(Tree<TreePattern.Decl<V>,?> pattern)
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 Detail
-
vars
public SortedSet<TreePattern.Var<V>> 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
public <B> TreePattern<B> map(Function<? super V,? extends B> mapper)
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
public TreeMatcher<V> matcher(Tree<V,?> tree)
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
public Optional<TreeMatchResult<V>> match(Tree<V,?> tree)
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
public boolean matches(Tree<V,?> tree)
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
public TreeNode<V> expand(Map<TreePattern.Var<V>,Tree<V,?>> vars)
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
-
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 isnull
IllegalArgumentException
- if the given parentheses tree string doesn't represent a valid pattern tree or one of the variable name 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 name is not a valid (Java) identifier
-
-