Skip to content

RuleContext

A RuleContext is the execution environment that holds everything a rule needs to run: bindings, locale, converters, message formatters, a tracer, a clock, and an executor service.

// From bindings
Bindings bindings = Bindings.builder().standard();
bindings.bind("name", "Alice");
RuleContext ctx = RuleContext.builder().build(bindings);
// From lambda declarations
RuleContext ctx2 = RuleContext.builder().build(name -> "Alice", age -> 30);
Component Purpose
Bindings Shared variable workspace
Matching Strategy Rule parameter matching strategy
Locale For locale-aware formatting
ConverterRegistry Type conversion between binding values
MessageResolver Looks up message templates
MessageFormatter Formats messages with placeholders
Tracer Logs rule execution for debugging
Clock Provides current time (useful for testing)
ExecutorService For async rule execution

Most of the time, you don’t need to create one manually. When you call rule.run(bindings) or rule.run(name -> "Alice"), a default context is created for you. Create one explicitly when you need to customize locale, converters, or tracing.

// Running with an explicit context
RuleContext ctx = RuleContext.builder().build(name -> "Alice");
RuleResult result = rule.run(ctx);

In annotated rules, you can receive the RuleContext as a parameter:

@Given
public boolean when(RuleContext context) {
Bindings bindings = context.getBindings();
return bindings.contains("name");
}

RuleContext.fork() creates a context for work that proceeds independently of the caller, typically on another thread. The fork is a new context (new id) with the same services and a fork of the bindings: it shares every scope present at the time of the call, by reference, and owns a fresh scope on top. Values set through either context are seen by both; bindings and scopes added by the fork are never seen by the caller, and scopes the caller adds afterwards are never seen by the fork.

RuleContext ctx = RuleContext.builder().build(total -> 100);
// Take the fork on the calling thread, at the moment of the hand-off
RuleContext fork = ctx.fork();
Future<RuleSetExecutionStatus> future = executor.submit(() -> ruleSet.run(fork));
// Keep using ctx meanwhile: the rule set's scopes stay private to the fork

Inside the fork the reserved ruleContext and bindings resolve to the fork, by name and by type: the pair is bound into the fork’s first own scope and the caller’s pair is not inherited, so a RuleContext or Bindings parameter has exactly one candidate. This is what RuleSet.runAsync, RuleFlow.runAsync and a RuleFlow’s asyncRun step do before submitting work — see Async Execution.

Two related details. withLocalBindings() is public for custom execution strategies: it returns a per-run context over these very bindings that adds the run’s fresh LocalBindings and nothing else. And a context’s id is no longer a random UUID: getId() returns <jvm tag>-<creation millis>-<sequence>, each part in base 36, assigned at construction — so ids read in creation order. Derived contexts (asImmutable(), withLocalBindings()) share the id; a fork gets its own.