diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java | 89 |
1 files changed, 56 insertions, 33 deletions
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<E> { /** * The initial rule of the grammar */ - protected String initialRule; + protected String initialRule; /** * The rules currently in this grammar */ - protected Map<E, WeightedRandom<FunctionalList<E>>> rules; + protected IFunctionalMap<E, WeightedRandom<IFunctionalList<E>>> rules; /** * The random number generator used for random numbers */ - private Random rng; + private Random rng; /** * All of the subgrammars of this grammar */ - protected Map<E, WeightedGrammar<E>> subgrammars; + protected IFunctionalMap<E, WeightedGrammar<E>> 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<E> { * The case being added. */ public void addCase(E ruleName, int probability, - FunctionalList<E> cse) { + IFunctionalList<E> cse) { if (ruleName == null) { throw new NullPointerException("Rule name must be not null"); } else if (cse == null) { @@ -145,7 +146,7 @@ public class WeightedGrammar<E> { * @return Whether or not the rule was succesfully added. */ public boolean addRule(E name, - WeightedRandom<FunctionalList<E>> cases) { + WeightedRandom<IFunctionalList<E>> cases) { if (name == null) { throw new NullPointerException("Name must not be null"); } else if (cases == null) { @@ -193,15 +194,15 @@ public class WeightedGrammar<E> { * The rule to test. * @return A set of sentances generated by the specified rule. */ - public FunctionalList<FunctionalList<E>> generateDebugValues( + public IFunctionalList<IFunctionalList<E>> generateDebugValues( E ruleName) { if (ruleName == null) { throw new NullPointerException("Rule name must not be null"); } - FunctionalList<FunctionalList<E>> returnedList = new FunctionalList<>(); + IFunctionalList<IFunctionalList<E>> returnedList = new FunctionalList<>(); - WeightedRandom<FunctionalList<E>> ruleGenerator = rules + WeightedRandom<IFunctionalList<E>> ruleGenerator = rules .get(ruleName); for (int i = 0; i < 10; i++) { @@ -226,7 +227,7 @@ public class WeightedGrammar<E> { * @return A randomly generated sentance from the specified initial * rule. */ - public <T> FunctionalList<T> generateGenericValues(E initRule, + public <T> IFunctionalList<T> generateGenericValues(E initRule, Function<E, T> tokenTransformer, T spacer) { if (initRule == null) { throw new NullPointerException( @@ -237,7 +238,7 @@ public class WeightedGrammar<E> { throw new NullPointerException("Spacer must not be null"); } - FunctionalList<T> returnedList = new FunctionalList<>(); + IFunctionalList<T> returnedList = new FunctionalList<>(); if (subgrammars.containsKey(initRule)) { subgrammars.get(initRule).generateGenericValues(initRule, @@ -280,7 +281,7 @@ public class WeightedGrammar<E> { * @return A list of random grammar elements generated by the specified * rule. */ - public FunctionalList<E> generateListValues(E initRule, E spacer) { + public IFunctionalList<E> generateListValues(E initRule, E spacer) { return generateGenericValues(initRule, strang -> strang, spacer); } @@ -342,16 +343,23 @@ public class WeightedGrammar<E> { "Number of times to prefix must be positive."); } - WeightedRandom<FunctionalList<E>> rule = rules.get(ruleName); + WeightedRandom<IFunctionalList<E>> rule = rules.get(ruleName); - FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = new FunctionalList<>(); + IFunctionalList<IPair<Integer, IFunctionalList<E>>> newResults = new FunctionalList<>(); rule.getValues().forEach((pair) -> { - FunctionalList<FunctionalList<E>> newRule = new FunctionalList<>(); + IFunctionalList<IFunctionalList<E>> newRule = new FunctionalList<>(); for (int i = 1; i <= numberOfTimes; i++) { - FunctionalList<E> newCase = pair - .merge((left, right) -> right.clone()); + IFunctionalList<E> newCase = pair.merge((left, right) -> { + IFunctionalList<E> 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<E> { "Prefix token must not be null"); } - WeightedRandom<FunctionalList<E>> rule = rules.get(ruleName); + WeightedRandom<IFunctionalList<E>> rule = rules.get(ruleName); - FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = new FunctionalList<>(); + IFunctionalList<IPair<Integer, IFunctionalList<E>>> newResults = new FunctionalList<>(); rule.getValues().forEach((pair) -> { - FunctionalList<E> newCase = pair - .merge((left, right) -> right.clone()); + IFunctionalList<E> newCase = pair.merge((left, right) -> { + IFunctionalList<E> 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<E> { * @return The number of rules in this grammar */ public int getRuleCount() { - return rules.size(); + return rules.getSize(); } /** @@ -451,8 +467,8 @@ public class WeightedGrammar<E> { * * @return The set of all rule names in this grammar */ - public Set<E> getRuleNames() { - return rules.keySet(); + public IFunctionalList<E> getRuleNames() { + return rules.keyList(); } /** @@ -484,13 +500,20 @@ public class WeightedGrammar<E> { "Prefix token must not be null"); } - WeightedRandom<FunctionalList<E>> rule = rules.get(ruleName); + WeightedRandom<IFunctionalList<E>> rule = rules.get(ruleName); - FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = new FunctionalList<>(); + IFunctionalList<IPair<Integer, IFunctionalList<E>>> newResults = new FunctionalList<>(); rule.getValues().forEach((par) -> { - FunctionalList<E> newCase = par - .merge((left, right) -> right.clone()); + IFunctionalList<E> newCase = par.merge((left, right) -> { + IFunctionalList<E> 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) |
