Skip to content

Building Rules (Functional)

Lambda rules are the quickest way to define rules in rulii. You write conditions and actions as Java lambdas, and the framework matches parameter names to bindings automatically.

import static org.rulii.model.condition.Conditions.condition;
import static org.rulii.model.action.Actions.action;
Rule rule = Rule.builder()
.name("AlwaysTrue")
.given(condition(() -> true))
.then(action(() -> System.out.println("Fired!")))
.build();

Parameter names in the lambda must match binding names:

Rule rule = Rule.builder()
.name("AgeCheck")
.given(condition((Integer age) -> age >= 18))
.then(action(() -> System.out.println("Welcome!")))
.build();
rule.run(age -> 25); // Output: Welcome!

The names of a lambda’s parameters are read from the debug information in the class file, which Maven keeps by default. A build that strips it needs a different approach: see Keep parameter names in the class file.

Rule rule = Rule.builder()
.name("MultiAction")
.given(condition((Integer score) -> score > 90))
.then(action(() -> System.out.println("Excellent!")))
.then(action((Binding<String> grade) -> grade.setValue("A")))
.build();
Rule rule = Rule.builder()
.name("PassFail")
.given(condition((Integer score) -> score >= 60))
.then(action(() -> System.out.println("Pass")))
.otherwise(action(() -> System.out.println("Fail")))
.build();
rule.run(score -> 45); // Output: Fail

A pre-condition decides whether the rule should even attempt to run. If it returns false, the rule is skipped entirely:

Rule rule = Rule.builder()
.name("ConditionalRule")
.preCondition(condition((Boolean featureEnabled) -> featureEnabled))
.given(condition((Integer age) -> age >= 18))
.then(action(() -> System.out.println("Running!")))
.build();
// Pre-condition false → rule is SKIPPED (not FAIL)
rule.run(featureEnabled -> false, age -> 25);
// With lambda bindings
rule.run(name -> "Alice", age -> 30);
// With a Bindings object
Bindings b = Bindings.builder().standard();
b.bind("name", "Alice");
b.bind("age", 30);
rule.run(b);
// With a RuleContext
RuleContext ctx = RuleContext.builder().build(b);
RuleResult result = rule.run(ctx);
// Preferred in application code: run through a RuleFlow
RuleFlow<RuleContext> flow = RuleFlow.builder()
.name("MyFlow")
.run(rule)
.build();
flow.run(name -> "Alice", age -> 30);
import static org.rulii.model.condition.Conditions.condition;
import static org.rulii.model.action.Actions.action;
Rule dateRule = Rule.builder()
.name("DateConsistency")
.given(condition((LocalDate startDate, LocalDate endDate)
-> startDate != null && endDate != null && !endDate.isBefore(startDate)))
.then(action(() -> System.out.println("Dates are valid")))
.otherwise(action(() -> System.out.println("End date must be after start date")))
.build();
dateRule.run(
startDate -> LocalDate.of(2025, 1, 1),
endDate -> LocalDate.of(2025, 12, 31)
);
// Output: Dates are valid
Rule rule = Rule.builder()
.name("MyLambdaRule", "Rule demonstrating ParameterDefinitionEditor usage")
.given(
Condition.builder()
.with((List<String> value, Integer threshold) -> {
return value != null && value.size() > threshold;
})
// Override param 0: type
.param(0)
.type(new TypeReference<List<String>>() {})
.description("The input text value to validate")
.build()
// Override param 1: type, description, and default value
.param(1)
.description("The minimum length threshold")
.defaultValueText("3")
.build()
.build())
.build();