Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Simple Rule Engine Updated

DZone's Guide to

Simple Rule Engine Updated

· DevOps Zone
Free Resource

Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

I have taken the time to upgrade my simple Java rule engine so that it supports Java 8 lambdas and streams and it is now published in Maven Central. The code is now also available on GitHub.

First off, the Maven dependencies. The following dependency is for the basic rule engine which is compatible with Java 6.

<dependency>
<groupId>ch.maxant</groupId>
<artifactId>rules</artifactId>
<version>2.1.0</version>
</dependency>

If you want to use lambdas from Java 8, then you need to also add a dependency as follows:





<dependency>
<groupId>ch.maxant</groupId>
<artifactId>rules-java8</artifactId>
<version>2.1.0</version>
</dependency>

This allows you to write code like that found on lines 15 and 20.

Rule rule1 =newRule("R1",
"input.p1.name == \"ant\" && input.p2.name == \"clare\"",
"outcome1",
0,
"ch.maxant.produits",
"Règle spéciale pour famille Kutschera");
Rule rule2 =newRule("R2","true","outcome2",1,
"ch.maxant.produits","Régle par défault");
List<Rule> rules =Arrays.asList(rule1, rule2);
//to use a lambda, construct a SamAction and pass it a lambda.
IAction<MyInput,BigDecimal> action1 =
newSamAction<MyInput,BigDecimal>(
"outcome1",
 i ->newBigDecimal("100.0")
);
IAction<MyInput,BigDecimal> action2 =
newSamAction<MyInput,BigDecimal>(
"outcome2",
 i ->newBigDecimal("101.0")
);
List<IAction<MyInput,BigDecimal>> actions =
Arrays.asList(action1, action2);
Engine e =newEngine(rules,true);
MyInput input =newMyInput();
Person p1 =newPerson("ant");
Person p2 =newPerson("clare");
input.setP1(p1);
input.setP2(p2);
BigDecimal price = e.executeBestAction(input, actions);
assertEquals(newBigDecimal("101.0"), price);

If you want to pass a Stream to the Engine rather than a Collection, then use the sub-class found in the second library, for example:





Stream<Rule> streamOfRules = getStreamOfRules();
//to pass in a stream, we need to use a different Engine
Java8Engine e =newJava8Engine(streamOfRules,true);
//use this engine as you would the normal Engine

See the tests on GitHub for more details. In theory, you can pass in a parallel stream - I haven't tried it but would be interested to hear about your successes / failures.

Finally, if you are using Scala, there is still support for that too, in theScalaEngine which can be found in the following dependency. See the tests onGitHub for more details.





<dependency>
<groupId>ch.maxant</groupId>
<artifactId>rules-scala</artifactId>
<version>2.1.0</version>
</dependency>

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs

Topics:

Published at DZone with permission of Ant Kutschera, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}