summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-02-21 15:41:33 -0500
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-02-21 15:41:33 -0500
commit99e79bca7c1fe3c43ca2ec67e36b8e323c5f7107 (patch)
tree3ba2a4205a0cfebf64839f3ec52c6e457817a043 /BJC-Utils2/src/main/java/bjc/utils
parent41e85b1493a9253f11aefd179d14c3c01a7c9287 (diff)
Commenting of various things
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java56
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java59
2 files changed, 96 insertions, 19 deletions
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 228f14d..de1e7b8 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java
@@ -19,9 +19,24 @@ import bjc.utils.funcdata.FunctionalList;
* The values that make up sentances of this grammar.
*/
public class WeightedGrammar<E> {
+ /**
+ * The initial rule of the grammar
+ */
protected String initRule;
+
+ /**
+ * The rules currently in this grammar
+ */
protected Map<E, WeightedRandom<FunctionalList<E>>> rules;
+
+ /**
+ * The random number generator used for random numbers
+ */
private Random sr;
+
+ /**
+ * All of the subgrammars of this grammar
+ */
protected Map<E, WeightedGrammar<E>> subgrammars;
/**
@@ -57,6 +72,15 @@ public class WeightedGrammar<E> {
rn.addProb(prob, cse);
}
+ /**
+ * Add a alias for an existing subgrammar
+ *
+ * @param name
+ * The name of the subgrammar to alias
+ * @param alias
+ * The alias of the subgrammar
+ * @return Whether the alias was succesfully created
+ */
public boolean addGrammarAlias(E name, E alias) {
if (subgrammars.containsKey(alias)) {
return false;
@@ -234,6 +258,18 @@ public class WeightedGrammar<E> {
});
}
+ /**
+ * Prefix a given rule with a token multiple times
+ *
+ * @param rName
+ * The name of the rule to prefix
+ * @param prefixToken
+ * The token to prefix to the rules
+ * @param addProb
+ * The additional probability of the tokens
+ * @param nTimes
+ * 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);
@@ -302,10 +338,25 @@ public class WeightedGrammar<E> {
return rules.keySet();
}
+ /**
+ * Set the initial rule of the graphic
+ *
+ * @param initRule
+ */
public void setInitRule(String initRule) {
this.initRule = initRule;
}
+ /**
+ * Suffix a token to a rule
+ *
+ * @param rName
+ * The rule to suffix
+ * @param prefixToken
+ * The token to prefix to the rule
+ * @param addProb
+ * Additional probability of the prefixed rule
+ */
public void suffixRule(E rName, E prefixToken, int addProb) {
WeightedRandom<FunctionalList<E>> rule = rules.get(rName);
@@ -323,6 +374,11 @@ public class WeightedGrammar<E> {
});
}
+ /**
+ * Check if this grammar has an initial rule
+ *
+ * @return Whether or not this grammar has an initial rule
+ */
public boolean hasInitRule() {
return initRule != null && !initRule.equalsIgnoreCase("");
}
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 4313360..4b0c276 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java
@@ -6,23 +6,38 @@ import bjc.utils.data.GenHolder;
import bjc.utils.data.Pair;
/**
- * 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.
+ * @param <E>
+ * The type of values that are randomly selected.
*/
public class WeightedRandom<E> {
- private Random src;
-
+ /**
+ * The list of probabilities for each result
+ */
private FunctionalList<Integer> probs;
+
+ /**
+ * The list of possible results to pick from
+ */
private FunctionalList<E> results;
- private int totalChance;
+ /**
+ * The source for any needed random numbers
+ */
+ private Random src;
+
+ private int totalChance;
/**
- * Create a new weighted random generator with the specified source of randomness
- * @param sr The source of randomness to use.
+ * Create a new weighted random generator with the specified source of
+ * randomness
+ *
+ * @param sr
+ * The source of randomness to use.
*/
public WeightedRandom(Random sr) {
probs = new FunctionalList<>();
@@ -33,8 +48,11 @@ public class WeightedRandom<E> {
/**
* Add a probability for a specific result to be given.
- * @param chance The chance to get this result.
- * @param res The result to get when the chance comes up.
+ *
+ * @param chance
+ * The chance to get this result.
+ * @param res
+ * The result to get when the chance comes up.
*/
public void addProb(int chance, E res) {
probs.add(chance);
@@ -45,6 +63,7 @@ public class WeightedRandom<E> {
/**
* Generate a weighted random value.
+ *
* @return A random value selected in a weighted fashion.
*/
public E genVal() {
@@ -65,21 +84,23 @@ public class WeightedRandom<E> {
return res.held;
}
-
- /**
- * 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
- */
- public FunctionalList<Pair<Integer, E>> getValues() {
- return probs.pairWith(results);
- }
/**
* Return a list of values that can be generated by this generator
+ *
* @return A list of all the values that can be generated
*/
public FunctionalList<E> getResults() {
return results;
}
+
+ /**
+ * 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
+ */
+ public FunctionalList<Pair<Integer, E>> getValues() {
+ return probs.pairWith(results);
+ }
}