Class RuleEngine

java.lang.Object
com.tnsai.invariants.rules.RuleEngine

public class RuleEngine extends Object
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":

  1. Evaluate condition: neighbors invalid input: '<' 2
  2. If condition is true, expected effect is DEAD
  3. Check if actual state field equals DEAD
  4. If not, report violation
Author:
TnsAI Team
  • Constructor Details

    • RuleEngine

      public RuleEngine(Object target)
      Creates a RuleEngine for the given target object.
      Parameters:
      target - the object to evaluate rules against
  • Method Details

    • setStateField

      public RuleEngine setStateField(String fieldName)
      Sets the name of the state field to check effects against.
      Parameters:
      fieldName - the field name (default: "state")
      Returns:
      this for chaining
    • addRule

      public RuleEngine addRule(String ruleString)
      Adds a rule from a string.
      Parameters:
      ruleString - the rule string (e.g., "DEAD if neighbors invalid input: '<' 2")
      Returns:
      this for chaining
    • addRules

      public RuleEngine addRules(String[] ruleStrings)
      Adds multiple rules from an array.
      Parameters:
      ruleStrings - array of rule strings
      Returns:
      this for chaining
    • addRule

      public RuleEngine addRule(ConditionalRule rule)
      Adds a pre-parsed rule.
      Parameters:
      rule - the ConditionalRule to add
      Returns:
      this for chaining
    • evaluateAllRules

      public List<RuleEngine.RuleViolation> evaluateAllRules()
      Evaluates all rules and returns violations.
      Returns:
      list of rule violations (empty if all rules pass)
    • evaluateAllRules

      public List<RuleEngine.RuleViolation> evaluateAllRules(String stateField)
      Evaluates all rules against a specific state field.
      Parameters:
      stateField - the field name to check effects against
      Returns:
      list of rule violations
    • evaluateRule

      public RuleEngine.RuleViolation evaluateRule(ConditionalRule rule, String stateField)
      Evaluates a single rule.
      Parameters:
      rule - the rule to evaluate
      stateField - 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

      public List<ConditionalRule> getRules()
      Gets all registered rules.
    • clearRules

      public void clearRules()
      Clears all rules.
    • hasRules

      public boolean hasRules()
      Checks if any rules are defined.
    • from

      public static RuleEngine from(Object target, String[] invariants)
      Creates a RuleEngine and adds rules from invariant strings.
      Parameters:
      target - the target object
      invariants - array of invariant/rule strings
      Returns:
      configured RuleEngine
    • enforce

      public static void enforce(Object target, String[] invariants, String stateField)
      Evaluates rules and throws exception if any violations found.
      Parameters:
      target - the target object
      invariants - array of rule strings
      stateField - the state field to check
      Throws:
      RuleEngine.RuleViolationException - if any rule is violated