Package io.jenetics.ext.rewriting
Class TreeRewriteRule<V>
- java.lang.Object
-
- io.jenetics.ext.rewriting.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
IllegalArgumentException
:add($x,0) -> $y mul(0,1) -> mul($x,1)
- Since:
- 5.0
- Version:
- 5.0
- See Also:
- Tree rewriting systems, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description TreeRewriteRule(TreePattern<V> left, TreePattern<V> right)
Create a new rewrite rule from the given matching (left
) and replacement (right
) pattern.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object obj)
int
hashCode()
TreePattern<V>
left()
Return the rule matching pattern.<B> TreeRewriteRule<B>
map(Function<? super V,? extends B> mapper)
Mapsthis
rewrite rule from typeV
to typeB
.static TreeRewriteRule<String>
parse(String rule)
Compiles the string representation of a rewrite rule:static <V> TreeRewriteRule<V>
parse(String rule, Function<? super String,? extends V> mapper)
Compiles the string representation of a rewrite rule:int
rewrite(TreeNode<V> tree, int limit)
Applies the rewriting to the given (mutable)tree
.TreePattern<V>
right()
Return the replacement pattern of the rule.String
toString()
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.jenetics.ext.rewriting.TreeRewriter
rewrite
-
-
-
-
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.- Parameters:
left
- the matching pattern of the ruleright
- the substitution pattern- Throws:
NullPointerException
- if one of the arguments isnull
IllegalArgumentException
- if the template pattern uses variables not defined in the matcher pattern
-
-
Method Detail
-
left
public TreePattern<V> left()
Return the rule matching pattern.- Returns:
- the rule matching pattern
-
right
public TreePattern<V> right()
Return the replacement pattern of the rule.- Returns:
- the replacement pattern of the rule
-
map
public <B> TreeRewriteRule<B> map(Function<? super V,? extends B> mapper)
Mapsthis
rewrite rule from typeV
to typeB
.- Type Parameters:
B
- the target type- Parameters:
mapper
- the type mapper- Returns:
- a new rewrite rule for the mapped type
- Throws:
NullPointerException
- if themapper
isnull
-
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 thelimit
parameter, the termination of the tree-rewrite process can be guaranteed.- Specified by:
rewrite
in interfaceTreeRewriter<V>
- Parameters:
tree
- the tree to be rewrittenlimit
- the maximal number this rewrite rule is applied to the given tree. This guarantees the termination of the rewrite method.- Returns:
- 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- Parameters:
rule
- the rewrite rulemapper
- the mapper function which converts the node value into the actual typeV
- Returns:
- a new rewrite rule, compiled from the given rule string
- Throws:
IllegalArgumentException
- if the rewrite rule is invalidNullPointerException
- if on of the arguments isnull
-
parse
public static TreeRewriteRule<String> parse(String rule)
Compiles the string representation of a rewrite rule:add($x,0) -> $x mul($x,1) -> $x
- Parameters:
rule
- the rewrite rule- Returns:
- a new rewrite rule, compiled from the given rule string
- Throws:
IllegalArgumentException
- if the rewrite rule is invalidNullPointerException
- if on of the arguments isnull
-
-