From 5c416488ef63b5004ca424de56894eb17712f116 Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Sun, 3 Jun 2018 23:59:30 -0300 Subject: 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 --- src/main/java/bjc/rgens/parser/Rule.java | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'src/main/java/bjc/rgens/parser/Rule.java') 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 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 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; + } } -- cgit v1.2.3