Saturday, 24 March 2018

Drools and Spring Boot RestFul Web service

Drools with Spring Boot Restful web service with Excel file

 In this post we will integrate Drool Rule engine with Spring Boot and create RestFul web service.
In this example we use to find rule of Interest from Excel sheet.
Drool decision table is used to setup rule that will read rule configured and will return the the response.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

       <modelVersion>4.0.0</modelVersion>

       <groupId>com.drools</groupId>
       <artifactId>Drool-Demo</artifactId>
       <version>1.0.0-SNAPSHOT</version>
       <packaging>jar</packaging>

      <properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
              <kie.version>6.1.0.Final</kie.version>
       </properties>


       <dependencyManagement>
              <dependencies>
                     <dependency>
                           <groupId>io.spring.platform</groupId>
                           <artifactId>platform-bom</artifactId>
                           <version>1.1.1.RELEASE</version>
                           <type>pom</type>
                           <scope>import</scope>
                     </dependency>
              </dependencies>
       </dependencyManagement>

       <dependencies>
              <dependency>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-starter-web</artifactId>
              </dependency>

              <dependency>
                     <groupId>org.kie</groupId>
                     <artifactId>kie-ci</artifactId>
                     <version>${kie.version}</version>
              </dependency>
              <dependency>
                     <groupId>org.drools</groupId>
                     <artifactId>drools-decisiontables</artifactId>
                     <version>${kie.version}</version>
              </dependency>

              <dependency>
                     <groupId>org.eclipse.jdt</groupId>
                     <artifactId>core</artifactId>
                     <version>3.3.0-v_771</version>
              </dependency>

              <dependency>
                     <groupId>org.antlr</groupId>
                     <artifactId>antlr-runtime</artifactId>
                     <version>3.1.3</version>
                     <scope>test</scope>
              </dependency>
              <dependency>
                     <groupId>org.mvel</groupId>
                     <artifactId>mvel2</artifactId>
                     <version>2.0.16</version>
                     <scope>test</scope>
              </dependency>       

       </dependencies>
       <build>
              <plugins>
                     <plugin>
                           <groupId>org.springframework.boot</groupId>
                           <artifactId>spring-boot-maven-plugin</artifactId>
                     </plugin>
              </plugins>
       </build>

</project>


-------------------POJO class-----------------------------
package com.drools.model;

public class Interest {

       private String type;
       private String formula;
       public String getType() {
              return type;
       }
       public void setType(String type) {
              this.type = type;
       }
       public String getFormula() {
              return formula;
       }
       public void setFormula(String formula) {
              this.formula = formula;
       }
      
      
}

-------------------------------Controller---------------------------------------------------------

package com.drools.controller;

import java.io.FileNotFoundException;

import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.Message;
import org.kie.api.builder.Results;
import org.kie.api.io.Resource;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.KnowledgeBase;
import org.kie.internal.KnowledgeBaseFactory;
import org.kie.internal.builder.DecisionTableConfiguration;
import org.kie.internal.builder.DecisionTableInputType;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.runtime.StatelessKnowledgeSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.drools.model.Interest;

@RestController
public class InterestController {
      
       private static StatelessKnowledgeSession session;
      
       @RequestMapping(value = "/interest", method = RequestMethod.GET, produces = "application/json")
       public Interest interestCalculate(@RequestParam String type) throws Exception{
              return runInterestRule(type);
       }
      
       @SuppressWarnings("deprecation")
       private Interest runInterestRule(String type) throws Exception{
              KnowledgeBase knowledgeBase = createKnowledgeBaseFromXLS ();
              session = knowledgeBase.newStatelessKnowledgeSession();
              Interest interest = new Interest();
              interest.setType(type);
              session.execute(interest);
              return interest;
       }

      
       @SuppressWarnings("deprecation")
       private static KnowledgeBase createKnowledgeBaseFromXLS ()throws Exception {
              DecisionTableConfiguration dtconf = KnowledgeBuilderFactory.newDecisionTableConfiguration();
              dtconf.setInputType(DecisionTableInputType.XLS);

              KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
              knowledgeBuilder.add(ResourceFactory.newFileResource("C:/Users/AK/Desktop/Drools/Interest.xls"),ResourceType.DTABLE, dtconf);
              if (knowledgeBuilder.hasErrors()) {
                     throw new RuntimeException(knowledgeBuilder.getErrors().toString());
              }            

              KnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
              knowledgeBase.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages());
              return knowledgeBase;
       }      
      
}

---------------------Excel Rule file Sample----------------------


RuleSet com.drools
Import com.drools.model.Interest
Notes    
RuleTable Interest Calculator    
NAME CONDITION ACTION
$interest:Interest  
  $interest.getType()==$param $interest.setFormula($param);
NAME coupon code Set Discount
If account is CI "CI" "PRINCIAP+RATE"
If account is SI "SI" "PRINCIAP+TIME"