Skip to content

rulii 2.3.0 and rulii-spring 2.3.0

rulii 2.3.0 and rulii-spring 2.3.0 are on Maven Central, alongside rulii-bean-validation 1.1.0, rulii-explorer 1.2.0 and rulii-bom 2.3.0. This post covers what changed in the core engine and the Spring integration. Several of the changes alter behaviour; they are listed together at the end. The full lists are in the rulii changelog and the rulii-spring changelog.

Most of the work in 2.3.0 went into what the engine costs around your rules. On one thread it runs about 8 million rules a second where 2.2.0 ran 1.4 million, and a run allocates about a seventh of the memory. What rules accept, reject and report is the same, and there is nothing to change in your code to get it.

Runs per second, one thread 2.2.0 2.3.0
A rule: a condition and an action 1.4 million 8.1 million
A rule set of twenty rules 57,000 329,000
A rule set of ten built-in validators 38,000 215,000
A rule flow of five steps 272,000 1.5 million
A new RuleContext 766,000 2.6 million

Measured with JMH on one machine, each release from Maven Central. These measure the engine’s own overhead: the rules do almost nothing, so in an application the work inside the rules (a database call, a calculation) usually dominates.

Optimizing rulii has the full table, where the time was going and what changed.

Every JavaScript evaluation runs in a JavaScript context of its own, created for it and closed after it. Creating the context is nearly all of what the evaluation costs, so a simple condition runs about 7,900 times a second on one thread, whatever the script says. A GraalJsScriptProcessorFactory given GraalJsPoolOptions, or any GraalJS factory when -Drulii.script.graaljs.reuseContexts=true, keeps a pool of contexts instead, and the same condition runs about 160,000 times a second.

ScriptProcessorManager.getInstance().register(
new GraalJsScriptProcessorFactory("js", "ctx", GraalJsPoolOptions.DEFAULT));

It is opt-in. With reuse on, a script gives what it gives without it: what it declares ends with it, and a global it leaves behind is removed before the context is used again. The one thing not undone is a change made inside a built-in (String.prototype.shout = ...), which later scripts in that context see until it is retired. That is why reuse is not the default. See Scripting Support, which also has a note on doing the same for another script engine.

A flow-level exception handler lets the flow continue

Section titled “A flow-level exception handler lets the flow continue”

RuleFlow.builder().onException(type, body) now recovers the failed step and the flow continues with the next top-level step, as its Javadoc always said. Up to 2.2.0 the handler body ran and the flow then ended, skipping every remaining step, while a step-level handler let the flow carry on.

A failure inside a when, forEach or scope leaves that container first: the rest of the container is abandoned and the flow resumes with the step after it. To keep the old behaviour, end the handler body with exit(). A failure the handler’s type does not match, and a failure of the handler body, still propagate, and the finalizer still runs. See Exception Handling.

The default value of an optional input parameter of a rule flow or rule set (param(name, type, defaultValue)) is now bound in the flow’s or rule set’s own scope and goes with it. Up to 2.2.0 it was bound in the caller’s scope, so it stayed behind after the run, and the next run in the same context found the parameter bound and did not evaluate the default again. A default meant to be fresh on every run (a new collector, a timestamp) was computed once.

Now every run evaluates its defaults, and the caller’s bindings are untouched. Code that read a defaulted input from the context after the run must bind the value itself first, or return it with returning(...) or resultExtractor(...).

A context built over another context’s bindings

Section titled “A context built over another context’s bindings”

RuleContext.builder().build(other.getBindings()), and with it rule.run(bindings), ruleSet.run(bindings) or flow.run(bindings) called from an action with the bindings it was handed, builds a new context over another context’s bindings. Those bindings carry the other context’s reserved ruleContext and bindings, and up to 2.2.0 they were found before the new context’s own. A rule in the new context that asked for its RuleContext received the older one, which knows nothing of the scopes pushed on the new one.

In 2.3.0 the new context resolves ruleContext and bindings to itself. Everything else in the given bindings passes through as before, binds included, so a rule run this way still binds where the caller reads.

  • ValueValidationRule.check(ruleContext, value) decides a value the caller already holds: PASS, FAIL or SKIPPED, with the null handling and the supported-type test a run of the rule applies, and nothing else. buildViolation(ruleContext, value) builds the violation the rule reports. They are for callers that run many validators against values they have already read; rulii-bean-validation 1.1.0 is the first.
  • The decimal validator no longer accepts leading or trailing spaces unless asked to with allowSpace(). Its builder started with the setting on, so the strict check could not be had.

Bean Validation on rulii is opt-in. rulii-bean-validation is now an optional dependency of rulii-spring and no longer comes with it. Add org.rulii:rulii-bean-validation and rulii becomes the application’s Validator, in place of Hibernate Validator; adding it is the switch. rulii.validation.enabled=false turns it off again while the dependency stays. In 2.2.0 the library came with rulii-spring and was on by default, which meant adding rulii-spring for its rules also changed how an application validated. That is now a decision the application makes.

JPA is not affected. Spring Boot does not hand its Validator to JPA; Hibernate builds one of its own from the first provider on the classpath. While Hibernate Validator is on the classpath, JPA goes on validating entities with it, whatever the dependency order. See Bean Validation in Spring Boot.

All 48 built-in validators are XML elements. The eleven validators added in rulii 2.2.0 have elements now, and every setting of a validator’s builder is an optional attribute named after the builder method:

<r:uuid binding="orderId" versions="4" letterCase="INSENSITIVE"/>
<r:durationMax binding="session" max="PT8H" inclusive="false"/>
<r:pattern binding="notes" pattern="a.b" flags="DOTALL,CASE_INSENSITIVE"/>

See XML Rule Definitions.

Jakarta EL as ${...}. rulii-spring resolves Spring property placeholders in script text before it is compiled, and in 2.2.0 that included Jakarta EL, so an @Expr written as ${...} failed with a PlaceholderResolutionException. From 2.3.0 a scripting language can opt out of the resolver, and Jakarta EL does: ${...} and #{...} both work.

  • rulii-bean-validation 1.1.0 requires rulii 2.3.0. Predefined constraints are checked directly, and a class with nothing else needs no engine around it: a bean of ten satisfied constraints validates about 640,000 times a second where 1.0.0 managed 34,000.
  • rulii-explorer 1.2.0 requires rulii 2.3.0 and rulii-spring 2.3.0. It starts in an application on WebFlux without Spring MVC, a start-up warning is gone, and its Beans section needs rulii-bean-validation in the application, now that the library is opt-in.
  • rulii-bom 2.3.0 pins all four at these versions.
  • rulii-spring: rulii-bean-validation no longer comes with rulii-spring. Upgrading from 2.2: add the dependency to keep validating with rulii.
  • Rule flows: a flow-level exception handler recovers the failed step and the flow continues. End the handler body with exit() (<r:exit/> in XML) to stop the flow as before.
  • Rule flows and rule sets: the default of an optional input parameter is bound in the run’s own scope and is not left in the caller’s bindings.
  • Rule context: a context built over another context’s bindings resolves ruleContext and bindings to itself. A binding made through them inside a rule set or flow run this way lands in that rule set’s or flow’s scope and goes with it.
  • Validation: decimal rejects surrounding spaces unless allowSpace() is set; <r:decimal> and rulii-bean-validation’s @Decimal follow.
  • Scripting: a Spring property placeholder inside a jakarta-el script is no longer resolved; pass the value in as a binding.
  • org.rulii.spring.validation.BeanValidationOptOutConfig is renamed HibernateValidatorConfig.