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 --- .../src/main/java/bjc/utils/gen/RandomGrammar.java | 18 ++--- .../main/java/bjc/utils/gen/WeightedGrammar.java | 89 ++++++++++++++-------- .../main/java/bjc/utils/gen/WeightedRandom.java | 11 +-- 3 files changed, 71 insertions(+), 47 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/gen') diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java b/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java index 4f194f3..349d9fa 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java @@ -1,8 +1,7 @@ package bjc.utils.gen; -import java.util.HashMap; - -import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.FunctionalMap; +import bjc.utils.funcdata.IFunctionalList; /** * A weighted grammar where all the rules have a equal chance of occuring. @@ -18,7 +17,7 @@ public class RandomGrammar extends WeightedGrammar { * Create a new random grammar. */ public RandomGrammar() { - rules = new HashMap<>(); + rules = new FunctionalMap<>(); } /** @@ -30,8 +29,8 @@ public class RandomGrammar extends WeightedGrammar { * The cases to add for this rule. */ @SafeVarargs - public final void addCases(E rule, FunctionalList... cases) { - for (FunctionalList currentCase : cases) { + public final void addCases(E rule, IFunctionalList... cases) { + for (IFunctionalList currentCase : cases) { super.addCase(rule, 1, currentCase); } } @@ -45,10 +44,10 @@ public class RandomGrammar extends WeightedGrammar { * The cases to add for this rule. */ @SafeVarargs - public final void makeRule(E rule, FunctionalList... cases) { + public final void makeRule(E rule, IFunctionalList... cases) { super.addRule(rule); - for (FunctionalList currentCase : cases) { + for (IFunctionalList currentCase : cases) { super.addCase(rule, 1, currentCase); } } @@ -61,7 +60,8 @@ public class RandomGrammar extends WeightedGrammar { * @param cases * The cases to add for this rule. */ - public void makeRule(E rule, FunctionalList> cases) { + public void makeRule(E rule, + IFunctionalList> cases) { if (cases == null) { throw new NullPointerException("Cases must not be null"); } 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) diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java index 5157ee2..10da34e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java @@ -2,9 +2,10 @@ package bjc.utils.gen; import java.util.Random; import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IFunctionalList; import bjc.utils.data.GenHolder; import bjc.utils.data.IHolder; -import bjc.utils.data.Pair; +import bjc.utils.data.IPair; /** * Represents a random number generator where certain results are weighted @@ -19,12 +20,12 @@ public class WeightedRandom { /** * The list of probabilities for each result */ - private FunctionalList probabilities; + private IFunctionalList probabilities; /** * The list of possible results to pick from */ - private FunctionalList results; + private IFunctionalList results; /** * The source for any needed random numbers @@ -101,7 +102,7 @@ public class WeightedRandom { * * @return A list of all the values that can be generated */ - public FunctionalList getResults() { + public IFunctionalList getResults() { return results; } @@ -111,7 +112,7 @@ public class WeightedRandom { * * @return A list of pairs of values and value probabilities */ - public FunctionalList> getValues() { + public IFunctionalList> getValues() { return probabilities.pairWith(results); } } \ No newline at end of file -- cgit v1.2.3