Class ContextFreeGrammar

java.lang.Object
org.moeaframework.util.grammar.ContextFreeGrammar

public class ContextFreeGrammar extends Object
A context-free grammar. The rule at index 0 is the starting rule when building derivation trees.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an empty grammar.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(Rule rule)
    Adds a rule to this grammar.
    build(int[] array)
    Returns the grammar derivation using the construction rules of Grammatical Evolution on the specified codon array; or null if the codon array failed to produce a valid derivation Whenever the derivation encounters a rule with multiple productions, the production used to expand the rule is chosen using this codon array.
    get(int index)
    Returns the rule at the specified index.
    get(Symbol symbol)
    Returns the rule for the specified symbol; or null if no rule with the specified symbol exists.
    int
    Returns the maximum number of times the builder will wrap around the codon array before failing to produce a valid derivation.
    boolean
    Returns true if this grammar is valid; false otherwise.
    void
    remove(Rule rule)
    Removes a rule from this grammar.
    void
    setWrapLimit(int wrapLimit)
    Sets the maximum number of times the builder will wrap around the codon array before failing to produce a valid derivation.
    int
    Returns the number of rules contained in this grammar.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ContextFreeGrammar

      public ContextFreeGrammar()
      Constructs an empty grammar.
  • Method Details

    • getWrapLimit

      public int getWrapLimit()
      Returns the maximum number of times the builder will wrap around the codon array before failing to produce a valid derivation.
      Returns:
      the maximum number of times the builder will wrap around the codon array before failing to produce a valid derivation
    • setWrapLimit

      public void setWrapLimit(int wrapLimit)
      Sets the maximum number of times the builder will wrap around the codon array before failing to produce a valid derivation.
      Parameters:
      wrapLimit - the maximum number of times the builder will wrap around the codon array before failing to produce a valid derivation
    • add

      public void add(Rule rule)
      Adds a rule to this grammar.
      Parameters:
      rule - the rule to be added
    • remove

      public void remove(Rule rule)
      Removes a rule from this grammar.
      Parameters:
      rule - the rule to be removed
    • size

      public int size()
      Returns the number of rules contained in this grammar.
      Returns:
      the number of rules contained in this grammar
    • get

      public Rule get(int index)
      Returns the rule at the specified index.
      Parameters:
      index - the index of the rule to be returned
      Returns:
      the rule at the specified index
      Throws:
      IndexOutOfBoundsException - if index is out of range ((index < 0) || (index >= size())
    • get

      public Rule get(Symbol symbol)
      Returns the rule for the specified symbol; or null if no rule with the specified symbol exists.
      Parameters:
      symbol - the symbol of the rule to be returned
      Returns:
      the rule for the specified symbol; or null if no rule with the specified symbol exists
    • build

      public String build(int[] array)
      Returns the grammar derivation using the construction rules of Grammatical Evolution on the specified codon array; or null if the codon array failed to produce a valid derivation Whenever the derivation encounters a rule with multiple productions, the production used to expand the rule is chosen using this codon array.
      Parameters:
      array - the codon array
      Returns:
      the grammar derivation using the construction rules of Grammatical Evolution on the specified codon array; or null if the codon array failed to produce a valid derivation
      Throws:
      GrammarException - if the codon array is empty
    • isValid

      public boolean isValid()
      Returns true if this grammar is valid; false otherwise. A grammar is valid if it is non-empty and all non-terminal symbols are defined by a rule in the grammar.
      Returns:
      true if this grammar is valid; false otherwise