diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-06-03 23:59:30 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-06-04 00:01:28 -0300 |
| commit | 5c416488ef63b5004ca424de56894eb17712f116 (patch) | |
| tree | b16cb41e704f8046ce75d094bfe5968116d93dfe /src/main/java/bjc/rgens/parser/Rule.java | |
| parent | 63aaf20560a2c4037cab8a7ef5fd6251d6036a2d (diff) | |
Add recurrance limit
This controls the number of times a rule can be entered into during a
single generations, and is set to 5 by default
Diffstat (limited to 'src/main/java/bjc/rgens/parser/Rule.java')
| -rwxr-xr-x | src/main/java/bjc/rgens/parser/Rule.java | 28 |
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; + } } |
