From 4e4d470626087c26ee18f5cb04c86647b5f2699c Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Tue, 29 Sep 2015 13:35:50 -0400 Subject: Added random grammar support. Import of library code from version 1 finished. Now for more library code and examples on library usage. --- .../src/main/java/bjc/utils/gen/RandomGrammar.java | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java b/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java new file mode 100644 index 0000000..0245b17 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java @@ -0,0 +1,59 @@ +package bjc.utils.gen; + +import java.util.HashMap; + +import bjc.utils.funcdata.FunctionalList; + +/** + * 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 HashMap<>(); + } + + /** + * 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(E rule, FunctionalList... cases) { + for (FunctionalList cse : cases) { + super.addCase(rule, 1, cse); + } + } + + /** + * 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(E rule, FunctionalList... cases) { + super.addRule(rule); + + for (FunctionalList cse : cases) { + super.addCase(rule, 1, cse); + } + } + + /** + * 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(E rule, + FunctionalList> cases) { + super.addRule(rule); + + cases.forEach(cse -> super.addCase(rule, 1, cse)); + } +} -- cgit v1.2.3