From 27bf571d6413c3cc6a5d664b5bddd38d21d7b1cd Mon Sep 17 00:00:00 2001 From: EVE Date: Mon, 13 Mar 2017 16:42:21 -0400 Subject: Formatting --- .../main/java/bjc/utils/gen/WeightedGrammar.java | 123 ++++++++++----------- 1 file changed, 58 insertions(+), 65 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java') 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 2a13be7..c266c7c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java @@ -14,13 +14,12 @@ import bjc.utils.funcdata.IList; import bjc.utils.funcdata.IMap; /** - * A random grammar, where certain rules will come up more often than - * others. + * A random grammar, where certain rules will come up more often than others. * * @author ben * * @param - * The values that make up sentances of this grammar. + * The values that make up sentances of this grammar. */ public class WeightedGrammar { /** @@ -31,7 +30,7 @@ public class WeightedGrammar { /** * The rules currently in this grammar */ - protected IMap>> rules; + protected IMap>> rules; /** * The random number generator used for random numbers @@ -51,7 +50,7 @@ public class WeightedGrammar { /** * Predicate for marking special tokens */ - + private Predicate specialMarker; /** @@ -73,7 +72,7 @@ public class WeightedGrammar { * randomness. * * @param source - * The source of randomness to use + * The source of randomness to use */ public WeightedGrammar(Random source) { this(); @@ -85,14 +84,13 @@ public class WeightedGrammar { rng = source; } - public void configureSpecial(Predicate marker, - BiFunction, IList> action) { + public void configureSpecial(Predicate marker, BiFunction, IList> action) { specialMarker = marker; specialAction = action; } public void addSpecialRule(E ruleName, Supplier> cse) { - if(ruleName == null) { + if (ruleName == null) { throw new NullPointerException("Rule name must not be null"); } else if (cse == null) { throw new NullPointerException("Case must not be null"); @@ -105,11 +103,11 @@ public class WeightedGrammar { * Add a case to an already existing rule. * * @param ruleName - * The rule to add a case to. + * The rule to add a case to. * @param probability - * The probability for this rule to be chosen. + * The probability for this rule to be chosen. * @param cse - * The case being added. + * The case being added. */ public void addCase(E ruleName, int probability, IList cse) { if (ruleName == null) { @@ -125,9 +123,9 @@ public class WeightedGrammar { * Add a alias for an existing subgrammar * * @param name - * The name of the subgrammar to alias + * The name of the subgrammar to alias * @param alias - * The alias of the subgrammar + * The alias of the subgrammar * @return Whether the alias was succesfully created */ public boolean addGrammarAlias(E name, E alias) { @@ -153,7 +151,7 @@ public class WeightedGrammar { * Add a new rule with no cases. * * @param name - * The name of the rule to add. + * The name of the rule to add. * @return Whether or not the rule was succesfully added. */ public boolean addRule(E name) { @@ -172,9 +170,9 @@ public class WeightedGrammar { * Add a new rule with a set of cases. * * @param name - * The name of the rule to add. + * The name of the rule to add. * @param cases - * The set of cases for the rule. + * The set of cases for the rule. * @return Whether or not the rule was succesfully added. */ public boolean addRule(E name, WeightedRandom> cases) { @@ -196,9 +194,9 @@ public class WeightedGrammar { * Add a subgrammar. * * @param name - * The name of the subgrammar. + * The name of the subgrammar. * @param subgrammar - * The subgrammar to add. + * The subgrammar to add. * @return Whether or not the subgrammar was succesfully added. */ public boolean addSubgrammar(E name, WeightedGrammar subgrammar) { @@ -220,7 +218,7 @@ public class WeightedGrammar { * Remove a rule with the specified name. * * @param name - * The name of the rule to remove. + * The name of the rule to remove. */ public void deleteRule(E name) { if (name == null) { @@ -234,7 +232,7 @@ public class WeightedGrammar { * Remove a subgrammar with the specified name. * * @param name - * The name of the subgrammar to remove. + * The name of the subgrammar to remove. */ public void deleteSubgrammar(E name) { if (name == null) { @@ -250,7 +248,7 @@ public class WeightedGrammar { * Only generates sentances one layer deep. * * @param ruleName - * The rule to test. + * The rule to test. * @return A set of sentances generated by the specified rule. */ public IList> generateDebugValues(E ruleName) { @@ -273,14 +271,15 @@ public class WeightedGrammar { * Generate a generic sentance from a initial rule. * * @param - * The type of the transformed output + * The type of the transformed output * * @param initRule - * The initial rule to start with. + * The initial rule to start with. * @param tokenTransformer - * The function to transform grammar output into something. + * The function to transform grammar output into + * something. * @param spacer - * The spacer element to add in between output tokens. + * The spacer element to add in between output tokens. * @return A randomly generated sentance from the specified initial * rule. */ @@ -297,39 +296,39 @@ public class WeightedGrammar { IList genRules = new FunctionalList<>(initRules); - if(specialMarker != null) { - if(specialMarker.test(initRules)) { + if (specialMarker != null) { + if (specialMarker.test(initRules)) { genRules = specialAction.apply(initRules, this); } } - for(E initRule : genRules.toIterable()) { - if(specialRules.containsKey(initRule)) { - for(E rulePart : specialRules.get(initRule).get().toIterable()) { - Iterable generatedRuleParts = generateGenericValues( - rulePart, tokenTransformer, spacer).toIterable(); + for (E initRule : genRules.toIterable()) { + if (specialRules.containsKey(initRule)) { + for (E rulePart : specialRules.get(initRule).get().toIterable()) { + Iterable generatedRuleParts = generateGenericValues(rulePart, + tokenTransformer, spacer).toIterable(); - for(T generatedRulePart : generatedRuleParts) { + for (T generatedRulePart : generatedRuleParts) { returnedList.add(generatedRulePart); returnedList.add(spacer); } } } else if (subgrammars.containsKey(initRule)) { - Iterable ruleParts = subgrammars.get(initRule).generateGenericValues( - initRule, tokenTransformer, spacer).toIterable(); + Iterable ruleParts = subgrammars.get(initRule) + .generateGenericValues(initRule, tokenTransformer, spacer).toIterable(); - for(T rulePart : ruleParts) { + for (T rulePart : ruleParts) { returnedList.add(rulePart); returnedList.add(spacer); } } else if (rules.containsKey(initRule)) { Iterable ruleParts = rules.get(initRule).generateValue().toIterable(); - for(E rulePart : ruleParts) { - Iterable generatedRuleParts = - generateGenericValues(rulePart, tokenTransformer, spacer).toIterable(); + for (E rulePart : ruleParts) { + Iterable generatedRuleParts = generateGenericValues(rulePart, + tokenTransformer, spacer).toIterable(); - for(T generatedRulePart : generatedRuleParts) { + for (T generatedRulePart : generatedRuleParts) { returnedList.add(generatedRulePart); returnedList.add(spacer); } @@ -350,13 +349,12 @@ public class WeightedGrammar { } /** - * Generate a random list of grammar elements from a given initial - * rule. + * Generate a random list of grammar elements from a given initial rule. * * @param initRule - * The initial rule to start with. + * The initial rule to start with. * @param spacer - * The item to use to space the list. + * The item to use to space the list. * @return A list of random grammar elements generated by the specified * rule. */ @@ -402,7 +400,7 @@ public class WeightedGrammar { * Get the subgrammar with the specified name. * * @param name - * The name of the subgrammar to get. + * The name of the subgrammar to get. * @return The subgrammar with the specified name. */ public WeightedGrammar getSubgrammar(E name) { @@ -430,16 +428,15 @@ public class WeightedGrammar { * Prefix a given rule with a token multiple times * * @param ruleName - * The name of the rule to prefix + * The name of the rule to prefix * @param prefixToken - * The token to prefix to the rules + * The token to prefix to the rules * @param additionalProbability - * The additional probability of the tokens + * The additional probability of the tokens * @param numberOfTimes - * The number of times to prefix the token + * The number of times to prefix the token */ - public void multiPrefixRule(E ruleName, E prefixToken, - int additionalProbability, int numberOfTimes) { + public void multiPrefixRule(E ruleName, E prefixToken, int additionalProbability, int numberOfTimes) { if (ruleName == null) { throw new NullPointerException("Rule name must not be null"); } else if (prefixToken == null) { @@ -492,11 +489,11 @@ public class WeightedGrammar { * given token * * @param additionalProbability - * The amount to adjust the probability by + * The amount to adjust the probability by * @param ruleName - * The name of the rule to prefix + * The name of the rule to prefix * @param prefixToken - * The token to prefix to the rule + * The token to prefix to the rule */ public void prefixRule(E ruleName, E prefixToken, int additionalProbability) { if (ruleName == null) { @@ -525,16 +522,14 @@ public class WeightedGrammar { newResults.add(new Pair<>(pair.merge((left, right) -> left) + additionalProbability, newCase)); }); - newResults.forEach((pair) -> - pair.doWith((left, right) -> - addCase(ruleName, left, right))); + newResults.forEach((pair) -> pair.doWith((left, right) -> addCase(ruleName, left, right))); } /** * Set the initial rule of the graphic * * @param initRule - * The initial rule of this grammar + * The initial rule of this grammar */ public void setInitialRule(String initRule) { this.initialRule = initRule; @@ -544,11 +539,11 @@ public class WeightedGrammar { * Suffix a token to a rule * * @param ruleName - * The rule to suffix + * The rule to suffix * @param suffixToken - * The token to prefix to the rule + * The token to prefix to the rule * @param additionalProbability - * Additional probability of the prefixed rule + * Additional probability of the prefixed rule */ public void suffixRule(E ruleName, E suffixToken, int additionalProbability) { if (ruleName == null) { @@ -577,8 +572,6 @@ public class WeightedGrammar { newResults.add(new Pair<>(par.merge((left, right) -> left) + additionalProbability, newCase)); }); - newResults.forEach((pair) -> - pair.doWith((left, right) -> - addCase(ruleName, left, right))); + newResults.forEach((pair) -> pair.doWith((left, right) -> addCase(ruleName, left, right))); } } -- cgit v1.2.3