Class DerivationTreeGenerator<T>

java.lang.Object
io.jenetics.ext.grammar.DerivationTreeGenerator<T>
All Implemented Interfaces:
Generator<T,Tree<Cfg.Symbol<T>,?>>

public final class DerivationTreeGenerator<T> extends Object implements Generator<T,Tree<Cfg.Symbol<T>,?>>
Standard implementation of a derivation-tree generator. The following code snippet lets you generate a derivation tree from a given grammar.
final Cfg<String> cfg = Bnf.parse("""
    <expr> ::= ( <expr> <op> <expr> ) | <num> | <var> |  <fun> ( <arg>, <arg> )
    <fun>  ::= FUN1 | FUN2
    <arg>  ::= <expr> | <var> | <num>
    <op>   ::= + | - | * | /
    <var>  ::= x | y
    <num>  ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
    """
);

final var random = RandomGenerator.of("L64X256MixRandom");
final var generator = new DerivationTreeGenerator<String>(
    SymbolIndex.of(random),
    1_000
);
final Tree<Symbol<String>, ?> tree = generator.generate(cfg);
Since:
7.1
Version:
7.1
See Also:
  • Constructor Details

    • DerivationTreeGenerator

      public DerivationTreeGenerator(SymbolIndex index, int limit)
      Create a new derivation tree generator from the given parameters.
      Parameters:
      index - the symbol index function used for generating the derivation tree
      limit - the maximal allowed nodes of the tree. If the generated tree exceeds this length, the generation is interrupted and an empty tree is returned. If a tree is empty can be checked with Tree.isEmpty().
  • Method Details

    • generate

      public Tree<Cfg.Symbol<T>,?> generate(Cfg<? extends T> cfg)
      Generates a new derivation tree from the given grammar, cfg.
      Specified by:
      generate in interface Generator<T,Tree<Cfg.Symbol<T>,?>>
      Parameters:
      cfg - the generating grammar
      Returns:
      a newly created derivation tree, or an empty tree if the number of nodes exceeds the defined node limit
      See Also: