BAEL-804 A guide to Spring Drools (#1776)

* BAEL-804  A guide to Spring Drools

* BAEL-804  A guide to Spring Drools

* BAEL-804  A guide to Spring Drools
This commit is contained in:
baljeet20 2017-05-04 15:50:55 +05:30 committed by maibin
parent 223cc5357f
commit 5f02c64c95
13 changed files with 510 additions and 0 deletions

View File

@ -212,6 +212,7 @@
<module>spring-data-gemfire</module>
<module>cucumber</module>
<module>mybatis</module>
<module>spring-drools</module>
</modules>
<build>

130
spring-drools/pom.xml Normal file
View File

@ -0,0 +1,130 @@
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>spring-drools</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<spring-boot-version>1.1.1.RELEASE</spring-boot-version>
<http-component-version>4.0-alpha6</http-component-version>
<drools-version>7.0.0.CR1</drools-version>
<apache-poi-version>3.13</apache-poi-version>
<surefire-plugin-version>2.19.1</surefire-plugin-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>${spring-boot-version}</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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${http-component-version}</version>
</dependency>
<!-- ... -->
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-ci</artifactId>
<version>${drools-version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-decisiontables</artifactId>
<version>${drools-version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>${drools-version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>${drools-version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${apache-poi-version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${apache-poi-version}</version>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-spring</artifactId>
<version>6.4.0.Final</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin-version}</version>
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,30 @@
package com.baeldung.spring.drools;
import com.baeldung.spring.drools.model.Applicant;
import com.baeldung.spring.drools.model.Product;
import com.baeldung.spring.drools.model.SuggestedRole;
import com.baeldung.spring.drools.service.ApplicantService;
import com.baeldung.spring.drools.service.ProductService;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
@SpringBootApplication
@Import(DroolConfiguration.class)
public class Application {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
ApplicantService applicantService=(ApplicantService)ctx.getBean("applicantService");
applicantService.suggestARoleForApplicant(new Applicant("Baljeet",37,1200000.0,8),new SuggestedRole());
ProductService productService=(ProductService)ctx.getBean("productService");
Product returnedProduct=productService.applyLabelToProduct(new Product("Microwave","Book"));
}
}

View File

@ -0,0 +1,83 @@
package com.baeldung.spring.drools;
import com.baeldung.spring.drools.service.ApplicantService;
import com.baeldung.spring.drools.service.ProductService;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.*;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.kie.spring.KModuleBeanFactoryPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import java.io.IOException;
@Configuration
public class DroolConfiguration {
private static final String RULES_PATH = "com/baeldung/spring/drools/rules/";
@Bean
public KieFileSystem kieFileSystem() throws IOException {
KieFileSystem kieFileSystem = getKieServices().newKieFileSystem();
for (Resource file : getRuleFiles()) {
kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_PATH + file.getFilename(), "UTF-8"));
}
return kieFileSystem;
}
private Resource[] getRuleFiles() throws IOException {
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
return resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "**/*.*");
}
@Bean
public KieContainer kieContainer() throws IOException {
final KieRepository kieRepository = getKieServices().getRepository();
kieRepository.addKieModule(new KieModule() {
public ReleaseId getReleaseId() {
return kieRepository.getDefaultReleaseId();
}
});
KieBuilder kieBuilder = getKieServices().newKieBuilder(kieFileSystem());
kieBuilder.buildAll();
return getKieServices().newKieContainer(kieRepository.getDefaultReleaseId());
}
private KieServices getKieServices() {
return KieServices.Factory.get();
}
@Bean
public KieBase kieBase() throws IOException {
return kieContainer().getKieBase();
}
@Bean
public KieSession kieSession() throws IOException {
return kieContainer().newKieSession();
}
@Bean
public ApplicantService getApplicantService(){
return new ApplicantService();
}
@Bean
public ProductService getProductService(KieContainer kieContainer){
return new ProductService();
}
@Bean
public KModuleBeanFactoryPostProcessor kiePostProcessor() {
return new KModuleBeanFactoryPostProcessor();
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.spring.drools.model;
public class Applicant {
private String name;
private int age;
private double currentSalary;
private int experienceInYears;
public Applicant(String name, int age, Double currentSalary, int experienceInYears) {
this.name = name;
this.age = age;
this.currentSalary = currentSalary;
this.experienceInYears = experienceInYears;
}
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 Double getCurrentSalary() {
return currentSalary;
}
public void setCurrentSalary(Double currentSalary) {
this.currentSalary = currentSalary;
}
public int getExperienceInYears() {
return experienceInYears;
}
public void setExperienceInYears(int experienceInYears) {
this.experienceInYears = experienceInYears;
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.spring.drools.model;
public class Product {
private String name;
private String type;
private String label;
public Product(String name, String type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.spring.drools.model;
public class SuggestedRole {
private String role;
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.spring.drools.service;
import com.baeldung.spring.drools.model.Applicant;
import com.baeldung.spring.drools.model.SuggestedRole;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ApplicantService {
@Autowired
private KieContainer kieContainer;
public SuggestedRole suggestARoleForApplicant(Applicant applicant,SuggestedRole suggestedRole){
KieSession kieSession = kieContainer.newKieSession();
kieSession.insert(applicant);
kieSession.setGlobal("suggestedRole",suggestedRole);
kieSession.fireAllRules();
System.out.println(suggestedRole.getRole());
return suggestedRole;
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.spring.drools.service;
import com.baeldung.spring.drools.model.Product;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ProductService {
@Autowired
private KieContainer kieContainer;
public Product applyLabelToProduct(Product product){
KieSession kieSession = kieContainer.newKieSession();
kieSession.insert(product);
kieSession.fireAllRules();
System.out.println(product.getLabel());
return product;
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.spring.drools.rules;
import com.baeldung.spring.drools.model.Applicant;
global com.baeldung.spring.drools.model.SuggestedRole suggestedRole;
dialect "mvel"
rule "Suggest Manager Role"
when
Applicant(experienceInYears > 10)
Applicant(currentSalary > 1000000 && currentSalary <= 2500000)
then
suggestedRole.setRole("Manager");
end
rule "Suggest Senior developer Role"
when
Applicant(experienceInYears > 5 && experienceInYears <= 10)
Applicant(currentSalary > 500000 && currentSalary <= 1500000)
then
suggestedRole.setRole("Senior developer");
end
rule "Suggest Developer Role"
when
Applicant(experienceInYears > 0 && experienceInYears <= 5)
Applicant(currentSalary > 200000 && currentSalary <= 1000000)
then
suggestedRole.setRole("Developer");
end

View File

@ -0,0 +1,52 @@
package com.baeldung.spring.drools.service;
import com.baeldung.spring.drools.Application;
import com.baeldung.spring.drools.DroolConfiguration;
import com.baeldung.spring.drools.model.Applicant;
import com.baeldung.spring.drools.model.SuggestedRole;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static junit.framework.Assert.assertNull;
import static junit.framework.TestCase.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {DroolConfiguration.class})
public class ApplicantServiceIntegrationTest {
@Autowired
ApplicantService applicantService;
@Test
public void whenCriteriaMatching_ThenSuggestManagerRole(){
Applicant applicant=new Applicant("Davis",37,1600000.0,11);
SuggestedRole suggestedRole=new SuggestedRole();
applicantService.suggestARoleForApplicant(applicant,suggestedRole);
assertEquals("Manager",suggestedRole.getRole());
}
@Test
public void whenCriteriaMatching_ThenSuggestSeniorDeveloperRole(){
Applicant applicant=new Applicant("John",37,1200000.0,8);
SuggestedRole suggestedRole=new SuggestedRole();
applicantService.suggestARoleForApplicant(applicant,suggestedRole);
assertEquals("Senior developer",suggestedRole.getRole());
}
@Test
public void whenCriteriaMatching_ThenSuggestDeveloperRole(){
Applicant applicant=new Applicant("Davis",37,800000.0,3);
SuggestedRole suggestedRole=new SuggestedRole();
applicantService.suggestARoleForApplicant(applicant,suggestedRole);
assertEquals("Developer",suggestedRole.getRole());
}
@Test
public void whenCriteriaNotMatching_ThenNoRole(){
Applicant applicant=new Applicant("John",37,1200000.0,5);
SuggestedRole suggestedRole=new SuggestedRole();
applicantService.suggestARoleForApplicant(applicant,suggestedRole);
assertNull(suggestedRole.getRole());
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.spring.drools.service;
import com.baeldung.spring.drools.Application;
import com.baeldung.spring.drools.DroolConfiguration;
import com.baeldung.spring.drools.model.Product;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static junit.framework.TestCase.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {DroolConfiguration.class})
public class ProductServiceIntegrationTest {
@Autowired
ProductService productService;
@Test
public void whenProductTypeElectronic_ThenLabelBarcode(){
Product product=new Product("Microwave","Electronic");
product=productService.applyLabelToProduct(product);
assertEquals("BarCode",product.getLabel());
}
@Test
public void whenProductTypeBook_ThenLabelIsbn(){
Product product=new Product("AutoBiography","Book");
product=productService.applyLabelToProduct(product);
assertEquals("ISBN",product.getLabel());
}
}