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.
Creating a RuleContext
Section titled “Creating a RuleContext”// From bindingsBindings bindings = Bindings.builder().standard();bindings.bind("name", "Alice");
RuleContext ctx = RuleContext.builder().build(bindings);
// From lambda declarationsRuleContext ctx2 = RuleContext.builder().build(name -> "Alice", age -> 30);What’s Inside a RuleContext?
Section titled “What’s Inside a RuleContext?”| 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 |
When Do You Need a RuleContext?
Section titled “When Do You Need a RuleContext?”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 contextRuleContext ctx = RuleContext.builder().build(name -> "Alice");RuleResult result = rule.run(ctx);Accessing Context Inside a Rule
Section titled “Accessing Context Inside a Rule”In annotated rules, you can receive the RuleContext as a parameter:
@Givenpublic boolean when(RuleContext context) { Bindings bindings = context.getBindings(); return bindings.contains("name");}Forking a RuleContext (new in 2.2.0)
Section titled “Forking a RuleContext (new in 2.2.0)”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-offRuleContext fork = ctx.fork();Future<RuleSetExecutionStatus> future = executor.submit(() -> ruleSet.run(fork));
// Keep using ctx meanwhile: the rule set's scopes stay private to the forkInside 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.