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
Section titled “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") |
| Built-in validation rule | Validators.notNull(Validators.binding("name")).build() |
| Local Bindings (new in 2.2.0) | Bindings.builder().local() |
New Builder Methods in 2.1.0
Section titled “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 |
New Builder Methods in 2.2.0
Section titled “New Builder Methods in 2.2.0”2.2.0 adds formal result types to rule sets and flows, extra violation parameters and descriptions to validation rules, eleven new Validators entries, forks on bindings and contexts, a process-wide default scripting language, and category/tag listing on the registry.
| Method | Available On | Purpose |
|---|---|---|
resultType(Type) (new in 2.2.0) |
Rule set, rule flow | State the result type on its own — for a script extractor, or a flow whose result comes from exit steps. Checked once at build() against the extractor; read back with getDefinition().getResolvedResultType(). See RuleSets / RuleFlows |
resultExtractor(Class<T>, Function<T>) (new in 2.2.0) |
Rule set | Result extractor plus an explicit result type; the rule-set counterpart of the flow’s returning(Class, Function) |
param(name, value) / params(Map) (new in 2.2.0) |
Value validation rule | Extra parameters every violation reports, after the validated value and before the rule’s own — for message templates. See Custom Validation Rules |
describe() (new in 2.2.0) |
Value validation rule (builder and rule) | What the rule checks, naming the value: code size must be between 3 and 20. The default description when none is set |
Bindings.builder().local() (new in 2.2.0) |
Bindings | A LocalBindings instance; the engine creates one per run for a rule method declaring the parameter. See Building Rules (Declarative) |
uuid(fn), uniqueElements(fn), normalized(fn), codePointLength(fn, min, max), durationMin(fn, Duration), durationMax(fn, Duration), luhnCheck(fn), mod10Check(fn), mod11Check(fn), ean(fn), isbn(fn) (new in 2.2.0) |
Validators |
Eleven new validation rules, each with its own builder. See Built-in Validation Rules |
anyDomain(), pattern(String), flags(int) (new in 2.2.0) |
EmailValidationRuleBuilder |
Judge the domain part by shape alone (no TLD registry); require the whole address to match a regular expression |
protocol(String), host(String), port(int), regexp(String), flags(int) (new in 2.2.0) |
UrlValidationRuleBuilder |
Parts the URL must have, and a regular expression it must also match |
fork() (new in 2.2.0) |
ScopedBindings, RuleContext |
An independent stack / context for another thread that shares the scopes present at the call. See Scoped Bindings and RuleContext |
setDefaultLanguage(name), getDefaultLanguage(), clearDefaultLanguage(expected), getAvailableLanguages() (new in 2.2.0) |
ScriptProcessorManager |
The language a script is compiled in when none is named (also the system property rulii.script.defaultLanguage), and the languages available right now. See Scripting Support |
getByCategory(category), getByTags(tags...), getCategories(), getTags() (new in 2.2.0) |
RuleRegistry |
List a registry by the categories and tags its runnables declare. Default methods, so custom registries get them for free. See Introspection & Metadata |
Static Helper Imports
Section titled “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
Section titled “Usage Examples”condition() — Creates a Condition
Section titled “condition() — Creates a Condition”// No parameterscondition(() -> 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
Section titled “action() — Creates an Action”// No parametersaction(() -> System.out.println("Done!"))
// Modify a binding valueaction((Binding<Integer> count) -> count.setValue(count.getValue() + 1))
// Add new bindingsaction((Bindings bindings) -> bindings.bind("result", 42))function() — Creates a Function (returns a value)
Section titled “function() — Creates a Function (returns a value)”// Return a constantfunction(() -> "hello world!")
// Return a computed value from bindingsfunction((String firstName, String lastName) -> firstName + " " + lastName)Built-in Constants
Section titled “Built-in Constants”// A condition that always returns trueConditions.TRUE()
// An action that does nothingActions.EMPTY_ACTION()Cheat Sheet
Section titled “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)