summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2015-09-29 13:35:50 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2015-09-29 13:35:50 -0400
commit4e4d470626087c26ee18f5cb04c86647b5f2699c (patch)
tree869a5c7452c50b1e751d86e7058d090f3d55f69b /BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java
parent47577a40bc0e298041ab0915fa4bdc3355d53349 (diff)
Added random grammar support.
Import of library code from version 1 finished. Now for more library code and examples on library usage.
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java59
1 files changed, 59 insertions, 0 deletions
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 <E> The type of grammar elements to use.
+ */
+public class RandomGrammar<E> extends WeightedGrammar<E> {
+
+ /**
+ * 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<E>... cases) {
+ for (FunctionalList<E> 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<E>... cases) {
+ super.addRule(rule);
+
+ for (FunctionalList<E> 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<FunctionalList<E>> cases) {
+ super.addRule(rule);
+
+ cases.forEach(cse -> super.addCase(rule, 1, cse));
+ }
+}