summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/gen
diff options
context:
space:
mode:
Diffstat (limited to 'base/src/main/java/bjc/utils/gen')
-rw-r--r--base/src/main/java/bjc/utils/gen/RandomGrammar.java21
-rw-r--r--base/src/main/java/bjc/utils/gen/WeightedGrammar.java247
-rw-r--r--base/src/main/java/bjc/utils/gen/WeightedRandom.java39
3 files changed, 172 insertions, 135 deletions
diff --git a/base/src/main/java/bjc/utils/gen/RandomGrammar.java b/base/src/main/java/bjc/utils/gen/RandomGrammar.java
index 3de08d6..978accb 100644
--- a/base/src/main/java/bjc/utils/gen/RandomGrammar.java
+++ b/base/src/main/java/bjc/utils/gen/RandomGrammar.java
@@ -9,12 +9,10 @@ import bjc.utils.funcdata.IList;
* @author ben
*
* @param <E>
- * The type of grammar elements to use.
+ * The type of grammar elements to use.
*/
public class RandomGrammar<E> extends WeightedGrammar<E> {
- /**
- * Create a new random grammar.
- */
+ /** Create a new random grammar. */
public RandomGrammar() {
rules = new FunctionalMap<>();
}
@@ -23,9 +21,10 @@ public class RandomGrammar<E> extends WeightedGrammar<E> {
* Add cases to a specified rule.
*
* @param rule
- * The name of the rule to add cases to.
+ * The name of the rule to add cases to.
+ *
* @param cases
- * The cases to add for this rule.
+ * The cases to add for this rule.
*/
@SafeVarargs
public final void addCases(final E rule, final IList<E>... cases) {
@@ -38,9 +37,10 @@ public class RandomGrammar<E> extends WeightedGrammar<E> {
* Create a rule with the specified name and cases.
*
* @param rule
- * The name of the rule to add.
+ * The name of the rule to add.
+ *
* @param cases
- * The cases to add for this rule.
+ * The cases to add for this rule.
*/
@SafeVarargs
public final void makeRule(final E rule, final IList<E>... cases) {
@@ -55,9 +55,10 @@ public class RandomGrammar<E> extends WeightedGrammar<E> {
* Create a rule with the specified name and cases.
*
* @param rule
- * The name of the rule to add.
+ * The name of the rule to add.
+ *
* @param cases
- * The cases to add for this rule.
+ * The cases to add for this rule.
*/
public void makeRule(final E rule, final IList<IList<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 7777ad8..1fbba02 100644
--- a/base/src/main/java/bjc/utils/gen/WeightedGrammar.java
+++ b/base/src/main/java/bjc/utils/gen/WeightedGrammar.java
@@ -19,48 +19,31 @@ import bjc.utils.funcdata.IMap;
* @author ben
*
* @param <E>
- * The values that make up sentences of this grammar.
+ * The values that make up sentences of this grammar.
*/
public class WeightedGrammar<E> {
- /**
- * The initial rule of the grammar
- */
+ /** The initial rule of the grammar */
protected String initialRule;
- /**
- * The rules currently in this grammar
- */
+ /** The rules currently in this grammar */
protected IMap<E, WeightedRandom<IList<E>>> rules;
- /**
- * The random number generator used for random numbers
- */
+ /** The random number generator used for random numbers */
private Random rng;
- /**
- * All of the subgrammars of this grammar
- */
+ /** All of the subgrammars of this grammar */
protected IMap<E, WeightedGrammar<E>> subgrammars;
- /**
- * Rules that require special handling
- */
+ /** Rules that require special handling */
private IMap<E, Supplier<IList<E>>> specialRules;
- /**
- * Predicate for marking special tokens
- */
-
+ /** Predicate for marking special tokens */
private Predicate<E> specialMarker;
- /**
- * Action for special tokens
- */
+ /** Action for special tokens */
private BiFunction<E, WeightedGrammar<E>, IList<E>> specialAction;
- /**
- * Create a new weighted grammar.
- */
+ /** Create a new weighted grammar. */
public WeightedGrammar() {
rules = new FunctionalMap<>();
subgrammars = new FunctionalMap<>();
@@ -72,7 +55,7 @@ public class WeightedGrammar<E> {
* randomness.
*
* @param source
- * The source of randomness to use
+ * The source of randomness to use
*/
public WeightedGrammar(final Random source) {
this();
@@ -86,10 +69,10 @@ public class WeightedGrammar<E> {
* Configure the action to perform on special tokens.
*
* @param marker
- * The marker to find special tokens.
+ * The marker to find special tokens.
*
* @param action
- * The action to take on those tokens.
+ * The action to take on those tokens.
*/
public void configureSpecial(final Predicate<E> marker,
final BiFunction<E, WeightedGrammar<E>, IList<E>> action) {
@@ -101,15 +84,17 @@ public class WeightedGrammar<E> {
* Adds a special rule to the grammar.
*
* @param ruleName
- * The name of the special rule.
+ * The name of the special rule.
*
* @param cse
- * The case for the rule.
+ * The case for the rule.
*/
public void addSpecialRule(final E ruleName, final Supplier<IList<E>> 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");
+ } else if (cse == null) {
+ throw new NullPointerException("Case must not be null");
+ }
specialRules.put(ruleName, cse);
}
@@ -118,33 +103,42 @@ public class WeightedGrammar<E> {
* 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(final E ruleName, final int probability, final IList<E> cse) {
- if (ruleName == null)
+ 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");
+ } else if (cse == null) {
+ throw new NullPointerException("Case body must not be null");
+ }
rules.get(ruleName).addProbability(probability, cse);
}
/**
- * Add a alias for an existing subgrammar
+ * 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
- * @return Whether the alias was succesfully created
+ * The alias of the subgrammar.
+ *
+ * @return
+ * Whether the alias was succesfully created.
*/
public boolean addGrammarAlias(final E name, final E alias) {
- if (name == null)
+ 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");
+ } else if (alias == null) {
+ throw new NullPointerException("Subgrammar alias must not be null");
+ }
if (subgrammars.containsKey(alias)) return false;
@@ -160,8 +154,10 @@ public class WeightedGrammar<E> {
* Add a new rule with no cases.
*
* @param name
- * The name of the rule to add.
- * @return Whether or not the rule was successfully added.
+ * The name of the rule to add.
+ *
+ * @return
+ * Whether or not the rule was successfully added.
*/
public boolean addRule(final E name) {
if (rng == null) {
@@ -177,15 +173,20 @@ public class WeightedGrammar<E> {
* 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.
- * @return Whether or not the rule was succesfully added.
+ * The set of cases for the rule.
+ *
+ * @return
+ * Whether or not the rule was succesfully added.
*/
public boolean addRule(final E name, final WeightedRandom<IList<E>> cases) {
- if (name == null)
+ if (name == null) {
throw new NullPointerException("Name must not be null");
- else if (cases == null) throw new NullPointerException("Cases must not be null");
+ } else if (cases == null) {
+ throw new NullPointerException("Cases must not be null");
+ }
if (rules.containsKey(name)) return false;
@@ -197,15 +198,20 @@ public class WeightedGrammar<E> {
* Add a subgrammar.
*
* @param name
- * The name of the subgrammar.
+ * The name of the subgrammar.
+ *
* @param subgrammar
- * The subgrammar to add.
- * @return Whether or not the subgrammar was succesfully added.
+ * The subgrammar to add.
+ *
+ * @return
+ * Whether or not the subgrammar was succesfully added.
*/
public boolean addSubgrammar(final E name, final WeightedGrammar<E> subgrammar) {
- if (name == null)
+ if (name == null) {
throw new NullPointerException("Subgrammar name must not be null");
- else if (subgrammar == null) throw new NullPointerException("Subgrammar must not be null");
+ } else if (subgrammar == null) {
+ throw new NullPointerException("Subgrammar must not be null");
+ }
if (subgrammars.containsKey(name)) return false;
@@ -217,7 +223,7 @@ public class WeightedGrammar<E> {
* 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(final E name) {
if (name == null) throw new NullPointerException("Rule name must not be null");
@@ -229,7 +235,7 @@ public class WeightedGrammar<E> {
* 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(final E name) {
if (name == null) throw new NullPointerException("Rule name must not be null");
@@ -243,8 +249,10 @@ public class WeightedGrammar<E> {
* Only generates sentences one layer deep.
*
* @param ruleName
- * The rule to test.
- * @return A set of sentences generated by the specified rule.
+ * The rule to test.
+ *
+ * @return
+ * A set of sentences generated by the specified rule.
*/
public IList<IList<E>> generateDebugValues(final E ruleName) {
if (ruleName == null) throw new NullPointerException("Rule name must not be null");
@@ -264,28 +272,29 @@ public class WeightedGrammar<E> {
* Generate a generic sentence from a initial rule.
*
* @param <T>
- * The type of the transformed output
+ * The type of the transformed output
*
* @param initRules
- * 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 sentence from the specified initial
- * rule.
+ * @return
+ * A randomly generated sentence from the specified initial rule.
*/
public <T> IList<T> generateGenericValues(final E initRules, final Function<E, T> tokenTransformer,
final T spacer) {
- if (initRules == null)
+ if (initRules == null) {
throw new NullPointerException("Initial rule must not be null");
- else if (tokenTransformer == 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");
+ } else if (spacer == null) {
+ throw new NullPointerException("Spacer must not be null");
+ }
final IList<T> returnedList = new FunctionalList<>();
@@ -297,6 +306,10 @@ public class WeightedGrammar<E> {
}
}
+ /*
+ * @NOTE
+ * Can this loop be simplified in some way?
+ */
for (final E initRule : genRules.toIterable()) {
if (specialRules.containsKey(initRule)) {
for (final E rulePart : specialRules.get(initRule).get().toIterable()) {
@@ -346,11 +359,14 @@ public class WeightedGrammar<E> {
* 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.
- * @return A list of random grammar elements generated by the specified
- * rule.
+ * The item to use to space the list.
+ *
+ * @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 = generateGenericValues(initRule, strang -> strang, spacer);
@@ -359,27 +375,30 @@ public class WeightedGrammar<E> {
}
/**
- * Get the initial rule of this grammar
+ * Get the initial rule of this grammar.
*
- * @return The initial rule of this grammar
+ * @return
+ * The initial rule of this grammar.
*/
public String getInitialRule() {
return initialRule;
}
/**
- * Returns the number of rules in this grammar
+ * Returns the number of rules in this grammar.
*
- * @return The number of rules in this grammar
+ * @return
+ * The number of rules in this grammar.
*/
public int getRuleCount() {
return rules.size();
}
/**
- * Returns a set containing all of the rules in this grammar
+ * Returns a set containing all of the rules in this grammar.
*
- * @return The set of all rule names in this grammar
+ * @return
+ * The set of all rule names in this grammar.
*/
public IList<E> getRuleNames() {
final IList<E> ruleNames = new FunctionalList<>();
@@ -394,8 +413,10 @@ public class WeightedGrammar<E> {
* Get the subgrammar with the specified name.
*
* @param name
- * The name of the subgrammar to get.
- * @return The subgrammar with the specified name.
+ * The name of the subgrammar to get.
+ *
+ * @return
+ * The subgrammar with the specified name.
*/
public WeightedGrammar<E> getSubgrammar(final E name) {
if (name == null) throw new NullPointerException("Subgrammar name must not be null");
@@ -406,7 +427,8 @@ public class WeightedGrammar<E> {
/**
* Check if this grammar has an initial rule
*
- * @return Whether or not this grammar has an initial rule
+ * @return
+ * Whether or not this grammar has an initial rule
*/
public boolean hasInitialRule() {
return initialRule != null && !initialRule.equalsIgnoreCase("");
@@ -416,25 +438,29 @@ public class WeightedGrammar<E> {
* Check if this grammar has a given rule.
*
* @param ruleName
- * The rule to check for.
+ * The rule to check for.
*
- * @return Whether or not the grammar has a rule by that name.
+ * @return
+ * Whether or not the grammar has a rule by that name.
*/
public boolean hasRule(final E ruleName) {
return rules.containsKey(ruleName) || specialRules.containsKey(ruleName);
}
/**
- * Prefix a given rule with a token multiple times
+ * 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(final E ruleName, final E prefixToken, final int additionalProbability,
final int numberOfTimes) {
@@ -449,6 +475,10 @@ public class WeightedGrammar<E> {
final IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>();
+ /*
+ * @NOTE
+ * Can this be simplified?
+ */
rule.getValues().forEach((pair) -> {
final IList<IList<E>> newRule = new FunctionalList<>();
@@ -486,19 +516,24 @@ public class WeightedGrammar<E> {
/**
* Create a series of alternatives for a rule by prefixing them with a
- * given token
+ * 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(final E ruleName, final E prefixToken, final int additionalProbability) {
- if (ruleName == null)
+ 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");
+ }
+ else if (prefixToken == null) {
+ throw new NullPointerException("Prefix token must not be null");
+ }
final WeightedRandom<IList<E>> rule = rules.get(ruleName);
@@ -524,29 +559,33 @@ public class WeightedGrammar<E> {
}
/**
- * Set the initial rule of the graphic
+ * Set the initial rule of the grammar.
*
* @param initRule
- * The initial rule of this grammar
+ * The initial rule of this grammar.
*/
public void setInitialRule(final String initRule) {
this.initialRule = initRule;
}
/**
- * Suffix a token to a rule
+ * 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(final E ruleName, final E suffixToken, final int additionalProbability) {
- if (ruleName == null)
+ 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");
+ } else if (suffixToken == null) {
+ throw new NullPointerException("Prefix token must not be null");
+ }
final WeightedRandom<IList<E>> rule = rules.get(ruleName);
diff --git a/base/src/main/java/bjc/utils/gen/WeightedRandom.java b/base/src/main/java/bjc/utils/gen/WeightedRandom.java
index 18225ef..abba36a 100644
--- a/base/src/main/java/bjc/utils/gen/WeightedRandom.java
+++ b/base/src/main/java/bjc/utils/gen/WeightedRandom.java
@@ -15,32 +15,25 @@ import bjc.utils.funcdata.IList;
* @author ben
*
* @param <E>
- * The type of values that are randomly selected.
+ * The type of values that are randomly selected.
*/
public class WeightedRandom<E> {
- /*
- * The list of probabilities for each result
- */
+ /* The list of probabilities for each result */
private final IList<Integer> probabilities;
-
- /*
- * The list of possible results to pick from
- */
+ /* The list of possible results to pick from */
private final IList<E> results;
- /*
- * The source for any needed random numbers
- */
+ /* The source for any needed random numbers */
private final Random source;
-
+ /* The total chance for all values. */
private int totalChance;
/**
* Create a new weighted random generator with the specified source of
- * randomness
+ * randomness.
*
* @param src
- * The source of randomness to use.
+ * The source of randomness to use.
*/
public WeightedRandom(final Random src) {
probabilities = new FunctionalList<>();
@@ -55,9 +48,10 @@ public class WeightedRandom<E> {
* Add a probability for a specific result to be given.
*
* @param chance
- * The chance to get this result.
+ * The chance to get this result.
+ *
* @param result
- * The result to get when the chance comes up.
+ * The result to get when the chance comes up.
*/
public void addProbability(final int chance, final E result) {
probabilities.add(chance);
@@ -69,11 +63,12 @@ public class WeightedRandom<E> {
/**
* Generate a weighted random value.
*
- * @return A random value selected in a weighted fashion.
+ * @return
+ * A random value selected in a weighted fashion.
*/
public E generateValue() {
- final IHolder<Integer> value = new Identity<>(source.nextInt(totalChance));
- final IHolder<E> current = new Identity<>();
+ 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) -> {
@@ -94,7 +89,8 @@ public class WeightedRandom<E> {
/**
* Return a list of values that can be generated by this generator
*
- * @return A list of all the values that can be generated
+ * @return
+ * A list of all the values that can be generated
*/
public IList<E> getResults() {
return results;
@@ -104,7 +100,8 @@ public class WeightedRandom<E> {
* Return a list containing values that can be generated paired with the
* probability of those values being generated
*
- * @return A list of pairs of values and value probabilities
+ * @return
+ * A list of pairs of values and value probabilities
*/
public IList<IPair<Integer, E>> getValues() {
return probabilities.pairWith(results);