Scoped Bindings
ScopedBindings create a stack of named scopes, each with its own set of bindings. Scopes are layered so that bindings are resolved from the current scope upward through parent scopes. This is ideal for isolating rule execution contexts and managing request-scoped variables.
Creating Scoped Bindings
Section titled “Creating Scoped Bindings”ScopedBindings scoped = (ScopedBindings) Bindings.builder().scoped();Scope Layering
Section titled “Scope Layering”Scopes are layered in a stack: root at the bottom, then global, then any user-created scopes on top. When resolving a binding, rulii searches from the current (topmost) scope downward until it finds a match.
- ROOT_SCOPE The bottom-most scope, always present
- GLOBAL_SCOPE Above root, for shared bindings
- User Scopes Added on top via addScope()
Working with Scopes
Section titled “Working with Scopes”ScopedBindings scoped = (ScopedBindings) Bindings.builder().scoped();
// Add bindings to root scopescoped.bind("appName", "MyApp");
// Create a child scopeNamedScope childScope = scoped.addScope("request");scoped.bind("userId", 42);
// Both are accessiblescoped.getValue("appName"); // "MyApp" (from root)scoped.getValue("userId"); // 42 (from request scope)
// Remove the child scopescoped.removeScope();// "userId" is no longer accessible// "appName" is still thereKey Methods
Section titled “Key Methods”| Method | Description |
|---|---|
addScope(name) |
Push a new named scope onto the stack |
removeScope() |
Pop the current scope from the stack |
getCurrentScope() |
Get the topmost scope |
getParentScope() |
Get the scope below the current one |
getRootScope() |
Get the bottom-most root scope |
getGlobalScope() |
Get the global scope |
getScopeSize() |
Get the number of scopes in the stack |
containsScope(name) |
Check if a named scope exists |
fork() (new in 2.2.0) |
Create an independent stack that shares the scopes present right now (see below) |
Forking Scoped Bindings (new in 2.2.0)
Section titled “Forking Scoped Bindings (new in 2.2.0)”ScopedBindings.fork() creates a stack for work that proceeds independently of the caller — typically on another thread. The fork starts with every scope on the caller’s stack at that moment, by reference, plus a fresh scope of its own on top. Take the fork on the calling thread at the moment of the hand-off, so it captures the caller’s scopes as they stand then.
ScopedBindings scoped = (ScopedBindings) Bindings.builder().scoped();scoped.bind("total", 100);
// Fork on the calling thread, then hand it offScopedBindings fork = scoped.fork();executor.submit(() -> { fork.setValue("total", 150); // shared binding: the caller sees 150 fork.bind("scratch", "tmp"); // private: lands in the fork's own scope fork.addScope("inner"); // private: never visible to the caller});
scoped.addScope("later"); // never visible to the forkThe rules, in short:
- Values are shared. A value set through either stack on an inherited scope is seen by both, and a
Bindingthe caller adds into an inherited scope is visible to the fork. - Structure is private. The fork cannot see scopes the caller adds afterwards, keeps seeing inherited scopes the caller removes afterwards, and its own bindings and scopes are never visible to the caller.
- The fork owns its bottom. It cannot pop below its first own scope (
CannotRemoveRootScopeException, as for the root), and removing an inherited scope through the fork fails cleanly without popping anything (NoSuchScopeException/NoSuchBindingsException). - Reserved names are rebound.
ruleContextandbindingsdescribe the stack they were bound in, so they are not inherited; an inherited scope holding them is seen by the fork as the same scope minus those two. RuleContext.fork() rebinds its own. - Listeners. The caller’s listeners present at the fork are copied, so tracing continues into the fork; listeners added to the fork attach to its own scopes only.
- A fork of a fork works the same way.
ImmutableScopedBindings.fork()throws.
Use Cases
Section titled “Use Cases”Scoped bindings are particularly useful for:
- Isolating rule execution contexts so rules don’t interfere with each other
- Managing request-scoped variables in web applications
- Creating temporary binding layers that are discarded after use
- Sharing global configuration while allowing local overrides