What is DROOLS?
DROOLS is a Rules Engine supported by JBoss and Red Hat, It executes statements that might modify the state of the objects, depending on which rules satisfy the input. It is very powerful tool that we can use to change the state of an object by specifying various condition based statement through which the object pass and a refined and cleaned object is achieved. Drools is a best-of-breed open source Rules Engine which also offers Complex Event Processing.
Within Mule, input is fed to the rules engine from within a flow, and the rules execute to modify the state of that input. Below is the sample of one such flow. We will be covering more about it how to achieve Drools implementation in Mule ESB.
Considerations
Before we start I would like to mention that using drools should only be considered,If the business logic of your application is complex, If the business logic for your Mule application is relatively simple, First consideration should be given to implementing it using Mule’s orchestration functionality such as flows, routers, and custom components.
- The business logic is overly complex or time-consuming when defined procedurally
- The business logic contains a lot of if-then-else statements
- The business logic changes often
- 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)
Let’s Get Started
Business Case
Today we will be implementing an business case where based on Customer’s purchase history we will be giving him a grade or rating, There are three main logic of our business case.
- If purchase is greater than equal to 10000 than Platinum customer
- If purchase is less than 10000 but greater than equal to 5000 Gold customer
- If purchase is less than 5000 than Silver
It is a very simple business case but it will give us a good idea how to implement it in Mule. Our mule flow will be receiving a JSON data and will use that JSON data object to pass through our rules statement in rules engine.
{
"name": "Harish Kumar",
"age":30,
"purchase":19000
}
A video tutorial of same implementation and explanation is available at Drools in Mule ESB.
Create a mule project named droolsdemo , you can choose any runtime starting from 3.6
Namespace
Before we start writing let’s add the namespace and schema location to our mule config file droolsdemo.xml,
<mule xmlns:bpm="http://www.mulesoft.org/schema/mule/bpm" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/bpm http://www.mulesoft.org/schema/mule/bpm/3.7/mule-bpm.xsd"> </mule>
Initialisation of BPM
Add below tag to your droolsdemo.xml, this tag will tell mule that you will be using Rules Engine and will startup Drools’ working memory and all your rules and keep them ready to be called
<bpm:drools />
Mule Flow
As explained earlier we will be getting a JSON object and we will convert it into a Java object using mule JSON to Object transformer. Our java class represent the customer object and looks like below
package com.drools.demo;
import java.math.BigDecimal;
public class Customer {
private String name;
private int age;
private BigDecimal purchase;
private String customerType;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public BigDecimal getPurchase() {
return purchase;
}
public void setPurchase(BigDecimal purchase) {
this.purchase = purchase;
}
public String getCustomerType() {
return customerType;
}
public void setCustomerType(String customerType) {
this.customerType = customerType;
}
}
Rule Definiton Statements
In drools we write our logic in a file with the extension of .drl , This file contains your business logic in plan text format that could be easily changed and loaded at runtime if the need be. This file should be available at your project classpath to be loaded by mule at project startup.
package com.drools.demo
import org.mule.MessageExchangePattern;
import com.drools.demo.Customer;
global org.mule.module.bpm.MessageService mule;
dialect "mvel"
declare Customer
@role('event')
end
rule "Decide Platinum Customer"
lock-on-active
when
$customer : Customer(purchase >= 10000)
then
modify($customer) {setCustomerType("Platinum")}
end
rule "Decide Gold Customer"
lock-on-active
when
$customer : Customer(purchase >= 5000 && purchase < 10000)
then
modify($customer) {setCustomerType("Gold")}
end
rule "Decide Silver Customer"
lock-on-active
when
$customer : Customer(purchase< 5000)
then
modify($customer) {setCustomerType("Silver")}
end
In the starting part of this rule file we define the general things like imports and package. Than we create a object of our customer to be used by our rule statements and then comes the juicy part of RULES, what we are saying here is a when condition based on which if it passes the object is changed with a new state.
Usage of drl in flow
<bpm:rules rulesDefinition="customerGroup.drl" initialFacts-ref="NoFactsBean" doc:name="BPM Rules" />
here we use the drl file that we created and implement the rules in middle of our flow.
Complete flow
So now we are done with the setting of flow the complete droolsdemo.xml will looks like below.
<?xml version="1.0" encoding="UTF-8"?>
<mule ...>
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration" />
<bpm:drools/>
<spring:beans>
<spring:bean name="NoFactsBean" class="java.util.ArrayList"/>
</spring:beans>
<flow name="droolsdemoFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/brtest" allowedMethods="POST" doc:name="HTTP" />
<object-to-string-transformer doc:name="Object to String" />
<json:json-to-object-transformer returnClass="com.drools.demo.Customer" doc:name="JSON to Object" />
<bpm:rules rulesDefinition="customerGroup.drl" initialFacts-ref="NoFactsBean" doc:name="BPM Rules" />
<set-payload value="#[payload.object]" doc:name="Set Payload"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
</flow>
</mule>
Testing
We will be sending three different JSON input to our flow inbound http endpoint, and hopefully will get the correct rules fired and proper grade back for our customer.
-
Purchase above 10000

-
Between 5000 and 10000

-
Below 5000

So we can see for all three cases the correct rule was applied and a proper grading or customer type was given to our Customer object.
Video
I have created the same implementation in a youtube video that you can watch at this link https://www.youtube.com/watch?v=1OgzLdEayLw
References
More details about Drools in Mule is available at https://docs.mulesoft.com/mule-user-guide/v/3.7/drools-module-reference
I hope that you found this blog interesting and that it helps you with your integration of Mule and JBoss Drools. If you liked this blog please comment and follow this site and if you liked the video of tutorial please share and subscribe it will encourage me to write and create more awesome stuff and blogs.
