08 — Execution
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
// RuleResult is a Java record
public record RuleResult(Rule rule, RuleExecutionStatus status) {}
Three Possible Statuses
PASS
Condition was true, then-actions executed
FAIL
Condition was false, otherwise-action executed
SKIPPED
PreCondition was false, rule didn't run
Checking Results
RuleResult result = rule.run(age -> 25);
// Check the status
result.status(); // RuleExecutionStatus.PASS
result.status().isPass(); // true
result.status().isFail(); // false
result.status().isSkipped();// false
// Get the rule that ran
result.rule().getName(); // "AgeCheck"
Practical Examples
// Condition true → PASS
RuleResult r1 = rule.run(age -> 25);
// r1.status() == PASS
// Condition false → FAIL
RuleResult r2 = rule.run(age -> 15);
// r2.status() == FAIL
// PreCondition false → SKIPPED
Rule guarded = Rule.builder()
.name("Guarded")
.preCondition(condition((Boolean enabled) -> enabled))
.given(condition(() -> true))
.then(action(() -> {}))
.build();
RuleResult r3 = guarded.run(enabled -> false);
// r3.status() == SKIPPED
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());
}