Skip to content

Core Concepts

rulii is built on three building blocks: Conditions, Actions and Bindings. Every rule combines them, following the Production Rule Model.

  • Condition. A question that returns true or false.
  • Action. Something that happens: a side effect.
  • Binding. A named, typed variable in shared state.
  1. PreCondition. Should this rule run at all? (optional)
  2. Condition (Given). IF this holds…
  3. Action (Then). …THEN do this.
  4. Otherwise. …ELSE do this instead (optional).
import static org.rulii.model.condition.Conditions.condition;
import static org.rulii.model.action.Actions.action;
// 1. A Condition: is the user old enough?
Condition isAdult = condition((Integer age) -> age >= 18);
// 2. Two Actions
Action allow = action(() -> System.out.println("Access granted"));
Action reject = action(() -> System.out.println("Too young"));
// 3. The Rule: IF adult THEN allow OTHERWISE reject
Rule rule = Rule.builder()
.name("AgeGate")
.given(isAdult)
.then(allow)
.otherwise(reject)
.build();
// 4. Wrap it in a RuleFlow, the recommended way to run rules
RuleFlow<RuleContext> flow = RuleFlow.builder()
.name("AgeGateFlow")
.param("age", Integer.class) // checked before the flow runs
.run(rule)
.build();
// 5. Run with age = 25
flow.run(age -> 25);
// Output: Access granted
// 6. Run with age = 15
flow.run(age -> 15);
// Output: Too young

The rule reads as a sentence: given the user is an adult, then allow access, otherwise reject. That is the pattern you will use throughout rulii.

As the logic grows, related rules are grouped into a RuleSet, and a RuleFlow orchestrates the run: a pipeline that composes rules, rule sets and other flows with conditionals, loops, exception handling and async execution. A rule or rule set can be run directly, but a flow is the recommended entry point from application code.