summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/gen
diff options
context:
space:
mode:
authorEVE <EVE@EVE-PC>2017-03-13 16:42:21 -0400
committerEVE <EVE@EVE-PC>2017-03-13 16:42:21 -0400
commit27bf571d6413c3cc6a5d664b5bddd38d21d7b1cd (patch)
tree847fb52acb091c1c613d37b8477094d5762c6988 /BJC-Utils2/src/main/java/bjc/utils/gen
parentaa807a96cae2c47259fb38f710640883060339e9 (diff)
Formatting
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/gen')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java14
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java123
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java27
3 files changed, 78 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 7ed476e..5d054cf 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java
@@ -9,7 +9,7 @@ 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> {
/**
@@ -23,9 +23,9 @@ 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(E rule, IList<E>... cases) {
@@ -38,9 +38,9 @@ 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(E rule, IList<E>... cases) {
@@ -55,9 +55,9 @@ 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(E rule, IList<IList<E>> cases) {
if (cases == null) {
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 <E>
- * The values that make up sentances of this grammar.
+ * The values that make up sentances of this grammar.
*/
public class WeightedGrammar<E> {
/**
@@ -31,7 +30,7 @@ public class WeightedGrammar<E> {
/**
* The rules currently in this grammar
*/
- protected IMap<E, WeightedRandom<IList<E>>> rules;
+ protected IMap<E, WeightedRandom<IList<E>>> rules;
/**
* The random number generator used for random numbers
@@ -51,7 +50,7 @@ public class WeightedGrammar<E> {
/**
* Predicate for marking special tokens
*/
-
+
private Predicate<E> specialMarker;
/**
@@ -73,7 +72,7 @@ public class WeightedGrammar<E> {
* 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<E> {
rng = source;
}
- public void configureSpecial(Predicate<E> marker,
- BiFunction<E, WeightedGrammar<E>, IList<E>> action) {
+ public void configureSpecial(Predicate<E> marker, BiFunction<E, WeightedGrammar<E>, IList<E>> action) {
specialMarker = marker;
specialAction = action;
}
public void addSpecialRule(E ruleName, 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");
@@ -105,11 +103,11 @@ 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(E ruleName, int probability, IList<E> cse) {
if (ruleName == null) {
@@ -125,9 +123,9 @@ public class WeightedGrammar<E> {
* 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<E> {
* 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<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.
+ * 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) {
@@ -196,9 +194,9 @@ 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.
+ * The subgrammar to add.
* @return Whether or not the subgrammar was succesfully added.
*/
public boolean addSubgrammar(E name, WeightedGrammar<E> subgrammar) {
@@ -220,7 +218,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(E name) {
if (name == null) {
@@ -234,7 +232,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(E name) {
if (name == null) {
@@ -250,7 +248,7 @@ public class WeightedGrammar<E> {
* 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<IList<E>> generateDebugValues(E ruleName) {
@@ -273,14 +271,15 @@ public class WeightedGrammar<E> {
* Generate a generic sentance from a initial rule.
*
* @param <T>
- * 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<E> {
IList<E> 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<T> generatedRuleParts = generateGenericValues(
- rulePart, tokenTransformer, spacer).toIterable();
+ for (E initRule : genRules.toIterable()) {
+ if (specialRules.containsKey(initRule)) {
+ for (E rulePart : specialRules.get(initRule).get().toIterable()) {
+ Iterable<T> 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<T> ruleParts = subgrammars.get(initRule).generateGenericValues(
- initRule, tokenTransformer, spacer).toIterable();
+ Iterable<T> 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<E> ruleParts = rules.get(initRule).generateValue().toIterable();
- for(E rulePart : ruleParts) {
- Iterable<T> generatedRuleParts =
- generateGenericValues(rulePart, tokenTransformer, spacer).toIterable();
+ for (E rulePart : ruleParts) {
+ Iterable<T> 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<E> {
}
/**
- * 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<E> {
* 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<E> getSubgrammar(E name) {
@@ -430,16 +428,15 @@ public class WeightedGrammar<E> {
* 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<E> {
* 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<E> {
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<E> {
* 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<E> {
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)));
}
}
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 2df820c..d5256ee 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java
@@ -9,46 +9,45 @@ import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.IList;
/**
- * Represents a random number generator where certain results are weighted
- * more heavily than others.
+ * Represents a random number generator where certain results are weighted more
+ * heavily than others.
*
* @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
*/
- private IList<Integer> probabilities;
+ private IList<Integer> probabilities;
/*
* The list of possible results to pick from
*/
- private IList<E> results;
+ private IList<E> results;
/*
* The source for any needed random numbers
*/
- private Random source;
+ private Random source;
- private int totalChance;
+ private int totalChance;
/**
* Create a new weighted random generator with the specified source of
* randomness
*
* @param src
- * The source of randomness to use.
+ * The source of randomness to use.
*/
public WeightedRandom(Random src) {
probabilities = new FunctionalList<>();
results = new FunctionalList<>();
if (src == null) {
- throw new NullPointerException(
- "Source of randomness must not be null");
+ throw new NullPointerException("Source of randomness must not be null");
}
source = src;
@@ -58,9 +57,9 @@ 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(int chance, E result) {
probabilities.add(chance);
@@ -104,8 +103,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 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
*/