diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-12-03 19:22:35 -0500 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-12-03 19:22:35 -0500 |
| commit | a2c7425458f645802a352abc4783e0afc73dba13 (patch) | |
| tree | 92fe887eb09674ddc61c251626989c06aff88a22 /base/src/main/java/bjc/utils/gen | |
| parent | bcda03dd2ba95a93a93df7f139b3607491750b74 (diff) | |
Adapt to esodata changes
Diffstat (limited to 'base/src/main/java/bjc/utils/gen')
| -rw-r--r-- | base/src/main/java/bjc/utils/gen/RandomGrammar.java | 12 | ||||
| -rw-r--r-- | base/src/main/java/bjc/utils/gen/WeightedGrammar.java | 74 | ||||
| -rw-r--r-- | base/src/main/java/bjc/utils/gen/WeightedRandom.java | 28 |
3 files changed, 57 insertions, 57 deletions
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<E> extends WeightedGrammar<E> { * The cases to add for this rule. */ @SafeVarargs - public final void addCases(final E rule, final IList<E>... cases) { - for (final IList<E> currentCase : cases) { + public final void addCases(final E rule, final ListEx<E>... cases) { + for (final ListEx<E> currentCase : cases) { super.addCase(rule, 1, currentCase); } } @@ -43,10 +43,10 @@ public class RandomGrammar<E> extends WeightedGrammar<E> { * The cases to add for this rule. */ @SafeVarargs - public final void makeRule(final E rule, final IList<E>... cases) { + public final void makeRule(final E rule, final ListEx<E>... cases) { super.addRule(rule); - for (final IList<E> currentCase : cases) { + for (final ListEx<E> currentCase : cases) { super.addCase(rule, 1, currentCase); } } @@ -60,7 +60,7 @@ public class RandomGrammar<E> extends WeightedGrammar<E> { * @param cases * The cases to add for this rule. */ - public void makeRule(final E rule, final IList<IList<E>> cases) { + public void makeRule(final E rule, final ListEx<ListEx<E>> 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<E> { protected String initialRule; /** The rules currently in this grammar */ - protected IMap<E, WeightedRandom<IList<E>>> rules; + protected MapEx<E, WeightedRandom<ListEx<E>>> rules; /** The random number generator used for random numbers */ private Random rng; /** All of the subgrammars of this grammar */ - protected IMap<E, WeightedGrammar<E>> subgrammars; + protected MapEx<E, WeightedGrammar<E>> subgrammars; /** Rules that require special handling */ - private IMap<E, Supplier<IList<E>>> specialRules; + private MapEx<E, Supplier<ListEx<E>>> specialRules; /** Predicate for marking special tokens */ private Predicate<E> specialMarker; /** Action for special tokens */ - private BiFunction<E, WeightedGrammar<E>, IList<E>> specialAction; + private BiFunction<E, WeightedGrammar<E>, ListEx<E>> specialAction; /** Create a new weighted grammar. */ public WeightedGrammar() { @@ -75,7 +75,7 @@ public class WeightedGrammar<E> { * The action to take on those tokens. */ public void configureSpecial(final Predicate<E> marker, - final BiFunction<E, WeightedGrammar<E>, IList<E>> action) { + final BiFunction<E, WeightedGrammar<E>, ListEx<E>> action) { specialMarker = marker; specialAction = action; } @@ -89,7 +89,7 @@ public class WeightedGrammar<E> { * @param cse * The case for the rule. */ - public void addSpecialRule(final E ruleName, final Supplier<IList<E>> cse) { + public void addSpecialRule(final E ruleName, final Supplier<ListEx<E>> cse) { if (ruleName == null) { throw new NullPointerException("Rule name must not be null"); } else if (cse == null) { @@ -111,7 +111,7 @@ public class WeightedGrammar<E> { * @param cse * The case being added. */ - public void addCase(final E ruleName, final int probability, final IList<E> cse) { + public void addCase(final E ruleName, final int probability, final ListEx<E> cse) { if (ruleName == null) { throw new NullPointerException("Rule name must be not null"); } else if (cse == null) { @@ -180,7 +180,7 @@ public class WeightedGrammar<E> { * * @return Whether or not the rule was succesfully added. */ - public boolean addRule(final E name, final WeightedRandom<IList<E>> cases) { + public boolean addRule(final E name, final WeightedRandom<ListEx<E>> cases) { if (name == null) { throw new NullPointerException("Name must not be null"); } else if (cases == null) { @@ -255,13 +255,13 @@ public class WeightedGrammar<E> { * * @return A set of sentences generated by the specified rule. */ - public IList<IList<E>> generateDebugValues(final E ruleName) { + public ListEx<ListEx<E>> generateDebugValues(final E ruleName) { if (ruleName == null) throw new NullPointerException("Rule name must not be null"); - final IList<IList<E>> returnedList = new FunctionalList<>(); + final ListEx<ListEx<E>> returnedList = new FunctionalList<>(); - final WeightedRandom<IList<E>> ruleGenerator = rules.get(ruleName).get(); + final WeightedRandom<ListEx<E>> ruleGenerator = rules.get(ruleName).get(); for (int i = 0; i < 10; i++) { returnedList.add(ruleGenerator.generateValue()); @@ -288,7 +288,7 @@ public class WeightedGrammar<E> { * * @return A randomly generated sentence from the specified initial rule. */ - public <T> IList<T> generateGenericValues(final E initRules, + public <T> ListEx<T> generateGenericValues(final E initRules, final Function<E, T> tokenTransformer, final T spacer) { if (initRules == null) { throw new NullPointerException("Initial rule must not be null"); @@ -298,9 +298,9 @@ public class WeightedGrammar<E> { throw new NullPointerException("Spacer must not be null"); } - final IList<T> returnedList = new FunctionalList<>(); + final ListEx<T> returnedList = new FunctionalList<>(); - IList<E> genRules = new FunctionalList<>(initRules); + ListEx<E> genRules = new FunctionalList<>(initRules); if (specialMarker != null) { if (specialMarker.test(initRules)) { @@ -371,8 +371,8 @@ public class WeightedGrammar<E> { * * @return A list of random grammar elements generated by the specified rule. */ - public IList<E> generateListValues(final E initRule, final E spacer) { - final IList<E> retList + public ListEx<E> generateListValues(final E initRule, final E spacer) { + final ListEx<E> retList = generateGenericValues(initRule, strang -> strang, spacer); return retList; @@ -401,8 +401,8 @@ public class WeightedGrammar<E> { * * @return The set of all rule names in this grammar. */ - public IList<E> getRuleNames() { - final IList<E> ruleNames = new FunctionalList<>(); + public ListEx<E> getRuleNames() { + final ListEx<E> ruleNames = new FunctionalList<>(); ruleNames.addAll(rules.keyList()); ruleNames.addAll(specialRules.keyList()); @@ -471,19 +471,19 @@ public class WeightedGrammar<E> { throw new IllegalArgumentException( "Number of times to prefix must be positive."); - final WeightedRandom<IList<E>> rule = rules.get(ruleName).get(); + final WeightedRandom<ListEx<E>> rule = rules.get(ruleName).get(); - final IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>(); + final ListEx<Pair<Integer, ListEx<E>>> newResults = new FunctionalList<>(); /* * @NOTE Can this be simplified? */ rule.getValues().forEach(pair -> { - final IList<IList<E>> newRule = new FunctionalList<>(); + final ListEx<ListEx<E>> newRule = new FunctionalList<>(); for (int i = 1; i <= numberOfTimes; i++) { - final IList<E> newCase = pair.merge((left, right) -> { - final IList<E> returnVal = new FunctionalList<>(); + final ListEx<E> newCase = pair.merge((left, right) -> { + final ListEx<E> returnVal = new FunctionalList<>(); for (final E val : right.toIterable()) { returnVal.add(val); @@ -502,7 +502,7 @@ public class WeightedGrammar<E> { 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<E> { throw new NullPointerException("Prefix token must not be null"); } - final WeightedRandom<IList<E>> rule = rules.get(ruleName).get(); + final WeightedRandom<ListEx<E>> rule = rules.get(ruleName).get(); - final IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>(); + final ListEx<Pair<Integer, ListEx<E>>> newResults = new FunctionalList<>(); rule.getValues().forEach(pair -> { - final IList<E> newCase = pair.merge((left, right) -> { - final IList<E> returnVal = new FunctionalList<>(); + final ListEx<E> newCase = pair.merge((left, right) -> { + final ListEx<E> returnVal = new FunctionalList<>(); for (final E val : right.toIterable()) { returnVal.add(val); @@ -551,7 +551,7 @@ public class WeightedGrammar<E> { 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<E> { throw new NullPointerException("Prefix token must not be null"); } - final WeightedRandom<IList<E>> rule = rules.get(ruleName).get(); + final WeightedRandom<ListEx<E>> rule = rules.get(ruleName).get(); - final IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>(); + final ListEx<Pair<Integer, ListEx<E>>> newResults = new FunctionalList<>(); rule.getValues().forEach(par -> { - final IList<E> newCase = par.merge((left, right) -> { - final IList<E> returnVal = new FunctionalList<>(); + final ListEx<E> newCase = par.merge((left, right) -> { + final ListEx<E> returnVal = new FunctionalList<>(); for (final E val : right.toIterable()) { returnVal.add(val); @@ -606,7 +606,7 @@ public class WeightedGrammar<E> { 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<E> { - private final IList<IPair<Integer, E>> values; + private final ListEx<Pair<Integer, E>> values; /* The source for any needed random numbers */ private Random source; @@ -50,7 +50,7 @@ public class WeightedRandom<E> { this(BASE); } - private WeightedRandom(Random src, IList<IPair<Integer, E>> vals, int chance) { + private WeightedRandom(Random src, ListEx<Pair<Integer, E>> vals, int chance) { source = src; values = vals; @@ -68,7 +68,7 @@ public class WeightedRandom<E> { * 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<E> { */ public E generateValue(Random rn) { int target = rn.nextInt(totalChance); - for (IPair<Integer, E> val : values) { + for (Pair<Integer, E> val : values) { int prob = val.getLeft(); if (target < prob) { @@ -114,8 +114,8 @@ public class WeightedRandom<E> { * * @return A list of all the values that can be generated */ - public IList<E> getResults() { - return values.map(IPair::getRight); + public ListEx<E> getResults() { + return values.map(Pair::getRight); } /** @@ -124,7 +124,7 @@ public class WeightedRandom<E> { * * @return A list of pairs of values and value probabilities */ - public IList<IPair<Integer, E>> getValues() { + public ListEx<Pair<Integer, E>> getValues() { return values; } @@ -157,7 +157,7 @@ public class WeightedRandom<E> { public E getDescent(int factor, Random rn) { if (values.getSize() == 0) return null; - for (IPair<Integer, E> val : values) { + for (Pair<Integer, E> val : values) { if (rn.nextInt(factor) == 0) continue; if (exhaust) { @@ -169,7 +169,7 @@ public class WeightedRandom<E> { return val.getRight(); } - IPair<Integer, E> val = values.getByIndex(values.getSize() - 1); + Pair<Integer, E> val = values.getByIndex(values.getSize() - 1); if (exhaust) values.removeMatching(val); return val.getRight(); @@ -231,7 +231,7 @@ public class WeightedRandom<E> { // System.err.printf("\tTRACE: got %d success for binomial trials (%d <= 1d%d, // %d times)\n", numSuc, target, bound, trials); - IPair<Integer, E> val = values.getByIndex(Math.min(numSuc, values.getSize() - 1)); + Pair<Integer, E> val = values.getByIndex(Math.min(numSuc, values.getSize() - 1)); if (exhaust) { totalChance -= val.getLeft(); @@ -248,8 +248,8 @@ public class WeightedRandom<E> { * @return A new WeightedRandom that is exhaustible. */ public WeightedRandom<E> exhaustible() { - IList<IPair<Integer, E>> lst = new FunctionalList<>(); - for (IPair<Integer, E> val : values) lst.add(val); + ListEx<Pair<Integer, E>> lst = new FunctionalList<>(); + for (Pair<Integer, E> val : values) lst.add(val); WeightedRandom<E> res = new WeightedRandom<>(source, lst, totalChance); |
