Package org.moeaframework.util.grammar
Class ContextFreeGrammar
java.lang.Object
org.moeaframework.util.grammar.ContextFreeGrammar
A context-free grammar. The rule at index
0
is the starting rule when building derivation trees.- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
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; ornull
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.Returns the rule for the specified symbol; ornull
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
isValid()
Returnstrue
if this grammar is valid;false
otherwise.void
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
size()
Returns the number of rules contained in this grammar.
-
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
Adds a rule to this grammar.- Parameters:
rule
- the rule to be added
-
remove
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
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
Returns the rule for the specified symbol; ornull
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
Returns the grammar derivation using the construction rules of Grammatical Evolution on the specified codon array; ornull
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()Returnstrue
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
-