03 — Fundamentals

Core Concepts

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

The Three Building Blocks

Condition

A question that returns true or false

Action

Something that happens (a side-effect)

Binding

A named, typed variable in shared state

How a Rule Executes

1 PreCondition — Should this rule even run? (optional)
2 Condition (Given) — IF this is true...
3 Action (Then) — ...THEN do this
4 Otherwise — ...ELSE do this instead (optional)

Complete Example

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

// 1. Create a Condition: is the user old enough?
Condition isAdult = condition((Integer age) -> age >= 18);

// 2. Create Actions
Action allow  = action(() -> System.out.println("Access granted"));
Action reject = action(() -> System.out.println("Too young"));

// 3. Build the Rule: IF adult THEN allow OTHERWISE reject
Rule rule = Rule.builder()
    .name("AgeGate")
    .given(isAdult)
    .then(allow)
    .otherwise(reject)
    .build();

// 4. Run with bindings (age = 25)
rule.run(age -> 25);
// Output: Access granted

// 5. Run with different data (age = 15)
rule.run(age -> 15);
// Output: Too young

Notice how the rule reads almost like English: "Given the user is an adult, then allow access, otherwise reject." This is the core pattern you'll use throughout rulii.