diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2017-10-08 22:39:59 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2017-10-08 22:39:59 -0300 |
| commit | c82e3b3b2de0633317ec8fc85925e91422820597 (patch) | |
| tree | 96567416ce23c5ce85601f9cedc3a94bb1c55cba /base/src/main/java/bjc/utils/gen/RandomGrammar.java | |
| parent | b3ac1c8690c3e14c879913e5dcc03a5f5e14876e (diff) | |
Start splitting into maven modules
Diffstat (limited to 'base/src/main/java/bjc/utils/gen/RandomGrammar.java')
| -rw-r--r-- | base/src/main/java/bjc/utils/gen/RandomGrammar.java | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/base/src/main/java/bjc/utils/gen/RandomGrammar.java b/base/src/main/java/bjc/utils/gen/RandomGrammar.java new file mode 100644 index 0000000..3de08d6 --- /dev/null +++ b/base/src/main/java/bjc/utils/gen/RandomGrammar.java @@ -0,0 +1,69 @@ +package bjc.utils.gen; + +import bjc.utils.funcdata.FunctionalMap; +import bjc.utils.funcdata.IList; + +/** + * A weighted grammar where all the rules have a equal chance of occuring. + * + * @author ben + * + * @param <E> + * The type of grammar elements to use. + */ +public class RandomGrammar<E> extends WeightedGrammar<E> { + /** + * Create a new random grammar. + */ + public RandomGrammar() { + rules = new FunctionalMap<>(); + } + + /** + * Add cases to a specified rule. + * + * @param rule + * The name of the rule to add cases to. + * @param cases + * The cases to add for this rule. + */ + @SafeVarargs + public final void addCases(final E rule, final IList<E>... cases) { + for (final IList<E> currentCase : cases) { + super.addCase(rule, 1, currentCase); + } + } + + /** + * Create a rule with the specified name and cases. + * + * @param rule + * The name of the rule to add. + * @param cases + * The cases to add for this rule. + */ + @SafeVarargs + public final void makeRule(final E rule, final IList<E>... cases) { + super.addRule(rule); + + for (final IList<E> currentCase : cases) { + super.addCase(rule, 1, currentCase); + } + } + + /** + * Create a rule with the specified name and cases. + * + * @param rule + * The name of the rule to add. + * @param cases + * The cases to add for this rule. + */ + public void makeRule(final E rule, final IList<IList<E>> cases) { + if (cases == null) throw new NullPointerException("Cases must not be null"); + + super.addRule(rule); + + cases.forEach(currentCase -> super.addCase(rule, 1, currentCase)); + } +} |
