🏠Spring FrameworkDrools Rule Engine for Spring Boot – Tutorial

Drools Rule Engine for Spring Boot – Tutorial

Lets learn how to integrate Drools Rule Engine with Spring Boot application for business rules management with an Example. Drools is a Business Rule Engine that is based on Java Rules API. It lets you create complex applications where the business logic changes a lot post development.

Introduction To Drools

For example, you may run a shopping website that provides discounts based on a certain criteria. But this criteria may change in the future. In these cases, It would require a code change to update the business logic. However, You can use a rule engine like Drools to externalize the decision logic thus avoiding technical debt.

Drools Business Rule management System for Spring Boot

Spring Boot Dependencies for Drools Rule Engine

If you want to use drools in your spring boot project, then you first need to add the drools core and compiler dependencies.

<dependency>
    <groupId>org.drools</groupId>
    <artifactId>drools-core</artifactId>
    <version>7.49.0.Final</version>
</dependency>
<dependency>
    <groupId>org.drools</groupId>
    <artifactId>drools-compiler</artifactId>
    <version>7.49.0.Final</version>
</dependency>Code language: HTML, XML (xml)

After that, you can start creating drools container and auto wire it wherever you want. For instance, you can write a simple bean configuration as shown below.

Drools Configuration with Spring

Every rule in Drools belongs to a rule set and the application requires a KieContainer to execute these rules against an object. So lets create a simple container bean.

@Configuration
public class DroolsSpringConfiguration {

    private final KieServices kieServices = KieServices.Factory.<em>get</em>();


    @Bean
    public KieContainer getKieContainer() {
        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
        kieFileSystem.write(ResourceFactory.<em>newClassPathResource</em>("discount.drl"));
        KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);
        kb.buildAll();
        KieModule kieModule = kb.getKieModule();
        return kieServices.newKieContainer(kieModule.getReleaseId());

    }

}Code language: PHP (php)

The above snippet creates a KieContainer and lets you auto wire it wherever you want. To test this drools spring boot example, Let’s write a DRL file for a discount logic.

Creating rules with .DRL files

Lets say we have an order request as shown below.

import  com.springhow.examples.drools.domain.OrderRequest;

rule "Discount for CARD payment above 10000"
    when
        orderObject : OrderRequest( paymentType=="CARD" && totalPrice > 10000 );
    then
        orderObject.setDiscount(15);
end;

rule "Discount for CARD payment below 10000"
    when
        orderObject : OrderRequest( paymentType=="CARD" && totalPrice > 5000 && totalPrice < 10000 );
    then
        orderObject.setDiscount(5);
end;Code language: JavaScript (javascript)

This rule will apply a 15% discount on orders with card payment above 10000. However, if the order is below 10000 and above 5000, it will apply only 5%. Let’s test this out with an API call.

Verify Drools integration with Spring Boot

First, Lets write a controller that takes an order as input and evaluates these rule entries.

@PostMapping("/discount")
private OrderRequest getDiscountPercent(@RequestBody OrderRequest orderRequest) {
    KieSession kieSession = kieContainer.newKieSession();
    kieSession.insert(orderRequest);
    kieSession.fireAllRules();
    kieSession.dispose();
    return orderRequest;
}Code language: PHP (php)

As you see, We are using the kieContainer that was autowired from our previous steps. With this bean, We can initiate a kieSession and evaluate our rules. In this case, We can use postman to validate our implementation.

Drools rule 1 was applied
15% discount applied to order

Similarly you can test the other cases as well.

Conclusion

In conclusion, We learned how to integrate Drools with Spring Boot and execute decision rules with a sample application. If you want to learn more about it, Please checkout the implementation in this GitHub repository.

If you find this post useful, you may like read about the following titles.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

One Comment