summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/gen
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-03-31 11:43:21 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-03-31 11:43:21 -0400
commit8a8b457c98e207d809a7616e73eb59bfe197a7a5 (patch)
tree36fcbb7f10e92adbfb866fced7f27af1ef89f636 /BJC-Utils2/src/main/java/bjc/utils/gen
parent32b1b46fcc855fffe6b0dddd10442a9a4f1544d2 (diff)
More code maintenance
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/gen')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java10
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java210
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java48
3 files changed, 143 insertions, 125 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 339b3f4..ca0578d 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java
@@ -31,8 +31,8 @@ public class RandomGrammar<E> extends WeightedGrammar<E> {
*/
@SafeVarargs
public final void addCases(E rule, FunctionalList<E>... cases) {
- for (FunctionalList<E> cse : cases) {
- super.addCase(rule, 1, cse);
+ for (FunctionalList<E> currentCase : cases) {
+ super.addCase(rule, 1, currentCase);
}
}
@@ -48,8 +48,8 @@ public class RandomGrammar<E> extends WeightedGrammar<E> {
public final void makeRule(E rule, FunctionalList<E>... cases) {
super.addRule(rule);
- for (FunctionalList<E> cse : cases) {
- super.addCase(rule, 1, cse);
+ for (FunctionalList<E> currentCase : cases) {
+ super.addCase(rule, 1, currentCase);
}
}
@@ -64,6 +64,6 @@ public class RandomGrammar<E> extends WeightedGrammar<E> {
public void makeRule(E rule, FunctionalList<FunctionalList<E>> cases) {
super.addRule(rule);
- cases.forEach(cse -> super.addCase(rule, 1, cse));
+ cases.forEach(currentCase -> super.addCase(rule, 1, currentCase));
}
}
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 463ad8b..28b61c5 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java
@@ -22,7 +22,7 @@ public class WeightedGrammar<E> {
/**
* The initial rule of the grammar
*/
- protected String initRule;
+ protected String initialRule;
/**
* The rules currently in this grammar
@@ -32,7 +32,7 @@ public class WeightedGrammar<E> {
/**
* The random number generator used for random numbers
*/
- private Random sr;
+ private Random rng;
/**
* All of the subgrammars of this grammar
@@ -51,13 +51,13 @@ public class WeightedGrammar<E> {
* Create a new weighted grammar that uses the specified source of
* randomness.
*
- * @param src
+ * @param source
* The source of randomness to use
*/
- public WeightedGrammar(Random src) {
+ public WeightedGrammar(Random source) {
this();
- sr = src;
+ rng = source;
}
/**
@@ -65,13 +65,13 @@ public class WeightedGrammar<E> {
*
* @param rule
* The rule to add a case to.
- * @param prob
+ * @param probability
* The probability for this rule to be chosen.
* @param cse
* The case being added.
*/
- public void addCase(E rule, int prob, FunctionalList<E> cse) {
- rules.get(rule).addProb(prob, cse);
+ public void addCase(E rule, int probability, FunctionalList<E> cse) {
+ rules.get(rule).addProbability(probability, cse);
}
/**
@@ -104,10 +104,10 @@ public class WeightedGrammar<E> {
* @return Whether or not the rule was succesfully added.
*/
public boolean addRule(E name) {
- if (sr == null) {
- sr = new Random();
+ if (rng == null) {
+ rng = new Random();
}
- return addRule(name, new WeightedRandom<>(sr));
+ return addRule(name, new WeightedRandom<>(rng));
}
/**
@@ -115,15 +115,16 @@ public class WeightedGrammar<E> {
*
* @param name
* The name of the rule to add.
- * @param rnd
+ * @param cases
* The set of cases for the rule.
* @return Whether or not the rule was succesfully added.
*/
- public boolean addRule(E name, WeightedRandom<FunctionalList<E>> rnd) {
+ public boolean addRule(E name,
+ WeightedRandom<FunctionalList<E>> cases) {
if (rules.containsKey(name)) {
return false;
} else {
- rules.put(name, rnd);
+ rules.put(name, cases);
return true;
}
}
@@ -133,15 +134,15 @@ public class WeightedGrammar<E> {
*
* @param name
* The name of the subgrammar.
- * @param subG
+ * @param subgrammar
* The subgrammar to add.
* @return Whether or not the subgrammar was succesfully added.
*/
- public boolean addSubGrammar(E name, WeightedGrammar<E> subG) {
+ public boolean addSubgrammar(E name, WeightedGrammar<E> subgrammar) {
if (subgrammars.containsKey(name)) {
return false;
} else {
- subgrammars.put(name, subG);
+ subgrammars.put(name, subgrammar);
return true;
}
}
@@ -150,20 +151,23 @@ public class WeightedGrammar<E> {
* Generate a set of debug sentences for the specified rule. Only
* generates sentances one layer deep.
*
- * @param rl
+ * @param ruleName
* The rule to test.
* @return A set of sentances generated by the specified rule.
*/
- public FunctionalList<FunctionalList<E>> debugVals(E rl) {
- FunctionalList<FunctionalList<E>> fl = new FunctionalList<>();
+ public FunctionalList<FunctionalList<E>>
+ generateDebugValues(E ruleName) {
+ FunctionalList<FunctionalList<E>> returnedList =
+ new FunctionalList<>();
- WeightedRandom<FunctionalList<E>> random = rules.get(rl);
+ WeightedRandom<FunctionalList<E>> ruleGenerator =
+ rules.get(ruleName);
for (int i = 0; i < 10; i++) {
- fl.add(random.genVal());
+ returnedList.add(ruleGenerator.generateValue());
}
- return fl;
+ return returnedList;
}
/**
@@ -172,52 +176,55 @@ public class WeightedGrammar<E> {
* @param <T>
* The type of the transformed output
*
- * @param initRle
+ * @param initRule
* The initial rule to start with.
- * @param f
+ * @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 sentance from the specified initial
* rule.
*/
- public <T> FunctionalList<T> genGeneric(E initRle, Function<E, T> f,
- T spacer) {
- FunctionalList<T> r = new FunctionalList<>();
-
- if (subgrammars.containsKey(initRle)) {
- subgrammars.get(initRle).genGeneric(initRle, f, spacer)
- .forEach(rp -> {
- r.add(rp);
- r.add(spacer);
+ public <T> FunctionalList<T> generateGenericValues(E initRule,
+ Function<E, T> tokenTransformer, T spacer) {
+ FunctionalList<T> returnedList = new FunctionalList<>();
+
+ if (subgrammars.containsKey(initRule)) {
+ subgrammars.get(initRule).generateGenericValues(initRule,
+ tokenTransformer, spacer).forEach(rulePart -> {
+ returnedList.add(rulePart);
+ returnedList.add(spacer);
});
- } else if (rules.containsKey(initRle)) {
- rules.get(initRle).genVal().forEach(
- rp -> genGeneric(rp, f, spacer).forEach(rp2 -> {
- r.add(rp2);
- r.add(spacer);
- }));
+ } else if (rules.containsKey(initRule)) {
+ rules.get(initRule).generateValue()
+ .forEach(rulePart -> generateGenericValues(rulePart,
+ tokenTransformer, spacer)
+ .forEach(generatedRulePart -> {
+ returnedList
+ .add(generatedRulePart);
+ returnedList.add(spacer);
+ }));
} else {
- r.add(f.apply(initRle));
- r.add(spacer);
+ returnedList.add(tokenTransformer.apply(initRule));
+ returnedList.add(spacer);
}
- return r;
+ return returnedList;
}
/**
* Generate a random list of grammar elements from a given initial
* rule.
*
- * @param initRle
+ * @param initRule
* 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.
*/
- public FunctionalList<E> genList(E initRle, E spacer) {
- return genGeneric(initRle, s -> s, spacer);
+ public FunctionalList<E> generateListValues(E initRule, E spacer) {
+ return generateGenericValues(initRule, strang -> strang, spacer);
}
/**
@@ -225,8 +232,8 @@ public class WeightedGrammar<E> {
*
* @return The initial rule of this grammar
*/
- public String getInitRule() {
- return initRule;
+ public String getInitialRule() {
+ return initialRule;
}
/**
@@ -236,7 +243,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(E name) {
return subgrammars.get(name);
}
@@ -245,80 +252,85 @@ public class WeightedGrammar<E> {
*
* @return Whether or not this grammar has an initial rule
*/
- public boolean hasInitRule() {
- return initRule != null && !initRule.equalsIgnoreCase("");
+ public boolean hasInitialRule() {
+ return initialRule != null && !initialRule.equalsIgnoreCase("");
}
/**
* Prefix a given rule with a token multiple times
*
- * @param rName
+ * @param ruleName
* The name of the rule to prefix
* @param prefixToken
* The token to prefix to the rules
- * @param addProb
+ * @param additionalProbability
* The additional probability of the tokens
- * @param nTimes
+ * @param numberOfTimes
* The number of times to prefix the token
*/
- public void multiPrefixRule(E rName, E prefixToken, int addProb,
- int nTimes) {
- WeightedRandom<FunctionalList<E>> rule = rules.get(rName);
+ public void multiPrefixRule(E ruleName, E prefixToken,
+ int additionalProbability, int numberOfTimes) {
+ WeightedRandom<FunctionalList<E>> rule = rules.get(ruleName);
FunctionalList<Pair<Integer, FunctionalList<E>>> newResults =
new FunctionalList<>();
- rule.getValues().forEach((par) -> {
- FunctionalList<FunctionalList<E>> nls = new FunctionalList<>();
+ rule.getValues().forEach((pair) -> {
+ FunctionalList<FunctionalList<E>> newRule =
+ new FunctionalList<>();
- // TODO bugtest this. if it works, write multiSuffixWith
- for (int i = 1; i <= nTimes; i++) {
- FunctionalList<E> nl =
- par.merge((left, right) -> right.clone());
+ for (int i = 1; i <= numberOfTimes; i++) {
+ FunctionalList<E> newCase =
+ pair.merge((left, right) -> right.clone());
for (int j = 1; j <= i; j++) {
- nl.prepend(prefixToken);
+ newCase.prepend(prefixToken);
}
- nls.add(nl);
+ newRule.add(newCase);
}
- nls.forEach((ls) -> newResults.add(new Pair<>(
- par.merge((left, right) -> left) + addProb, ls)));
+ newRule.forEach(
+ (list) -> newResults
+ .add(new Pair<>(
+ pair.merge((left, right) -> left)
+ + additionalProbability,
+ list)));
});
- newResults.forEach((par) -> par
- .doWith((left, right) -> addCase(rName, left, right)));
+ newResults.forEach((pair) -> pair
+ .doWith((left, right) -> addCase(ruleName, left, right)));
}
/**
* Create a series of alternatives for a rule by prefixing them with a
* given token
*
- * @param addProb
+ * @param additionalProbability
* The amount to adjust the probability by
- * @param rName
+ * @param ruleName
* The name of the rule to prefix
* @param prefixToken
* The token to prefix to the rule
*/
- public void prefixRule(E rName, E prefixToken, int addProb) {
- WeightedRandom<FunctionalList<E>> rule = rules.get(rName);
+ public void prefixRule(E ruleName, E prefixToken,
+ int additionalProbability) {
+ WeightedRandom<FunctionalList<E>> rule = rules.get(ruleName);
FunctionalList<Pair<Integer, FunctionalList<E>>> newResults =
new FunctionalList<>();
- rule.getValues().forEach((par) -> {
- FunctionalList<E> nl =
- par.merge((left, right) -> right.clone());
- nl.prepend(prefixToken);
+ rule.getValues().forEach((pair) -> {
+ FunctionalList<E> newCase =
+ pair.merge((left, right) -> right.clone());
+ newCase.prepend(prefixToken);
- newResults.add(new Pair<>(
- par.merge((left, right) -> left) + addProb, nl));
+ newResults.add(new Pair<>(pair.merge((left, right) -> left)
+ + additionalProbability, newCase));
});
- newResults.forEach((par) -> par
- .doWith((left, right) -> addCase(rName, left, right)));
+ newResults.forEach((pair) -> pair
+ .doWith((left, right) -> addCase(ruleName, left, right)));
}
/**
@@ -327,7 +339,7 @@ public class WeightedGrammar<E> {
* @param name
* The name of the rule to remove.
*/
- public void removeRule(E name) {
+ public void deleteRule(E name) {
rules.remove(name);
}
@@ -337,7 +349,7 @@ public class WeightedGrammar<E> {
* @param name
* The name of the subgrammar to remove.
*/
- public void removeSubgrammar(E name) {
+ public void deleteSubgrammar(E name) {
subgrammars.remove(name);
}
@@ -346,7 +358,7 @@ public class WeightedGrammar<E> {
*
* @return The number of rules in this grammar
*/
- public int ruleCount() {
+ public int getRuleCount() {
return rules.size();
}
@@ -355,7 +367,7 @@ public class WeightedGrammar<E> {
*
* @return The set of all rule names in this grammar
*/
- public Set<E> ruleNames() {
+ public Set<E> getRuleNames() {
return rules.keySet();
}
@@ -363,37 +375,39 @@ public class WeightedGrammar<E> {
* Set the initial rule of the graphic
*
* @param initRule
+ * The initial rule of this grammar
*/
- public void setInitRule(String initRule) {
- this.initRule = initRule;
+ public void setInitialRule(String initRule) {
+ this.initialRule = initRule;
}
/**
* Suffix a token to a rule
*
- * @param rName
+ * @param ruleName
* The rule to suffix
* @param prefixToken
* The token to prefix to the rule
- * @param addProb
+ * @param additionalProbability
* Additional probability of the prefixed rule
*/
- public void suffixRule(E rName, E prefixToken, int addProb) {
- WeightedRandom<FunctionalList<E>> rule = rules.get(rName);
+ public void suffixRule(E ruleName, E prefixToken,
+ int additionalProbability) {
+ WeightedRandom<FunctionalList<E>> rule = rules.get(ruleName);
FunctionalList<Pair<Integer, FunctionalList<E>>> newResults =
new FunctionalList<>();
rule.getValues().forEach((par) -> {
- FunctionalList<E> nl =
+ FunctionalList<E> newCase =
par.merge((left, right) -> right.clone());
- nl.add(prefixToken);
+ newCase.add(prefixToken);
- newResults.add(new Pair<>(
- par.merge((left, right) -> left) + addProb, nl));
+ newResults.add(new Pair<>(par.merge((left, right) -> left)
+ + additionalProbability, newCase));
});
- newResults.forEach((par) -> par
- .doWith((left, right) -> addCase(rName, left, right)));
+ newResults.forEach((pair) -> pair
+ .doWith((left, right) -> addCase(ruleName, left, right)));
}
-}
+} \ No newline at end of file
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 3ddb8ef..5a8ef8f 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java
@@ -19,7 +19,7 @@ public class WeightedRandom<E> {
/**
* The list of probabilities for each result
*/
- private FunctionalList<Integer> probs;
+ private FunctionalList<Integer> probabilities;
/**
* The list of possible results to pick from
@@ -29,7 +29,7 @@ public class WeightedRandom<E> {
/**
* The source for any needed random numbers
*/
- private Random src;
+ private Random source;
private int totalChance;
@@ -37,14 +37,14 @@ public class WeightedRandom<E> {
* Create a new weighted random generator with the specified source of
* randomness
*
- * @param sr
+ * @param src
* The source of randomness to use.
*/
- public WeightedRandom(Random sr) {
- probs = new FunctionalList<>();
+ public WeightedRandom(Random src) {
+ probabilities = new FunctionalList<>();
results = new FunctionalList<>();
- src = sr;
+ source = src;
}
/**
@@ -52,12 +52,12 @@ public class WeightedRandom<E> {
*
* @param chance
* The chance to get this result.
- * @param res
+ * @param result
* The result to get when the chance comes up.
*/
- public void addProb(int chance, E res) {
- probs.add(chance);
- results.add(res);
+ public void addProbability(int chance, E result) {
+ probabilities.add(chance);
+ results.add(result);
totalChance += chance;
}
@@ -67,24 +67,28 @@ public class WeightedRandom<E> {
*
* @return A random value selected in a weighted fashion.
*/
- public E genVal() {
- GenHolder<Integer> v = new GenHolder<>(src.nextInt(totalChance));
- IHolder<E> res = new GenHolder<>();
- GenHolder<Boolean> bl = new GenHolder<>(true);
+ public E generateValue() {
+ GenHolder<Integer> randomValue =
+ new GenHolder<>(source.nextInt(totalChance));
+ IHolder<E> currentResult = new GenHolder<>();
+ GenHolder<Boolean> valuePicked = new GenHolder<>(true);
- probs.forEachIndexed((i, p) -> {
- if (bl.unwrap(vl -> vl)) {
- if (v.unwrap((vl) -> vl < p)) {
- res.transform((vl) -> results.getByIndex(i));
+ probabilities.forEachIndexed((itemIndex, itemProbability) -> {
+ if (valuePicked.unwrap(bool -> bool)) {
+ if (randomValue
+ .unwrap((number) -> number < itemProbability)) {
+ currentResult.transform(
+ (result) -> results.getByIndex(itemIndex));
- bl.transform((vl) -> false);
+ valuePicked.transform((bool) -> false);
} else {
- v.transform((vl) -> vl - p);
+ randomValue.transform(
+ (number) -> number - itemProbability);
}
}
});
- return res.unwrap((vl) -> vl);
+ return currentResult.unwrap((result) -> result);
}
/**
@@ -103,6 +107,6 @@ public class WeightedRandom<E> {
* @return A list of pairs of values and value probabilities
*/
public FunctionalList<Pair<Integer, E>> getValues() {
- return probs.pairWith(results);
+ return probabilities.pairWith(results);
}
}