Skip to content

Overview

rulii is a business rule engine written in Java. It defines, groups and runs business rules outside the hard-coded paths of the application, so a rule can change without the code around it changing, and can be tested on its own.

Concept Purpose / Description
Rule One business rule: a condition (given) and an action (then), with an optional pre-condition and an otherwise action.
RuleSet A group of rules run together, in order, with an optional stop condition and a result.
RuleFlow A pipeline that orchestrates rules, rule sets and other flows, with conditionals, loops, scopes, exception handling and async execution. The recommended way to run rules (since 2.0.0).
Bindings The named, typed values the rules read and write. Declared when a rule runs, or bound by rule actions along the way.
RuleContext Everything a run needs: the bindings, the options (converters, message resolver, tracer, object factory) and the trace of what ran.

1. Always use the Builder when creating objects

Section titled “1. Always use the Builder when creating objects”

Most rulii interfaces provide a static builder() method that you can use instead of creating objects directly.

Rule rule = Rule.builder()...build();
RuleSet ruleSet = RuleSet.builder()...build();
RuleFlow<?> flow = RuleFlow.builder()...build();
Binding binding = Binding.builder()...build();

The builders validate what they are given and set sensible defaults; the constructors do not.

To run a rule or rule set from application code, wrap it in a RuleFlow rather than calling run() on it directly. A flow validates its inputs, wires results between steps, and gives you conditionals, loops, exception handling, tracing and async execution declaratively, so orchestration stays out of your application code.

// Instead of: ruleSet.run(bindings);
RuleFlow<RuleContext> flow = RuleFlow.builder()
.name("MyFlow")
.run(ruleSet)
.build();
flow.run(age -> 25, score -> 75);

A rule is reused across many runs and may run on several threads at once. Do not keep or change state inside it. Anything a rule needs comes in through bindings, which map values from the context onto the method’s parameters, so the state lives outside the rule.

A rule should read only what it needs. Pass in a large object that carries many unrelated properties and it is no longer clear which of them the rule depends on.

This has two downsides:

  1. Hard to understand. You cannot see at a glance which inputs matter to the rule.
  2. Hard to test. A unit test has to populate the whole object, even when the rule reads one or two fields.

Good (narrow bindings):

Rule isAdult = Rule.builder()
.name("isAdultRule")
.given(condition((Integer age) -> age >= 18))
.then(action((Binding<Boolean> eligible) -> eligible.setValue(true)))
.build();

Bad (one large object):

Rule isAdult = Rule.builder()
.name("isAdultRule")
.given(condition((Person person) -> person.getAge() >= 18))
.then(action((Person person) -> person.setEligible(true)))
.build();
  1. Declarative (class-based). A dedicated class for each rule.
  2. Functional (lambda-based). Rules defined inline with lambdas.

Both approaches are valid, but they have different trade-offs.

@Rule
@Description("Anyone 18 or over is eligible")
public class IsAdultRule {
@Given
public boolean isAdult(Integer age) {
return age >= 18;
}
@Then
public void markEligible(Binding<Boolean> eligible) {
eligible.setValue(true);
}
}

Advantages

  • Clear separation: each rule lives in its own file.
  • Metadata is easy to attach: name, description, category, tags.
  • Explicit, which suits large teams and projects with many rules.
  • Easy to reuse and to unit test on its own.

Disadvantages

  • More code: a class per rule.
  • Harder to see the overall flow when many rules are involved.
Rule isAdult = Rule.builder()
.name("isAdultRule")
.given(condition((Integer age) -> age >= 18))
.then(action((Binding<Boolean> eligible) -> eligible.setValue(true)))
.build();

Advantages

  • Concise: rules defined inline, no extra files.
  • Easier to see the overall flow when many rules are involved.
  • Suits small projects and simple validation rules.

Disadvantages

  • Gets hard to read once a rule grows.
  • Less room for metadata: a name and a description, but no category or tags on a lambda.
  • Parameter annotations such as @Param need a method, so a lambda cannot use them.

Use Declarative (Class-based) Rules when:

  • Rules are complex and need clear structure.
  • Several people maintain them and clarity and reuse matter.
  • The rules need metadata or documentation.

Use Functional (Lambda-based) Rules when:

  • Rules are simple and straightforward.
  • A small team or one person writes them and speed matters.
  • They are unlikely to change often.

Most projects mix the two: declarative rules for the complex logic, functional rules for simple validations.

A rule that assumes its parameters are never null, or always in range, can only be used where that holds. Declare the assumption as a preCondition instead. It acts as a guard: the rule does not run unless it is met.

Good (using preCondition):

Rule isAdult = Rule.builder()
.name("isAdultRule")
// only run when age is present
.preCondition(condition((Integer age) -> age != null))
.given(condition((Integer age) -> age >= 18))
.then(action((Binding<Boolean> eligible) -> eligible.setValue(true)))
.build();

Bad (assuming parameters):

Rule isAdult = Rule.builder()
.name("isAdultRule")
// assumes age is never null
.given(condition((Integer age) -> age >= 18))
.then(action((Binding<Boolean> eligible) -> eligible.setValue(true)))
.build();

If age is missing or null, the second rule throws.

A pre-condition makes the rule reusable in more contexts, keeps null pointer exceptions out, and documents what the rule expects.