13 — Reference

Builder Pattern Reference

Almost everything in rulii is built using the builder pattern. This page is a quick reference for all builders and the static helper methods you'll use most often.

All Builders at a Glance

What You're Building How to Start
Bindings container Bindings.builder().standard()
Scoped Bindings Bindings.builder().scoped()
Individual Binding Binding.builder().with("name").value("Alice").build()
RuleContext RuleContext.builder().build(bindings)
Condition condition((x) -> ...)
Action action(() -> ...)
Rule (lambda) Rule.builder().name("...").given(...).then(...).build()
Rule (from class) Rule.builder().build(MyRule.class)
Rule (from instance) Rule.builder().build(new MyValidationRule("field"))
RuleSet RuleSet.builder().with("Name").rule(...).build()
RuleFlow (new in 2.0.0) RuleFlow.builder().name("Name").run(...).build()
MessageFormatter MessageFormatter.builder().build()
MessageResolver MessageResolver.builder("baseName").build()
RuleViolation RuleViolation.builder().build("rule", "code", "message")

New Builder Methods in 2.1.0

2.1.0 adds descriptive metadata methods across the builders. None of them affect execution — they land on the built definition for tools that read it (see Introspection & Metadata).

Method Available On Purpose
category(String) (new in 2.1.0) Rule, validation rule, value validation rule, rule set, rule flow At most one descriptive category; / separates hierarchy levels ("Pricing/Discounts")
tags(String...) / tags(Collection) (new in 2.1.0) Rule, validation rule, value validation rule, rule set, rule flow Any number of descriptive tags; tags(...) adds to what was given before
source(SourceDefinition) (new in 2.1.0) Rule, validation rule, value validation rule, rule set, rule flow Explicit source for loaders that know where an artifact was declared; create with SourceDefinition.forFile(fileName, line) or forClass(type)
param(name, type, required, description) / param(name, type, defaultValue, description) (new in 2.1.0) Rule set, rule flow Declare an input parameter with a description, recorded on the definition's InputParameter
returning(Class<T>, Function<T>) (new in 2.1.0) Rule flow Result extractor plus an explicit result type, for when the extractor's declared return type isn't specific enough

Static Helper Imports

These static imports let you write concise lambda-based conditions, actions, and functions:

import static org.rulii.model.condition.Conditions.condition;
import static org.rulii.model.action.Actions.action;
import static org.rulii.model.function.Functions.function;

Usage Examples

condition() — Creates a Condition

// No parameters
condition(() -> true)

// With parameters (matched by name to bindings)
condition((String name) -> name != null)
condition((Integer age, String status) -> age >= 18 && "active".equals(status))

action() — Creates an Action

// No parameters
action(() -> System.out.println("Done!"))

// Modify a binding value
action((Binding<Integer> count) -> count.setValue(count.getValue() + 1))

// Add new bindings
action((Bindings bindings) -> bindings.bind("result", 42))

function() — Creates a Function (returns a value)

// Return a constant
function(() -> "hello world!")

// Return a computed value from bindings
function((String firstName, String lastName) -> firstName + " " + lastName)

Built-in Constants

// A condition that always returns true
Conditions.TRUE()

// An action that does nothing
Actions.EMPTY_ACTION()

Cheat Sheet

// Quick rule with lambda

Rule.builder().name("R1").given(condition(...)).then(action(...)).build()

// Quick rule from annotated class

Rule.builder().build(MyRule.class)

// Quick validation rule

Rule.builder().build(new NotNullValidationRule("field"))

// Quick rule set

RuleSet.builder().with("Name").rule(r1).rule(r2).build()

// Quick rule flow (the recommended way to run rules)

RuleFlow.builder().name("Flow").run(ruleSet).build()

// Quick run with lambda bindings

flow.run(name -> "Alice", age -> 30)