summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/rgens/parser/Rule.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/bjc/rgens/parser/Rule.java')
-rwxr-xr-xsrc/main/java/bjc/rgens/parser/Rule.java28
1 files changed, 22 insertions, 6 deletions
diff --git a/src/main/java/bjc/rgens/parser/Rule.java b/src/main/java/bjc/rgens/parser/Rule.java
index 0022732..4e43fd7 100755
--- a/src/main/java/bjc/rgens/parser/Rule.java
+++ b/src/main/java/bjc/rgens/parser/Rule.java
@@ -19,6 +19,9 @@ public class Rule {
/* The cases for this rule. */
private WeightedRandom<RuleCase> cases;
+ public int recurLimit = 5;
+ private int currentRecur;
+
/**
* Create a new grammar rule.
*
@@ -47,11 +50,7 @@ public class Rule {
* The case to add.
*/
public void addCase(RuleCase cse) {
- if (cse == null) {
- throw new NullPointerException("Case must not be null");
- }
-
- cases.addProbability(1, cse);
+ addCase(cse, 1);
}
/**
@@ -65,6 +64,8 @@ public class Rule {
throw new NullPointerException("Case must not be null");
}
+ cse.belongsTo = name;
+
cases.addProbability(weight, cse);
}
@@ -111,7 +112,10 @@ public class Rule {
this.cases = new WeightedRandom<>();
for(IPair<Integer, RuleCase> cse : cases) {
- this.cases.addProbability(cse.getLeft(), cse.getRight());
+ RuleCase cs = cse.getRight();
+ cs.belongsTo = name;
+
+ this.cases.addProbability(cse.getLeft(), cs);
}
}
@@ -151,4 +155,16 @@ public class Rule {
public String toString() {
return String.format("Rule [ruleName='%s', ruleCases=%s]", name, cases);
}
+
+ public boolean doRecur() {
+ if(currentRecur > recurLimit) return false;
+
+ currentRecur += 1;
+
+ return true;
+ }
+
+ public void endRecur() {
+ if(currentRecur > 0) currentRecur -= 1;
+ }
}