diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/gen')
3 files changed, 89 insertions, 86 deletions
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 4153e7d..3de08d6 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java @@ -28,8 +28,8 @@ public class RandomGrammar<E> extends WeightedGrammar<E> { * The cases to add for this rule. */ @SafeVarargs - public final void addCases(E rule, IList<E>... cases) { - for(IList<E> currentCase : cases) { + public final void addCases(final E rule, final IList<E>... cases) { + for (final IList<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(E rule, IList<E>... cases) { + public final void makeRule(final E rule, final IList<E>... cases) { super.addRule(rule); - for(IList<E> currentCase : cases) { + for (final IList<E> currentCase : cases) { super.addCase(rule, 1, currentCase); } } @@ -59,8 +59,8 @@ public class RandomGrammar<E> extends WeightedGrammar<E> { * @param cases * The cases to add for this rule. */ - public void makeRule(E rule, IList<IList<E>> cases) { - if(cases == null) throw new NullPointerException("Cases must not be null"); + public void makeRule(final E rule, final IList<IList<E>> cases) { + if (cases == null) throw new NullPointerException("Cases must not be null"); super.addRule(rule); 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 66ee993..7777ad8 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java @@ -1,5 +1,11 @@ package bjc.utils.gen; +import java.util.Random; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.Supplier; + import bjc.utils.data.IPair; import bjc.utils.data.Pair; import bjc.utils.funcdata.FunctionalList; @@ -7,12 +13,6 @@ import bjc.utils.funcdata.FunctionalMap; import bjc.utils.funcdata.IList; import bjc.utils.funcdata.IMap; -import java.util.Random; -import java.util.function.BiFunction; -import java.util.function.Function; -import java.util.function.Predicate; -import java.util.function.Supplier; - /** * A random grammar, where certain rules will come up more often than others. * @@ -74,7 +74,7 @@ public class WeightedGrammar<E> { * @param source * The source of randomness to use */ - public WeightedGrammar(Random source) { + public WeightedGrammar(final Random source) { this(); if (source == null) throw new NullPointerException("Source of randomness must be non-null"); @@ -84,28 +84,29 @@ public class WeightedGrammar<E> { /** * Configure the action to perform on special tokens. - * + * * @param marker * The marker to find special tokens. - * + * * @param action * The action to take on those tokens. */ - public void configureSpecial(Predicate<E> marker, BiFunction<E, WeightedGrammar<E>, IList<E>> action) { + public void configureSpecial(final Predicate<E> marker, + final BiFunction<E, WeightedGrammar<E>, IList<E>> action) { specialMarker = marker; specialAction = action; } /** * Adds a special rule to the grammar. - * + * * @param ruleName * The name of the special rule. - * + * * @param cse * The case for the rule. */ - public void addSpecialRule(E ruleName, Supplier<IList<E>> cse) { + public void addSpecialRule(final E ruleName, final Supplier<IList<E>> cse) { if (ruleName == null) throw new NullPointerException("Rule name must not be null"); else if (cse == null) throw new NullPointerException("Case must not be null"); @@ -123,7 +124,7 @@ public class WeightedGrammar<E> { * @param cse * The case being added. */ - public void addCase(E ruleName, int probability, IList<E> cse) { + public void addCase(final E ruleName, final int probability, final IList<E> cse) { if (ruleName == null) throw new NullPointerException("Rule name must be not null"); else if (cse == null) throw new NullPointerException("Case body must not be null"); @@ -140,7 +141,7 @@ public class WeightedGrammar<E> { * The alias of the subgrammar * @return Whether the alias was succesfully created */ - public boolean addGrammarAlias(E name, E alias) { + public boolean addGrammarAlias(final E name, final E alias) { if (name == null) throw new NullPointerException("Subgrammar name must not be null"); else if (alias == null) throw new NullPointerException("Subgrammar alias must not be null"); @@ -162,7 +163,7 @@ public class WeightedGrammar<E> { * The name of the rule to add. * @return Whether or not the rule was successfully added. */ - public boolean addRule(E name) { + public boolean addRule(final E name) { if (rng == null) { rng = new Random(); } @@ -181,7 +182,7 @@ public class WeightedGrammar<E> { * The set of cases for the rule. * @return Whether or not the rule was succesfully added. */ - public boolean addRule(E name, WeightedRandom<IList<E>> cases) { + public boolean addRule(final E name, final WeightedRandom<IList<E>> cases) { if (name == null) throw new NullPointerException("Name must not be null"); else if (cases == null) throw new NullPointerException("Cases must not be null"); @@ -201,7 +202,7 @@ public class WeightedGrammar<E> { * The subgrammar to add. * @return Whether or not the subgrammar was succesfully added. */ - public boolean addSubgrammar(E name, WeightedGrammar<E> subgrammar) { + public boolean addSubgrammar(final E name, final WeightedGrammar<E> subgrammar) { if (name == null) throw new NullPointerException("Subgrammar name must not be null"); else if (subgrammar == null) throw new NullPointerException("Subgrammar must not be null"); @@ -218,7 +219,7 @@ public class WeightedGrammar<E> { * @param name * The name of the rule to remove. */ - public void deleteRule(E name) { + public void deleteRule(final E name) { if (name == null) throw new NullPointerException("Rule name must not be null"); rules.remove(name); @@ -230,7 +231,7 @@ public class WeightedGrammar<E> { * @param name * The name of the subgrammar to remove. */ - public void deleteSubgrammar(E name) { + public void deleteSubgrammar(final E name) { if (name == null) throw new NullPointerException("Rule name must not be null"); subgrammars.remove(name); @@ -245,12 +246,12 @@ public class WeightedGrammar<E> { * The rule to test. * @return A set of sentences generated by the specified rule. */ - public IList<IList<E>> generateDebugValues(E ruleName) { + public IList<IList<E>> generateDebugValues(final E ruleName) { if (ruleName == null) throw new NullPointerException("Rule name must not be null"); - IList<IList<E>> returnedList = new FunctionalList<>(); + final IList<IList<E>> returnedList = new FunctionalList<>(); - WeightedRandom<IList<E>> ruleGenerator = rules.get(ruleName); + final WeightedRandom<IList<E>> ruleGenerator = rules.get(ruleName); for (int i = 0; i < 10; i++) { returnedList.add(ruleGenerator.generateValue()); @@ -267,25 +268,26 @@ public class WeightedGrammar<E> { * * @param initRules * The initial rule to start with. - * + * * @param tokenTransformer * The function to transform grammar output into * something. - * + * * @param spacer * The spacer element to add in between output tokens. - * + * * @return A randomly generated sentence from the specified initial * rule. */ - public <T> IList<T> generateGenericValues(E initRules, Function<E, T> tokenTransformer, T spacer) { + public <T> IList<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"); else if (tokenTransformer == null) throw new NullPointerException("Transformer must not be null"); else if (spacer == null) throw new NullPointerException("Spacer must not be null"); - IList<T> returnedList = new FunctionalList<>(); + final IList<T> returnedList = new FunctionalList<>(); IList<E> genRules = new FunctionalList<>(initRules); @@ -295,39 +297,39 @@ public class WeightedGrammar<E> { } } - for (E initRule : genRules.toIterable()) { + for (final E initRule : genRules.toIterable()) { if (specialRules.containsKey(initRule)) { - for (E rulePart : specialRules.get(initRule).get().toIterable()) { - Iterable<T> generatedRuleParts = generateGenericValues(rulePart, + for (final E rulePart : specialRules.get(initRule).get().toIterable()) { + final Iterable<T> generatedRuleParts = generateGenericValues(rulePart, tokenTransformer, spacer).toIterable(); - for (T generatedRulePart : generatedRuleParts) { + for (final T generatedRulePart : generatedRuleParts) { returnedList.add(generatedRulePart); returnedList.add(spacer); } } } else if (subgrammars.containsKey(initRule)) { - Iterable<T> ruleParts = subgrammars.get(initRule) + final Iterable<T> ruleParts = subgrammars.get(initRule) .generateGenericValues(initRule, tokenTransformer, spacer).toIterable(); - for (T rulePart : ruleParts) { + for (final T rulePart : ruleParts) { returnedList.add(rulePart); returnedList.add(spacer); } } else if (rules.containsKey(initRule)) { - Iterable<E> ruleParts = rules.get(initRule).generateValue().toIterable(); + final Iterable<E> ruleParts = rules.get(initRule).generateValue().toIterable(); - for (E rulePart : ruleParts) { - Iterable<T> generatedRuleParts = generateGenericValues(rulePart, + for (final E rulePart : ruleParts) { + final Iterable<T> generatedRuleParts = generateGenericValues(rulePart, tokenTransformer, spacer).toIterable(); - for (T generatedRulePart : generatedRuleParts) { + for (final T generatedRulePart : generatedRuleParts) { returnedList.add(generatedRulePart); returnedList.add(spacer); } } } else { - T transformedToken = tokenTransformer.apply(initRule); + final T transformedToken = tokenTransformer.apply(initRule); if (transformedToken == null) throw new NullPointerException("Transformer created null token"); @@ -350,8 +352,8 @@ public class WeightedGrammar<E> { * @return A list of random grammar elements generated by the specified * rule. */ - public IList<E> generateListValues(E initRule, E spacer) { - IList<E> retList = generateGenericValues(initRule, strang -> strang, spacer); + public IList<E> generateListValues(final E initRule, final E spacer) { + final IList<E> retList = generateGenericValues(initRule, strang -> strang, spacer); return retList; } @@ -380,7 +382,7 @@ public class WeightedGrammar<E> { * @return The set of all rule names in this grammar */ public IList<E> getRuleNames() { - IList<E> ruleNames = new FunctionalList<>(); + final IList<E> ruleNames = new FunctionalList<>(); ruleNames.addAll(rules.keyList()); ruleNames.addAll(specialRules.keyList()); @@ -395,7 +397,7 @@ public class WeightedGrammar<E> { * The name of the subgrammar to get. * @return The subgrammar with the specified name. */ - public WeightedGrammar<E> getSubgrammar(E name) { + public WeightedGrammar<E> getSubgrammar(final E name) { if (name == null) throw new NullPointerException("Subgrammar name must not be null"); return subgrammars.get(name); @@ -412,13 +414,13 @@ public class WeightedGrammar<E> { /** * Check if this grammar has a given rule. - * + * * @param ruleName * The rule to check for. - * + * * @return Whether or not the grammar has a rule by that name. */ - public boolean hasRule(E ruleName) { + public boolean hasRule(final E ruleName) { return rules.containsKey(ruleName) || specialRules.containsKey(ruleName); } @@ -434,7 +436,8 @@ public class WeightedGrammar<E> { * @param numberOfTimes * The number of times to prefix the token */ - public void multiPrefixRule(E ruleName, E prefixToken, int additionalProbability, int numberOfTimes) { + public void multiPrefixRule(final E ruleName, final E prefixToken, final int additionalProbability, + final int numberOfTimes) { if (ruleName == null) throw new NullPointerException("Rule name must not be null"); else if (prefixToken == null) @@ -442,18 +445,18 @@ public class WeightedGrammar<E> { else if (numberOfTimes < 1) throw new IllegalArgumentException("Number of times to prefix must be positive."); - WeightedRandom<IList<E>> rule = rules.get(ruleName); + final WeightedRandom<IList<E>> rule = rules.get(ruleName); - IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>(); + final IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>(); rule.getValues().forEach((pair) -> { - IList<IList<E>> newRule = new FunctionalList<>(); + final IList<IList<E>> newRule = new FunctionalList<>(); for (int i = 1; i <= numberOfTimes; i++) { - IList<E> newCase = pair.merge((left, right) -> { - IList<E> returnVal = new FunctionalList<>(); + final IList<E> newCase = pair.merge((left, right) -> { + final IList<E> returnVal = new FunctionalList<>(); - for (E val : right.toIterable()) { + for (final E val : right.toIterable()) { returnVal.add(val); } @@ -468,7 +471,7 @@ public class WeightedGrammar<E> { } newRule.forEach((list) -> { - Integer currentProb = pair.merge((left, right) -> left); + final Integer currentProb = pair.merge((left, right) -> left); newResults.add(new Pair<>(currentProb + additionalProbability, list)); }); @@ -492,20 +495,20 @@ public class WeightedGrammar<E> { * @param prefixToken * The token to prefix to the rule */ - public void prefixRule(E ruleName, E prefixToken, int additionalProbability) { + public void prefixRule(final E ruleName, final E prefixToken, final int additionalProbability) { if (ruleName == null) throw new NullPointerException("Rule name must not be null"); else if (prefixToken == null) throw new NullPointerException("Prefix token must not be null"); - WeightedRandom<IList<E>> rule = rules.get(ruleName); + final WeightedRandom<IList<E>> rule = rules.get(ruleName); - IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>(); + final IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>(); rule.getValues().forEach((pair) -> { - IList<E> newCase = pair.merge((left, right) -> { - IList<E> returnVal = new FunctionalList<>(); + final IList<E> newCase = pair.merge((left, right) -> { + final IList<E> returnVal = new FunctionalList<>(); - for (E val : right.toIterable()) { + for (final E val : right.toIterable()) { returnVal.add(val); } @@ -526,7 +529,7 @@ public class WeightedGrammar<E> { * @param initRule * The initial rule of this grammar */ - public void setInitialRule(String initRule) { + public void setInitialRule(final String initRule) { this.initialRule = initRule; } @@ -540,20 +543,20 @@ public class WeightedGrammar<E> { * @param additionalProbability * Additional probability of the prefixed rule */ - public void suffixRule(E ruleName, E suffixToken, int additionalProbability) { + public void suffixRule(final E ruleName, final E suffixToken, final int additionalProbability) { if (ruleName == null) throw new NullPointerException("Rule name must not be null"); else if (suffixToken == null) throw new NullPointerException("Prefix token must not be null"); - WeightedRandom<IList<E>> rule = rules.get(ruleName); + final WeightedRandom<IList<E>> rule = rules.get(ruleName); - IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>(); + final IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>(); rule.getValues().forEach((par) -> { - IList<E> newCase = par.merge((left, right) -> { - IList<E> returnVal = new FunctionalList<>(); + final IList<E> newCase = par.merge((left, right) -> { + final IList<E> returnVal = new FunctionalList<>(); - for (E val : right.toIterable()) { + for (final E val : right.toIterable()) { returnVal.add(val); } 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 88f623e..18225ef 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java @@ -1,13 +1,13 @@ package bjc.utils.gen; +import java.util.Random; + import bjc.utils.data.IHolder; import bjc.utils.data.IPair; import bjc.utils.data.Identity; import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.IList; -import java.util.Random; - /** * Represents a random number generator where certain results are weighted more * heavily than others. @@ -21,17 +21,17 @@ public class WeightedRandom<E> { /* * The list of probabilities for each result */ - private IList<Integer> probabilities; + private final IList<Integer> probabilities; /* * The list of possible results to pick from */ - private IList<E> results; + private final IList<E> results; /* * The source for any needed random numbers */ - private Random source; + private final Random source; private int totalChance; @@ -42,11 +42,11 @@ public class WeightedRandom<E> { * @param src * The source of randomness to use. */ - public WeightedRandom(Random src) { + public WeightedRandom(final Random src) { probabilities = new FunctionalList<>(); results = new FunctionalList<>(); - if(src == null) throw new NullPointerException("Source of randomness must not be null"); + if (src == null) throw new NullPointerException("Source of randomness must not be null"); source = src; } @@ -59,7 +59,7 @@ public class WeightedRandom<E> { * @param result * The result to get when the chance comes up. */ - public void addProbability(int chance, E result) { + public void addProbability(final int chance, final E result) { probabilities.add(chance); results.add(result); @@ -72,13 +72,13 @@ public class WeightedRandom<E> { * @return A random value selected in a weighted fashion. */ public E generateValue() { - IHolder<Integer> value = new Identity<>(source.nextInt(totalChance)); - IHolder<E> current = new Identity<>(); - IHolder<Boolean> picked = new Identity<>(true); + final IHolder<Integer> value = new Identity<>(source.nextInt(totalChance)); + final IHolder<E> current = new Identity<>(); + final IHolder<Boolean> picked = new Identity<>(true); probabilities.forEachIndexed((index, probability) -> { - if(picked.unwrap(bool -> bool)) { - if(value.unwrap((number) -> number < probability)) { + if (picked.unwrap(bool -> bool)) { + if (value.unwrap((number) -> number < probability)) { current.transform((result) -> results.getByIndex(index)); picked.transform((bool) -> false); |
