A new version of the Simple Rule Engine is available, so that you can now use JavaScript (Nashorn) for writing your rules (MVEL is still supported because it is so fast!).
New Features:
- JavaScript based Rule Engine – Use the
JavascriptEngine
constructor to create a subclass ofEngine
which is capable of interpreting JavaScript rules. It uses Nashorn (Java 8) as a JavaScript engine for evaluating the textual rules. Additionally, you can load scripts, for example lodash, so that your rules can be very complex. See the
testRuleWithIterationUsingLibrary() and testComplexRuleInLibrary() and testLoadScriptRatherThanFile()
tests for examples. Nashorn isn’t threadsafe, but the rule engine is! Internally it uses a pool of Nashorn engines. You can also override the pool configuration
if you need to. See the
testMultithreadingAndPerformance_NoProblemsExpectedBecauseScriptsAreStateless()
tests for examples. If required, you can get the engine to preload the pool, or leave it lazily fill the pool (default). Please note, the engine is not completely Rhino (Java 6 / Java 7) compatible – the multithreaded tests do not work as expected for stateful scripts, but the performance of Rhino is so bad that you won’t want to use it anyway.
and testMultithreadingStatefulRules_NoProblemsExpectedBecauseOfEnginePool()
- You can now override the name of the input parameter – previous versions required that the rules refer to the input as “input”, for example “input.people[0].name == ‘Jane'”. You can now provide the engine with the name which should be used, so that you can create rules like
“company.people[0].name == ‘Jane'”. - Java 8 Javascript Rule Engine – If you want to use Java 8 lambdas, then you instantiate a
Java8JavascriptEngine
rather than the more plainJavascriptEngine
. - For your convenience, there are now builders for the
JavascriptEngine
andJava8JavascriptEngine
, because their constructors have so many parameters. See thetestBuilder()
test for an example. - Javascript rules can refer to input using bean notation (e.g. “
input.people[0].name
“)
or Java notation (e.g. “input.getPeople().get(0).getName()
“).
The library is available from Maven Central:
<dependency>
<groupId>ch.maxant</groupId>
<artifactId>rules</artifactId>
<version>2.2.0</version>
</dependency>
Have fun!
Copyright ©2015, Ant Kutschera