Rule Execution & Results
When you run a rule, it returns a RuleResult — a Java record containing the rule that was executed and its execution status.
RuleResult
Section titled “RuleResult”// RuleResult is a Java recordpublic record RuleResult(Rule rule, RuleExecutionStatus status) {}Four Possible Statuses
Section titled “Four Possible Statuses”- PASS Condition was true, then-actions executed
- FAIL Condition was false, otherwise-action executed
- SKIPPED PreCondition was false, rule didn’t run
- ERROR Execution threw an exception (new in 2.0.0)
Checking Results
Section titled “Checking Results”RuleResult result = rule.run(age -> 25);
// Check the statusresult.status(); // RuleExecutionStatus.PASSresult.status().isPass(); // trueresult.status().isFail(); // falseresult.status().isSkipped();// falseresult.status().isError(); // false (new in 2.0.0)
// Get the rule that ranresult.rule().getName(); // "AgeCheck"Practical Examples
Section titled “Practical Examples”// Condition true → PASSRuleResult r1 = rule.run(age -> 25);// r1.status() == PASS
// Condition false → FAILRuleResult r2 = rule.run(age -> 15);// r2.status() == FAIL
// PreCondition false → SKIPPEDRule guarded = Rule.builder() .name("Guarded") .preCondition(condition((Boolean enabled) -> enabled)) .given(condition(() -> true)) .then(action(() -> {})) .build();
RuleResult r3 = guarded.run(enabled -> false);// r3.status() == SKIPPEDUsing Results for Control Flow
Section titled “Using Results for Control Flow”RuleResult result = rule.run(age -> 25);
if (result.status().isPass()) { System.out.println("Rule passed: " + result.rule().getName());} else if (result.status().isFail()) { System.out.println("Rule failed: " + result.rule().getName());} else { System.out.println("Rule skipped: " + result.rule().getName());}