Package io.jenetics.ext.rewriting
Class TRS<V>
- java.lang.Object
-
- io.jenetics.ext.rewriting.TRS<V>
-
- All Implemented Interfaces:
TreeRewriter<V>
,Serializable
public final class TRS<V> extends Object implements TreeRewriter<V>, Serializable
This class represents a Tree Rewrite System, which consists of a set of Tree Rewrite Rules.final TRS<String> trs = TRS.parse( "add(0,$x) -> $x", "add(S($x),$y) -> S(add($x,$y))", "mul(0,$x) -> 0", "mul(S($x),$y) -> add(mul($x,$y),$y)" ); // Converting the input tree into its normal form. final TreeNode<String> tree = TreeNode.parse("add(S(0),S(mul(S(0),S(S(0)))))"); trs.rewrite(tree); assert tree.equals(TreeNode.parse("S(S(S(S(0))))"));
- Since:
- 5.0
- Version:
- 5.0
- See Also:
TreeRewriteRule
, TRS, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description TRS(ISeq<TreeRewriteRule<V>> rules)
Create a new TRS from the given rewrite rules.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object obj)
int
hashCode()
<B> TRS<B>
map(Function<? super V,? extends B> mapper)
Mapsthis
TRS from typeV
to typeB
.static TRS<String>
parse(String... rules)
Create a new TRS from the given rewrite rules.static <V> TRS<V>
parse(Function<? super String,? extends V> mapper, String... rules)
Create a new TRS from the given rewrite rules and type mapper.int
rewrite(TreeNode<V> tree, int limit)
Applies the rewriting to the given (mutable)tree
.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
-
TRS
public TRS(ISeq<TreeRewriteRule<V>> rules)
Create a new TRS from the given rewrite rules.- Parameters:
rules
- the rewrite rules the TRS consists of- Throws:
NullPointerException
- if the givenrules
arenull
IllegalArgumentException
- if the givenrules
sequence is empty
-
-
Method Detail
-
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
-
map
public <B> TRS<B> map(Function<? super V,? extends B> mapper)
Mapsthis
TRS from typeV
to typeB
.- Type Parameters:
B
- the target type- Parameters:
mapper
- the type mapper- Returns:
- a new TRS for the mapped type
- Throws:
NullPointerException
- if themapper
isnull
-
parse
public static <V> TRS<V> parse(Function<? super String,? extends V> mapper, String... rules)
Create a new TRS from the given rewrite rules and type mapper.- Type Parameters:
V
- the tree value type the rewriter is working on- Parameters:
mapper
- the tree value type mapperrules
- the rewrite rules- Returns:
- a new TRS
- Throws:
NullPointerException
- if one of the arguments isnull
IllegalArgumentException
- if the givenrules
sequence is empty
-
parse
public static TRS<String> parse(String... rules)
Create a new TRS from the given rewrite rules.- Parameters:
rules
- the rewrite rules- Returns:
- a new TRS
- Throws:
NullPointerException
- if one of the arguments isnull
IllegalArgumentException
- if the givenrules
sequence is empty
-
-