Skip to content

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.

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
Tracer tracer = Tracer.builder().build();

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

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

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. onRuleFlowCommandExecuted fires once for a when, forEach or scope step, not for the steps inside its body. The command’s getInfo() describes what ran; see Introspection & Metadata.
  • A handled step still completes. When a step’s own handler catches its exception, onRuleFlowExceptionHandled fires and then onRuleFlowCommandExecuted for that step.
  • The finalizer comes first. On an early exit or an unhandled error, onRuleFlowFinalizer fires before onRuleFlowEarlyExit or onRuleFlowError.
  • Rules and rulesets report as usual. The rules and rulesets a flow runs fire their own events to the RuleListeners and RuleSetListeners on the same tracer.
Tracer tracer = Tracer.builder().build();
// Add a rule listener
tracer.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 RuleContext
RuleContext ctx = RuleContext.builder()
.with(name -> "Alice", age -> 30)
.traceUsing(tracer)
.build();
rule.run(ctx);
// Output:
// Starting: AgeCheck
// AgeCheck condition: true
// Finished: AgeCheck → PASS
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: AgeFlow

The Tracer provides methods to manage listeners at runtime:

// Add a listener
tracer.addListener(myListener);
// Remove a specific listener
tracer.removeListener(myListener);
// Clear all listeners
tracer.clear();
  • 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