From c82e3b3b2de0633317ec8fc85925e91422820597 Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Sun, 8 Oct 2017 22:39:59 -0300 Subject: Start splitting into maven modules --- .../src/main/java/bjc/utils/gen/RandomGrammar.java | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 base/src/main/java/bjc/utils/gen/RandomGrammar.java (limited to 'base/src/main/java/bjc/utils/gen/RandomGrammar.java') 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 + * The type of grammar elements to use. + */ +public class RandomGrammar extends WeightedGrammar { + /** + * 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... cases) { + for (final IList 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... cases) { + super.addRule(rule); + + for (final IList 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> cases) { + if (cases == null) throw new NullPointerException("Cases must not be null"); + + super.addRule(rule); + + cases.forEach(currentCase -> super.addCase(rule, 1, currentCase)); + } +} -- cgit v1.2.3