Package com.tnsai.invariants.rules
Class RuleEngine
java.lang.Object
com.tnsai.invariants.rules.RuleEngine
Runtime engine for evaluating conditional transition rules.
Evaluates rules like "DEAD if neighbors invalid input: '<' 2" at runtime:
- Parses condition into expression
- Evaluates condition against target object
- Checks if actual state matches expected effect
- Reports violations when rules don't hold
Usage
RuleEngine engine = new RuleEngine(cellRole);
engine.addRule("DEAD if neighbors < 2 (underpopulation)");
engine.addRule("ALIVE if neighbors == 3 (reproduction)");
// After state change
List<RuleViolation> violations = engine.evaluateAllRules("state");
Rule Evaluation Semantics
For a rule "DEAD if neighbors invalid input: '<' 2":
- Evaluate condition: neighbors invalid input: '<' 2
- If condition is true, expected effect is DEAD
- Check if actual state field equals DEAD
- If not, report violation
- Author:
- TnsAI Team
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents a rule violation.static classException thrown when rules are violated. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddRule(ConditionalRule rule) Adds a pre-parsed rule.Adds a rule from a string.Adds multiple rules from an array.voidClears all rules.static voidEvaluates rules and throws exception if any violations found.Evaluates all rules and returns violations.evaluateAllRules(String stateField) Evaluates all rules against a specific state field.evaluateRule(ConditionalRule rule, String stateField) Evaluates a single rule.static RuleEngineCreates a RuleEngine and adds rules from invariant strings.intGets the number of registered rules.getRules()Gets all registered rules.booleanhasRules()Checks if any rules are defined.setStateField(String fieldName) Sets the name of the state field to check effects against.
-
Constructor Details
-
RuleEngine
Creates a RuleEngine for the given target object.- Parameters:
target- the object to evaluate rules against
-
-
Method Details
-
setStateField
Sets the name of the state field to check effects against.- Parameters:
fieldName- the field name (default: "state")- Returns:
- this for chaining
-
addRule
Adds a rule from a string.- Parameters:
ruleString- the rule string (e.g., "DEAD if neighbors invalid input: '<' 2")- Returns:
- this for chaining
-
addRules
Adds multiple rules from an array.- Parameters:
ruleStrings- array of rule strings- Returns:
- this for chaining
-
addRule
Adds a pre-parsed rule.- Parameters:
rule- the ConditionalRule to add- Returns:
- this for chaining
-
evaluateAllRules
Evaluates all rules and returns violations.- Returns:
- list of rule violations (empty if all rules pass)
-
evaluateAllRules
Evaluates all rules against a specific state field.- Parameters:
stateField- the field name to check effects against- Returns:
- list of rule violations
-
evaluateRule
Evaluates a single rule.- Parameters:
rule- the rule to evaluatestateField- the field name to check effects against- Returns:
- violation if rule fails, null if passes or not applicable
-
getRuleCount
public int getRuleCount()Gets the number of registered rules. -
getRules
Gets all registered rules. -
clearRules
public void clearRules()Clears all rules. -
hasRules
public boolean hasRules()Checks if any rules are defined. -
from
Creates a RuleEngine and adds rules from invariant strings.- Parameters:
target- the target objectinvariants- array of invariant/rule strings- Returns:
- configured RuleEngine
-
enforce
Evaluates rules and throws exception if any violations found.- Parameters:
target- the target objectinvariants- array of rule stringsstateField- the state field to check- Throws:
RuleEngine.RuleViolationException- if any rule is violated
-