DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Databases
  4. Drools: How Can We Overcame the Drastic Conditions Evaluation?

Drools: How Can We Overcame the Drastic Conditions Evaluation?

Want to learn how to overcome drastic conditions evaluations with drool? Check out this tutorial to learn more about the drools decision table.

Aruna Karunarathna user avatar by
Aruna Karunarathna
·
Aug. 16, 18 · Tutorial
Like (7)
Save
Tweet
Share
12.94K Views

Join the DZone community and get the full member experience.

Join For Free

One year ago, we started a project called Keystone, a rules evaluation engine based on spring-boot. The high-level architecture as follows [1]. It exposes several REST endpoints to evaluate some business rules. When a request hits the engine, several parallel calls hit the described endpoints based on the input parameters. ( We use RxJava to handle the async calls and zip out the results.) Then we have various IF ELSE blocks to evaluate the rules. And sent back the results back to the client after the rule evaluation.


In the beginning, the rules were quite simple and everyone was happy with the architecture and the evaluation of the rules. There was a manageable number of rules with simple if else blocks, and the changes to existing rules were quite minimum at that time.

But, when time passed by, there were many requests from partner teams, and the rules engine team was asked to implement more logical evaluations, so new REST endpoints were introduced. Now, the problem became more complex and hard to manage the rules in our code as well as presenting the rules. When some business users ask what happens if we use the REST endpoint X, we have no way to easily explain all the conditions and evaluation paths in a simple manner.

Then, the drools come into the picture to address this problem. We evaluate the drools and did POC for both the drl file and decision table approaches. The code becomes much simpler and lean since all the evaluation tree was derived from the decision table. Then, we presented both the drl file and decision table to the business people, and they really admired the decision table approach, since it became easier to present to other partner teams. See below for an example decision table that is being used. It contains ten decision points before the evaluation.

Image title


Let’s look into a sample that uses a decision table to evaluate some rules. 

Sample Use Case

We are going to evaluate the loan rate given by ABC bank, depending on if the customer is a GOVERNMENT or a PRIVATE worker and currently a retired person or not. The decision table for the above scenario is as follows:


The decision table for the above use case is as follows:

Maven Dependencies

 <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-core</artifactId>
            <version>7.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-spring</artifactId>
            <version>7.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-decisiontables</artifactId>
            <version>7.0.0.Final</version>
        </dependency>


Load the Configurations

public KieContainer getKieContainer() {

        KieServices kieServices = KieServices.Factory.get();
        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
        kieFileSystem.write(ResourceFactory.newFileResource(drlFile));
        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
        kieBuilder.buildAll();
        KieModule kieModule = kieBuilder.getKieModule();

        KieContainerkieContainer =  kieServices.newKieContainer(kieModule.getReleaseId());

        return kieContainer

}


We use the ExecutionBase class to hold the facts and the conditions. The fact is, of course, the Customer object, isGovernmentWorker() , and isRetired()  conditions will look like the following:

public class ExecutionBase {

    private Customer customer;

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public boolean isGovermentWorker() {
        return this.customer.getWorkType().equals(WorkType.GOVERNEMNT);
    }

    public boolean isRetired() {
        return this.customer.getAge() &gt; 60;
    }

    public void execute(String result) {
        System.out.println(result);
    }
}


After the execution, we get the entitlement loan rate for a bank customer. Try out the sample code link.

To summarize this post, we discussed how we can leverage the drools decision tables to overcome if there are drastic conditions evaluations in your program and you want to change those conditions without touching the code. Another advantage is the decision table can be used as a tool to describe your execution flow for non-technical people. That’s it for this post! I hope to see you in another exciting post soon!

Evaluation Drools Database

Published at DZone with permission of Aruna Karunarathna, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Choose the Right Streaming Database
  • A Gentle Introduction to Kubernetes
  • Top 5 Data Streaming Trends for 2023
  • What Is Advertised Kafka Address?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: