A rule, and running it
This is the rule from README, where it is a compiled fixture — if this page and the engine disagree, trust README.
apiVersion: rules.v1
rules:
- id: high-value-order-review
salience: 10
noLoop: true
when:
- fact: Order
as: o
where:
total: { gt: 10000 }
status: { eq: "PENDING" }
- fact: Customer
as: c
where:
id: { eq: { $ref: o.customerId } }
riskTier: { in: ["HIGH", "MEDIUM"] }
then:
- action: setField
target: o
field: status
value: "REVIEW"
- action: emit
event: "order.flagged"
payload:
orderId: { $ref: o.id }
reason: "high value + risk tier"
Compiled once, at startup, and shared by everything. Then a session per unit of work — a request, a message, a batch:
// RuleSource.of(Path) reads the file, so it declares IOException.
CompiledRuleSet rules = RuleFiles.compile(RuleSource.of(Path.of("orders.yaml")));
ObjectMapper json = new ObjectMapper();
try (RuleSession session = rules.newSession()) {
session.insert("Order", json.readTree("""
{"id": 1, "total": 25000, "status": "PENDING", "customerId": 7}"""));
session.insert("Customer", json.readTree("""
{"id": 7, "riskTier": "HIGH"}"""));
FireResult result = session.fireAllRules();
// result.fired() -> high-value-order-review, once
// result.emitted() -> order.flagged, stamped with the session id and the rule-set version
// result.why() -> DRAINED
}
Nothing performs I/O by default — emitted events come back as the return value of the
fire call, so a rule set is testable with no mocking at all.