From 275a627719fc2231b16caea41130ff09f0f2b6a1 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Fri, 8 Apr 2016 13:28:09 -0400 Subject: Switch functional data to use interfaces --- .../main/java/bjc/utils/gen/WeightedGrammar.java | 89 ++++++++++++++-------- 1 file changed, 56 insertions(+), 33 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java index 5d61201..37f31b0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java @@ -1,13 +1,14 @@ package bjc.utils.gen; -import java.util.Hashtable; -import java.util.Map; import java.util.Random; -import java.util.Set; import java.util.function.Function; +import bjc.utils.data.IPair; import bjc.utils.data.Pair; import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.FunctionalMap; +import bjc.utils.funcdata.IFunctionalList; +import bjc.utils.funcdata.IFunctionalMap; /** * A random grammar, where certain rules will come up more often than @@ -22,29 +23,29 @@ public class WeightedGrammar { /** * The initial rule of the grammar */ - protected String initialRule; + protected String initialRule; /** * The rules currently in this grammar */ - protected Map>> rules; + protected IFunctionalMap>> rules; /** * The random number generator used for random numbers */ - private Random rng; + private Random rng; /** * All of the subgrammars of this grammar */ - protected Map> subgrammars; + protected IFunctionalMap> subgrammars; /** * Create a new weighted grammar. */ public WeightedGrammar() { - rules = new Hashtable<>(); - subgrammars = new Hashtable<>(); + rules = new FunctionalMap<>(); + subgrammars = new FunctionalMap<>(); } /** @@ -76,7 +77,7 @@ public class WeightedGrammar { * The case being added. */ public void addCase(E ruleName, int probability, - FunctionalList cse) { + IFunctionalList cse) { if (ruleName == null) { throw new NullPointerException("Rule name must be not null"); } else if (cse == null) { @@ -145,7 +146,7 @@ public class WeightedGrammar { * @return Whether or not the rule was succesfully added. */ public boolean addRule(E name, - WeightedRandom> cases) { + WeightedRandom> cases) { if (name == null) { throw new NullPointerException("Name must not be null"); } else if (cases == null) { @@ -193,15 +194,15 @@ public class WeightedGrammar { * The rule to test. * @return A set of sentances generated by the specified rule. */ - public FunctionalList> generateDebugValues( + public IFunctionalList> generateDebugValues( E ruleName) { if (ruleName == null) { throw new NullPointerException("Rule name must not be null"); } - FunctionalList> returnedList = new FunctionalList<>(); + IFunctionalList> returnedList = new FunctionalList<>(); - WeightedRandom> ruleGenerator = rules + WeightedRandom> ruleGenerator = rules .get(ruleName); for (int i = 0; i < 10; i++) { @@ -226,7 +227,7 @@ public class WeightedGrammar { * @return A randomly generated sentance from the specified initial * rule. */ - public FunctionalList generateGenericValues(E initRule, + public IFunctionalList generateGenericValues(E initRule, Function tokenTransformer, T spacer) { if (initRule == null) { throw new NullPointerException( @@ -237,7 +238,7 @@ public class WeightedGrammar { throw new NullPointerException("Spacer must not be null"); } - FunctionalList returnedList = new FunctionalList<>(); + IFunctionalList returnedList = new FunctionalList<>(); if (subgrammars.containsKey(initRule)) { subgrammars.get(initRule).generateGenericValues(initRule, @@ -280,7 +281,7 @@ public class WeightedGrammar { * @return A list of random grammar elements generated by the specified * rule. */ - public FunctionalList generateListValues(E initRule, E spacer) { + public IFunctionalList generateListValues(E initRule, E spacer) { return generateGenericValues(initRule, strang -> strang, spacer); } @@ -342,16 +343,23 @@ public class WeightedGrammar { "Number of times to prefix must be positive."); } - WeightedRandom> rule = rules.get(ruleName); + WeightedRandom> rule = rules.get(ruleName); - FunctionalList>> newResults = new FunctionalList<>(); + IFunctionalList>> newResults = new FunctionalList<>(); rule.getValues().forEach((pair) -> { - FunctionalList> newRule = new FunctionalList<>(); + IFunctionalList> newRule = new FunctionalList<>(); for (int i = 1; i <= numberOfTimes; i++) { - FunctionalList newCase = pair - .merge((left, right) -> right.clone()); + IFunctionalList newCase = pair.merge((left, right) -> { + IFunctionalList returnVal = new FunctionalList<>(); + + for (E val : right.toIterable()) { + returnVal.add(val); + } + + return returnVal; + }); for (int j = 1; j <= i; j++) { newCase.prepend(prefixToken); @@ -392,13 +400,21 @@ public class WeightedGrammar { "Prefix token must not be null"); } - WeightedRandom> rule = rules.get(ruleName); + WeightedRandom> rule = rules.get(ruleName); - FunctionalList>> newResults = new FunctionalList<>(); + IFunctionalList>> newResults = new FunctionalList<>(); rule.getValues().forEach((pair) -> { - FunctionalList newCase = pair - .merge((left, right) -> right.clone()); + IFunctionalList newCase = pair.merge((left, right) -> { + IFunctionalList returnVal = new FunctionalList<>(); + + for (E val : right.toIterable()) { + returnVal.add(val); + } + + return returnVal; + }); + newCase.prepend(prefixToken); newResults.add(new Pair<>(pair.merge((left, right) -> left) @@ -443,7 +459,7 @@ public class WeightedGrammar { * @return The number of rules in this grammar */ public int getRuleCount() { - return rules.size(); + return rules.getSize(); } /** @@ -451,8 +467,8 @@ public class WeightedGrammar { * * @return The set of all rule names in this grammar */ - public Set getRuleNames() { - return rules.keySet(); + public IFunctionalList getRuleNames() { + return rules.keyList(); } /** @@ -484,13 +500,20 @@ public class WeightedGrammar { "Prefix token must not be null"); } - WeightedRandom> rule = rules.get(ruleName); + WeightedRandom> rule = rules.get(ruleName); - FunctionalList>> newResults = new FunctionalList<>(); + IFunctionalList>> newResults = new FunctionalList<>(); rule.getValues().forEach((par) -> { - FunctionalList newCase = par - .merge((left, right) -> right.clone()); + IFunctionalList newCase = par.merge((left, right) -> { + IFunctionalList returnVal = new FunctionalList<>(); + + for (E val : right.toIterable()) { + returnVal.add(val); + } + + return returnVal; + }); newCase.add(suffixToken); newResults.add(new Pair<>(par.merge((left, right) -> left) -- cgit v1.2.3