Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Integrating DROOLS With Mule ESB

Integrating DROOLS With Mule ESB

Integrating DROOLS With Mule ESB

Jitendra Bafna

June 18, 2017
Tweet

More Decks by Jitendra Bafna

Other Decks in Technology

Transcript

  1. Integrating DROOLS With Mule ESB Overview DROOLS is Business Logic

    integration platform (BLiP). It is written in Java. It is an open source that is backed by RedHat and JBoss. It is Business Rules Management System (BRMS) solution. It provides a core Business Rules Engine (BRE), a web authoring and rules management application (Drools Workbench) and an Eclipse IDE plugin for core development. It is one of the best open source rule engine provides Complex Event Processing. Facts are stored in working memory (like in-memory database), Fact is piece of data, such as salary=3000 or in object oriented rules engine, it may be java object with properties. Rules are defined in a knowledge base. A rule consists of conditions (which typically depend on facts in the working memory) and actions which are executed when the conditions are true (similar to an "if-then" statement).
  2. Integrating DROOLS With Mule ESB Advantages Of DROOLS Rules Engine

    • Reusability: The rules are kept at some centralized location (separation of business logic from rest of system), it is easy to reuse Drools rules engine. • Flexibility: Changing the application or its model is not easy task but it is very easy to change the rules than Java code. • Redeploying: It is easy to redeploy modified rules without stopping or modifying the application logic. • Easier To Understand: It is easier to understand the rules for new developers that code written in java or any other languages. • You can avoid lot of if-else from your actual business logic by separating or implementing business rules separately. • Unifying: The Drools platform brings unification of rules and processes. It is easy to call rules from a process or vice versa. • Easy to implement Complex logic using Drools Rules Engine.
  3. Integrating DROOLS With Mule ESB Considerations for Using Drools Rule

    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 below situations • Application business logic often change. • Business Logic contains lot of If-Else statements. • 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.
  4. Integrating DROOLS With Mule ESB Business Use Case Today we

    will going to implement business use case such as Life insurance policy, depending on age and policy type, we will be providing premium amount. Below is the logics for our business cases • If age < 10 and policyType = "Policy1", then premiumAmount = 20000. • If age is between 10 and 40 and policyType = "Policy1", then premiumAmount = 30000. • If age > 40 and policyType = "Policy1", then premiumAmount = 40000. • If age < 10 and policyType = "Policy2", then premiumAmount = 21000. • If age is between 10 and 40 and policyType = "Policy2", then premiumAmount = 32000. • If age > 40 and policyType = "Policy2", then premiumAmount = 43000. Our flow will receive below JSON message and we will use JSON data to pass through our rules engine.
  5. Integrating DROOLS With Mule ESB Once JSON message passed through

    Rules Engine and our expected output is shown below.
  6. Integrating DROOLS With Mule ESB Implementing DROOLS With Mule ESB

    Now, we will walkthrough how to integrate the Mule ESB and Java rules engine namely DROOLS. Create Mule project and named the project droolsexample.xml and you can select Mule runtime 3.6 or higher. Namespace And Schema First thing we need to add namespace and schema location to mule configuration file (e.g. droolsexample.xml).
  7. Integrating DROOLS With Mule ESB Initialize BPM Rules Add below

    tag in droolsdemo.xml and it will tell the mule flow there is some rules defined in your flow and it will start DROOL's working memory. Defining Mule Flow With DROOLS Rules We will be receiving the JSON message and it will be converted into Java object using JSON To Object transformer. Our Java class for Insurance will look like this:
  8. Integrating DROOLS With Mule ESB package com.drools.example; public class Insurance

    { private String name; private int age; private String policyType; private int premiumAmount; 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 String getPolicyType() { return policyType; } public void setPolicyType(String policyType) { this.policyType=policyType; } public int getPremiumAmount() { return premiumAmount; } public void setPremiumAmount(int premiumAmount) { this.premiumAmount=premiumAmount; } } Add file with .drl extension to your classpath (src/main/resources). This file will contains all rules in plaintext which can be easily modified and loaded at runtime if needed.
  9. Integrating DROOLS With Mule ESB1 package com.drools.example import org.mule.MessageExchangePattern; import

    com.drools.example.Insurance; global org.mule.module.bpm.MessageService mule; dialect "mvel" declare Insurance @role('event') end rule "Policy 1 And Infant" lock - on - active when $insurance: Insurance(age < 10 && policyType == "Policy1") then modify($insurance) { setPremiumAmount(20000) } end rule "Policy 1 And Adult" lock - on - active when $insurance: Insurance((age >= 10 && age <= 40) && policyType == "Policy1") then modify($insurance) { setPremiumAmount(30000) } end rule "Policy 1 And Older" lock - on - active when $insurance: Insurance(age > 40 && policyType == "Policy1") then modify($insurance) { setPremiumAmount(40000) } end rule "Policy 2 And Infant" lock - on - active when $insurance: Insurance(age < 10 && policyType == "Policy2") then modify($insurance) { setPremiumAmount(21000) } end rule "Policy 2 And Adult" lock - on - active when $insurance: Insurance((age >= 10 && age <= 40) && policyType == "Policy2") then modify($insurance) { setPremiumAmount(31000) } end rule "Policy 2 And Older" lock - on - active when $insurance: Insurance(age > 40 && policyType == "Policy2") then modify($insurance) { setPremiumAmount(41000) } end
  10. Integrating DROOLS With Mule ESB In 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 earlier. Define Spring Beans You need to define spring beans in your configuration file (e.g. droolsexample.xml) as shown below
  11. Integrating DROOLS With Mule ESB Call Rule Definition in Mule

    Flow • Rule Definition can be call in Mule flow by defining below statement in configuration file (droolsexample.xml)
  12. Integrating DROOLS With Mule ESB Mule Flow [Code] <?xml version="1.0"

    encoding="UTF-8"?> <mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:wmq="http://www.mulesoft.org/schema/mule/ee/wmq" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:bpm="http://www.mulesoft.org/schema/mule/bpm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd http://www.mulesoft.org/schema/mule/ee/wmq http://www.mulesoft.org/schema/mule/ee/wmq/current/mule-wmq-ee.xsd http://www.mulesoft.org/schema/mule/bpm http://www.mulesoft.org/schema/mule/bpm/current/mule-bpm.xsd"> <bpm:drools/> <spring:beans> <spring:bean name="NoFactsBean" class="java.util.ArrayList" /> </spring:beans> <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/> <flow name="mule-proj-flowFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/drools" allowedMethods="POST" doc:name="HTTP"/> <wmq:object-to-message-transformer doc:name="Object to Message"/> <json:json-to-object-transformer returnClass="com.drools.example.Insurance" doc:name="JSON to Object"/> <bpm:rules rulesDefinition="insuranceRules.drl" initialFacts-ref="NoFactsBean" doc:name="BPMRules" /> <set-payload value="#[payload.object]" doc:name="Set Payload"/> <json:object-to-json-transformer doc:name="Object to JSON"/> </flow> </mule>
  13. Integrating DROOLS With Mule ESB Testing We will use postman

    to test all the above scenarios and hopefully, we will get expected result back.
  14. Integrating DROOLS With Mule ESB Conclusion Mule provided easy steps

    to integrate with DROOLS and same is explained in above article. I hope you find this article interesting and provided required information to integrate DROOLS with Mule flow.