From a2c7425458f645802a352abc4783e0afc73dba13 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Thu, 3 Dec 2020 19:22:35 -0500 Subject: Adapt to esodata changes --- .../src/main/java/bjc/utils/gen/RandomGrammar.java | 12 ++-- .../main/java/bjc/utils/gen/WeightedGrammar.java | 74 +++++++++++----------- .../main/java/bjc/utils/gen/WeightedRandom.java | 28 ++++---- 3 files changed, 57 insertions(+), 57 deletions(-) (limited to 'base/src/main/java/bjc/utils/gen') diff --git a/base/src/main/java/bjc/utils/gen/RandomGrammar.java b/base/src/main/java/bjc/utils/gen/RandomGrammar.java index 050165b..620cc5b 100644 --- a/base/src/main/java/bjc/utils/gen/RandomGrammar.java +++ b/base/src/main/java/bjc/utils/gen/RandomGrammar.java @@ -1,7 +1,7 @@ package bjc.utils.gen; import bjc.funcdata.FunctionalMap; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; /** * A weighted grammar where all the rules have a equal chance of occuring. @@ -27,8 +27,8 @@ public class RandomGrammar extends WeightedGrammar { * The cases to add for this rule. */ @SafeVarargs - public final void addCases(final E rule, final IList... cases) { - for (final IList currentCase : cases) { + public final void addCases(final E rule, final ListEx... cases) { + for (final ListEx currentCase : cases) { super.addCase(rule, 1, currentCase); } } @@ -43,10 +43,10 @@ public class RandomGrammar extends WeightedGrammar { * The cases to add for this rule. */ @SafeVarargs - public final void makeRule(final E rule, final IList... cases) { + public final void makeRule(final E rule, final ListEx... cases) { super.addRule(rule); - for (final IList currentCase : cases) { + for (final ListEx currentCase : cases) { super.addCase(rule, 1, currentCase); } } @@ -60,7 +60,7 @@ public class RandomGrammar extends WeightedGrammar { * @param cases * The cases to add for this rule. */ - public void makeRule(final E rule, final IList> cases) { + public void makeRule(final E rule, final ListEx> cases) { if (cases == null) throw new NullPointerException("Cases must not be null"); diff --git a/base/src/main/java/bjc/utils/gen/WeightedGrammar.java b/base/src/main/java/bjc/utils/gen/WeightedGrammar.java index 540efbc..48cc658 100644 --- a/base/src/main/java/bjc/utils/gen/WeightedGrammar.java +++ b/base/src/main/java/bjc/utils/gen/WeightedGrammar.java @@ -6,12 +6,12 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; -import bjc.data.IPair; import bjc.data.Pair; +import bjc.data.SimplePair; import bjc.funcdata.FunctionalList; import bjc.funcdata.FunctionalMap; -import bjc.funcdata.IList; -import bjc.funcdata.IMap; +import bjc.funcdata.ListEx; +import bjc.funcdata.MapEx; /** * A random grammar, where certain rules will come up more often than others. @@ -26,22 +26,22 @@ public class WeightedGrammar { protected String initialRule; /** The rules currently in this grammar */ - protected IMap>> rules; + protected MapEx>> rules; /** The random number generator used for random numbers */ private Random rng; /** All of the subgrammars of this grammar */ - protected IMap> subgrammars; + protected MapEx> subgrammars; /** Rules that require special handling */ - private IMap>> specialRules; + private MapEx>> specialRules; /** Predicate for marking special tokens */ private Predicate specialMarker; /** Action for special tokens */ - private BiFunction, IList> specialAction; + private BiFunction, ListEx> specialAction; /** Create a new weighted grammar. */ public WeightedGrammar() { @@ -75,7 +75,7 @@ public class WeightedGrammar { * The action to take on those tokens. */ public void configureSpecial(final Predicate marker, - final BiFunction, IList> action) { + final BiFunction, ListEx> action) { specialMarker = marker; specialAction = action; } @@ -89,7 +89,7 @@ public class WeightedGrammar { * @param cse * The case for the rule. */ - public void addSpecialRule(final E ruleName, final Supplier> cse) { + public void addSpecialRule(final E ruleName, final Supplier> cse) { if (ruleName == null) { throw new NullPointerException("Rule name must not be null"); } else if (cse == null) { @@ -111,7 +111,7 @@ public class WeightedGrammar { * @param cse * The case being added. */ - public void addCase(final E ruleName, final int probability, final IList cse) { + public void addCase(final E ruleName, final int probability, final ListEx cse) { if (ruleName == null) { throw new NullPointerException("Rule name must be not null"); } else if (cse == null) { @@ -180,7 +180,7 @@ public class WeightedGrammar { * * @return Whether or not the rule was succesfully added. */ - public boolean addRule(final E name, final WeightedRandom> cases) { + public boolean addRule(final E name, final WeightedRandom> cases) { if (name == null) { throw new NullPointerException("Name must not be null"); } else if (cases == null) { @@ -255,13 +255,13 @@ public class WeightedGrammar { * * @return A set of sentences generated by the specified rule. */ - public IList> generateDebugValues(final E ruleName) { + public ListEx> generateDebugValues(final E ruleName) { if (ruleName == null) throw new NullPointerException("Rule name must not be null"); - final IList> returnedList = new FunctionalList<>(); + final ListEx> returnedList = new FunctionalList<>(); - final WeightedRandom> ruleGenerator = rules.get(ruleName).get(); + final WeightedRandom> ruleGenerator = rules.get(ruleName).get(); for (int i = 0; i < 10; i++) { returnedList.add(ruleGenerator.generateValue()); @@ -288,7 +288,7 @@ public class WeightedGrammar { * * @return A randomly generated sentence from the specified initial rule. */ - public IList generateGenericValues(final E initRules, + public ListEx generateGenericValues(final E initRules, final Function tokenTransformer, final T spacer) { if (initRules == null) { throw new NullPointerException("Initial rule must not be null"); @@ -298,9 +298,9 @@ public class WeightedGrammar { throw new NullPointerException("Spacer must not be null"); } - final IList returnedList = new FunctionalList<>(); + final ListEx returnedList = new FunctionalList<>(); - IList genRules = new FunctionalList<>(initRules); + ListEx genRules = new FunctionalList<>(initRules); if (specialMarker != null) { if (specialMarker.test(initRules)) { @@ -371,8 +371,8 @@ public class WeightedGrammar { * * @return A list of random grammar elements generated by the specified rule. */ - public IList generateListValues(final E initRule, final E spacer) { - final IList retList + public ListEx generateListValues(final E initRule, final E spacer) { + final ListEx retList = generateGenericValues(initRule, strang -> strang, spacer); return retList; @@ -401,8 +401,8 @@ public class WeightedGrammar { * * @return The set of all rule names in this grammar. */ - public IList getRuleNames() { - final IList ruleNames = new FunctionalList<>(); + public ListEx getRuleNames() { + final ListEx ruleNames = new FunctionalList<>(); ruleNames.addAll(rules.keyList()); ruleNames.addAll(specialRules.keyList()); @@ -471,19 +471,19 @@ public class WeightedGrammar { throw new IllegalArgumentException( "Number of times to prefix must be positive."); - final WeightedRandom> rule = rules.get(ruleName).get(); + final WeightedRandom> rule = rules.get(ruleName).get(); - final IList>> newResults = new FunctionalList<>(); + final ListEx>> newResults = new FunctionalList<>(); /* * @NOTE Can this be simplified? */ rule.getValues().forEach(pair -> { - final IList> newRule = new FunctionalList<>(); + final ListEx> newRule = new FunctionalList<>(); for (int i = 1; i <= numberOfTimes; i++) { - final IList newCase = pair.merge((left, right) -> { - final IList returnVal = new FunctionalList<>(); + final ListEx newCase = pair.merge((left, right) -> { + final ListEx returnVal = new FunctionalList<>(); for (final E val : right.toIterable()) { returnVal.add(val); @@ -502,7 +502,7 @@ public class WeightedGrammar { newRule.forEach((list) -> { final Integer currentProb = pair.merge((left, right) -> left); - newResults.add(new Pair<>(currentProb + additionalProbability, list)); + newResults.add(new SimplePair<>(currentProb + additionalProbability, list)); }); }); @@ -534,13 +534,13 @@ public class WeightedGrammar { throw new NullPointerException("Prefix token must not be null"); } - final WeightedRandom> rule = rules.get(ruleName).get(); + final WeightedRandom> rule = rules.get(ruleName).get(); - final IList>> newResults = new FunctionalList<>(); + final ListEx>> newResults = new FunctionalList<>(); rule.getValues().forEach(pair -> { - final IList newCase = pair.merge((left, right) -> { - final IList returnVal = new FunctionalList<>(); + final ListEx newCase = pair.merge((left, right) -> { + final ListEx returnVal = new FunctionalList<>(); for (final E val : right.toIterable()) { returnVal.add(val); @@ -551,7 +551,7 @@ public class WeightedGrammar { newCase.prepend(prefixToken); - newResults.add(new Pair<>( + newResults.add(new SimplePair<>( pair.merge((left, right) -> left) + additionalProbability, newCase)); }); @@ -589,13 +589,13 @@ public class WeightedGrammar { throw new NullPointerException("Prefix token must not be null"); } - final WeightedRandom> rule = rules.get(ruleName).get(); + final WeightedRandom> rule = rules.get(ruleName).get(); - final IList>> newResults = new FunctionalList<>(); + final ListEx>> newResults = new FunctionalList<>(); rule.getValues().forEach(par -> { - final IList newCase = par.merge((left, right) -> { - final IList returnVal = new FunctionalList<>(); + final ListEx newCase = par.merge((left, right) -> { + final ListEx returnVal = new FunctionalList<>(); for (final E val : right.toIterable()) { returnVal.add(val); @@ -606,7 +606,7 @@ public class WeightedGrammar { newCase.add(suffixToken); - newResults.add(new Pair<>( + newResults.add(new SimplePair<>( par.merge((left, right) -> left) + additionalProbability, newCase)); }); diff --git a/base/src/main/java/bjc/utils/gen/WeightedRandom.java b/base/src/main/java/bjc/utils/gen/WeightedRandom.java index 969020e..01a961f 100644 --- a/base/src/main/java/bjc/utils/gen/WeightedRandom.java +++ b/base/src/main/java/bjc/utils/gen/WeightedRandom.java @@ -2,10 +2,10 @@ package bjc.utils.gen; import java.util.Random; -import bjc.data.IPair; import bjc.data.Pair; +import bjc.data.SimplePair; import bjc.funcdata.FunctionalList; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; /** * Represents a random number generator where certain results are weighted more @@ -17,7 +17,7 @@ import bjc.funcdata.IList; * The type of values that are randomly selected. */ public class WeightedRandom { - private final IList> values; + private final ListEx> values; /* The source for any needed random numbers */ private Random source; @@ -50,7 +50,7 @@ public class WeightedRandom { this(BASE); } - private WeightedRandom(Random src, IList> vals, int chance) { + private WeightedRandom(Random src, ListEx> vals, int chance) { source = src; values = vals; @@ -68,7 +68,7 @@ public class WeightedRandom { * The result to get when the chance comes up. */ public void addProbability(final int chance, final E result) { - values.add(new Pair<>(chance, result)); + values.add(new SimplePair<>(chance, result)); totalChance += chance; } @@ -91,7 +91,7 @@ public class WeightedRandom { */ public E generateValue(Random rn) { int target = rn.nextInt(totalChance); - for (IPair val : values) { + for (Pair val : values) { int prob = val.getLeft(); if (target < prob) { @@ -114,8 +114,8 @@ public class WeightedRandom { * * @return A list of all the values that can be generated */ - public IList getResults() { - return values.map(IPair::getRight); + public ListEx getResults() { + return values.map(Pair::getRight); } /** @@ -124,7 +124,7 @@ public class WeightedRandom { * * @return A list of pairs of values and value probabilities */ - public IList> getValues() { + public ListEx> getValues() { return values; } @@ -157,7 +157,7 @@ public class WeightedRandom { public E getDescent(int factor, Random rn) { if (values.getSize() == 0) return null; - for (IPair val : values) { + for (Pair val : values) { if (rn.nextInt(factor) == 0) continue; if (exhaust) { @@ -169,7 +169,7 @@ public class WeightedRandom { return val.getRight(); } - IPair val = values.getByIndex(values.getSize() - 1); + Pair val = values.getByIndex(values.getSize() - 1); if (exhaust) values.removeMatching(val); return val.getRight(); @@ -231,7 +231,7 @@ public class WeightedRandom { // System.err.printf("\tTRACE: got %d success for binomial trials (%d <= 1d%d, // %d times)\n", numSuc, target, bound, trials); - IPair val = values.getByIndex(Math.min(numSuc, values.getSize() - 1)); + Pair val = values.getByIndex(Math.min(numSuc, values.getSize() - 1)); if (exhaust) { totalChance -= val.getLeft(); @@ -248,8 +248,8 @@ public class WeightedRandom { * @return A new WeightedRandom that is exhaustible. */ public WeightedRandom exhaustible() { - IList> lst = new FunctionalList<>(); - for (IPair val : values) lst.add(val); + ListEx> lst = new FunctionalList<>(); + for (Pair val : values) lst.add(val); WeightedRandom res = new WeightedRandom<>(source, lst, totalChance); -- cgit v1.2.3