how to integrate drools with spring boot application


Overview:
In this tutorial, i have explained how to integrate jboss drools rule engine with spring boot application by creating a simple spring boot rest api. - simple spring boot application, simple spring boot rest api, jboss drools rule engine integration with spring boot, simple spring boot get method rest api.


Video
This tutorial is explained in the below Youtube Video.




The project structure is as follows-




step 1:
Create a new maven spring boot project using https://start.spring.io/

step 2:
Add kie-ci dependency to pom.xml

step 3:
Create a model object named Test

step 4:
Create a .drl rules file under src/main/resources/META-INF/rules

package rules

import com.example.demo.Test

rule "sla for ui"
 when 
  testObject: Test(type=="ui")
 then
  testObject.setSla("15");
 end
rule "sla for be"
 when 
  testObject: Test(type=="be")
 then
  testObject.setSla("30";
 end
step 5:
Create a kmodule.xml file under src/main/resources/META-INF to create a kie session object.

<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
   <kbase name="rules" packages="rules">
        <ksession name="rulesSession"/>
    </kbase>
</kmodule>

step 6:
Create a controller class and initialize the kiesession object and fire all the rules.

//get the stateful session
  KieSession kieSession = kieContainer.newKieSession("rulesSession");
  kieSession.insert(test);
  kieSession.fireAllRules();
  kieSession.dispose();


step 7:
Create a kie container object in the main application class.

@Bean
 public KieContainer kieContainer() {
  return KieServices.Factory.get().getKieClasspathContainer();
 }


step 8:
Test the application using http://localhost:8080/getSla?type=ui



Comments