Rule Tracing & Debugging
The Tracer provides event-driven debugging for rule execution. It lets you observe every stage of a rule, ruleset or ruleflow lifecycle through listener callbacks, making it easy to debug, audit, and monitor your rule engine in action.
Listener Types
Section titled “Listener Types”Tracer supports four listener types, each targeting a different scope of events:
- RuliiListener Receives all events (rules, rulesets and ruleflows)
- RuleListener Receives rule-level events only
- RuleSetListener Receives ruleset-level events only
- RuleFlowListener Receives ruleflow-level events only
Creating a Tracer
Section titled “Creating a Tracer”Tracer tracer = Tracer.builder().build();Rule-Level Events
Section titled “Rule-Level Events”A RuleListener can respond to the following events during rule execution:
| Event | Description |
|---|---|
| onRuleStart | Fired when a rule begins execution |
| onPreConditionCheck | Fired before the pre-condition is evaluated |
| onGiven | Fired after the main condition is evaluated |
| onThen | Fired when the then-action executes |
| onOtherwise | Fired when the otherwise-action executes |
| onRuleError | Fired when a rule throws an error |
| onRuleEnd | Fired when a rule finishes execution |
RuleSet-Level Events
Section titled “RuleSet-Level Events”A RuleSetListener can respond to the following events during ruleset execution:
| Event | Description |
|---|---|
| onRuleSetStart | Fired when a ruleset begins execution |
| onRuleSetPreConditionCheck | Fired before the ruleset pre-condition |
| onRuleSetInitializer | Fired when the initializer runs |
| onRuleSetRuleRun | Fired each time a rule within the set runs |
| onRuleSetStop | Fired when execution stops early |
| onRuleSetFinalizer | Fired when the finalizer runs |
| onRuleSetResult | Fired when the result is produced |
| onRuleSetEnd | Fired when the ruleset finishes |
| onRuleSetError | Fired when the ruleset encounters an error |
RuleFlow-Level Events
Section titled “RuleFlow-Level Events”A RuleFlowListener (in org.rulii.ruleflow) can respond to the following events during ruleflow execution:
| Event | Description |
|---|---|
| onRuleFlowStart | Fired after the flow’s scope is created, before any step runs |
| onRuleFlowCommandExecuted | Fired after each top-level step completes |
| onRuleFlowExceptionHandled | Fired when a step-level or flow-level handler has handled an exception; the stepLevel argument says which |
| onRuleFlowResult | Fired after the result extractor runs (flows with returning(...) only) |
| onRuleFlowFinalizer | Fired after the finalizer completes |
| onRuleFlowEarlyExit | Fired when exit() ends the flow early; receives the result |
| onRuleFlowError | Fired when an exception leaves the flow unhandled |
| onRuleFlowEnd | Fired after the flow’s scope is removed; always fires, even on early exit or error |
- Top-level steps only.
onRuleFlowCommandExecutedfires once for awhen,forEachorscopestep, not for the steps inside its body. The command’sgetInfo()describes what ran; see Introspection & Metadata. - A handled step still completes. When a step’s own handler catches its exception,
onRuleFlowExceptionHandledfires and thenonRuleFlowCommandExecutedfor that step. - The finalizer comes first. On an early exit or an unhandled error,
onRuleFlowFinalizerfires beforeonRuleFlowEarlyExitoronRuleFlowError. - Rules and rulesets report as usual. The rules and rulesets a flow runs fire their own events to the
RuleListeners andRuleSetListeners on the same tracer.
Example: Adding a Rule Listener
Section titled “Example: Adding a Rule Listener”Tracer tracer = Tracer.builder().build();
// Add a rule listenertracer.addListener(new RuleListener() { @Override public void onRuleStart(Rule rule) { System.out.println("Starting: " + rule.getName()); }
@Override public void onGiven(Rule rule, Condition condition, boolean result) { System.out.println(rule.getName() + " condition: " + result); }
@Override public void onRuleEnd(Rule rule, RuleResult result) { System.out.println("Finished: " + rule.getName() + " → " + result.status()); }});
// Use tracer in RuleContextRuleContext ctx = RuleContext.builder() .with(name -> "Alice", age -> 30) .traceUsing(tracer) .build();
rule.run(ctx);// Output:// Starting: AgeCheck// AgeCheck condition: true// Finished: AgeCheck → PASSExample: Adding a RuleFlow Listener
Section titled “Example: Adding a RuleFlow Listener”RuleFlow<RuleContext> flow = RuleFlow.builder() .name("AgeFlow") .run(rule) .build();
Tracer tracer = Tracer.builder().build();
tracer.addListener(new RuleFlowListener() { @Override public void onRuleFlowStart(RuleFlow<?> ruleFlow, NamedScope ruleFlowScope) { System.out.println("Flow started: " + ruleFlow.getName()); }
@Override public void onRuleFlowCommandExecuted(RuleFlow<?> ruleFlow, RuleFlowCommand command) { System.out.println("Step completed: " + command.getInfo().getClass().getSimpleName()); }
@Override public void onRuleFlowEnd(RuleFlow<?> ruleFlow, NamedScope ruleFlowScope) { System.out.println("Flow ended: " + ruleFlow.getName()); }});
RuleContext ctx = RuleContext.builder() .with(age -> 30) .traceUsing(tracer) .build();
flow.run(ctx);// Output:// Flow started: AgeFlow// Step completed: Run// Flow ended: AgeFlowListener Management
Section titled “Listener Management”The Tracer provides methods to manage listeners at runtime:
// Add a listenertracer.addListener(myListener);
// Remove a specific listenertracer.removeListener(myListener);
// Clear all listenerstracer.clear();Use Cases
Section titled “Use Cases”- Debugging Step through rule execution to identify logic issues
- Audit Logging Record which rules fired and their outcomes
- Monitoring Track rule execution performance and patterns