DROOLS is an open source Business Logic Integration platform (BLiP) and rules engine, written in Java, with an Eclipse IDE plugin for core development.
You can avoid a lot of if-else from your actual business logic by separating or implementing business rules separately.
Easy to implement complex logic using the Drools Rules Engine.
Let consider that Drools should be used when your business logic is very complex. Apart from complex business logic, you can use Drools in the below situations:
Application business logic often changes.
The business logic is overly complex or time-consuming when defined procedurally.
The business logic needs to be maintained by people who don’ t (or shouldn’ t) have access to the application itself (to recompile/redeploy it) .
Domain experts (or business analysts) are readily available, but are nontechnical.
Today we will implement a business use case such as a life insurance policy; depending on age and policy type, we will be providing a premium amount. Below is the logic for our business case:
If age 40 and policyType = «Policy1», then premiumAmount = 40000.
If age 40 and policyType = «Policy2», then premiumAmount = 41000.
Our flow will receive the below JSON message and we will use JSON data to pass through our rules engine.
Once the JSON message has passed through the rules engine and our expected output is shown below:
Now, we will walk through how to integrate the Mule ESB and Java rules engine, namely Drools.
Create a Mule project and name the project droolsexample.xml; you can select Mule runtime 3.6 or higher.
The first thing we need to add is the namespace and schema location to the Mule configuration file (e.g. droolsexample.xml) .
Add the below tag in droolsdemo.xml and it will tell the Mule flow there are rules defined in your flow and it will start Drool’s working memory.
We will be receiving the JSON message and it will be converted into a Java object using the JSON To Object transformer. Our Java class for Insurance will look like this:
In the above rule file, we have defined import namespaces and packages statements. Then, we create the Insurance object to be used by our rule engine and followed by some rules with conditions that explained above.
You need to define spring beans in your configuration file (e.g. droolsexample.xml) as shown below
Rule defintions can be called in Mule Flow by defining the below statement in the configuration file (droolsexample.xml) :
We will use Postman to test all the above scenarios, and hopefully, we will get the expected result back.
Mule provides easy steps to integrate with DROOLS, as explained in above article. I hope you find this article is interesting and provides the required information to integrate DROOLS with Mule Flow.