BAEL-928 Introduction to drools (#1842)

* BAEL-804  A guide to Spring Drools

* BAEL-804  A guide to Spring Drools

* BAEL-804  A guide to Spring Drools

* BAEL-928 Introduction to Drools

* BAEL-928 Introduction to Drools
This commit is contained in:
baljeet20 2017-05-14 19:22:48 +05:30 committed by maibin
parent b2828f59e1
commit 1a10fda1a8
12 changed files with 411 additions and 0 deletions

84
drools/pom.xml Normal file
View File

@ -0,0 +1,84 @@
<?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>drools</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<http-component-version>4.4.6</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>
<junit-version>4.11</junit-version>
</properties>
<dependencies>
<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>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<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,67 @@
package com.baeldung.drools.config;
import org.kie.api.KieServices;
import org.kie.api.builder.*;
import org.kie.api.io.KieResources;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class DroolsBeanFactory {
private static final String RULES_PATH = "com/baeldung/drools/rules/";
private KieServices kieServices=KieServices.Factory.get();
private KieFileSystem getKieFileSystem() throws IOException{
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
List<String> rules=Arrays.asList("SuggestApplicant.drl","Product_rules.xls");
for(String rule:rules){
kieFileSystem.write(ResourceFactory.newClassPathResource(rule));
}
return kieFileSystem;
}
public KieContainer getKieContainer() throws IOException {
getKieRepository();
KieBuilder kb = kieServices.newKieBuilder(getKieFileSystem());
kb.buildAll();
KieModule kieModule = kb.getKieModule();
KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId());
return kContainer;
}
private void getKieRepository() {
final KieRepository kieRepository = kieServices.getRepository();
kieRepository.addKieModule(new KieModule() {
public ReleaseId getReleaseId() {
return kieRepository.getDefaultReleaseId();
}
});
}
public KieSession getKieSession(){
getKieRepository();
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
kieFileSystem.write(ResourceFactory.newClassPathResource("com/baeldung/drools/rules/SuggestApplicant.drl"));
kieFileSystem.write(ResourceFactory.newClassPathResource("com/baeldung/drools/rules/Product_rules.xls"));
KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);
kb.buildAll();
KieModule kieModule = kb.getKieModule();
KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId());
return kContainer.newKieSession();
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.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.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.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,23 @@
package com.baeldung.drools.service;
import com.baeldung.drools.config.DroolsBeanFactory;
import com.baeldung.drools.model.Applicant;
import com.baeldung.drools.model.SuggestedRole;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import java.io.IOException;
public class ApplicantService {
KieSession kieSession=new DroolsBeanFactory().getKieSession();
public SuggestedRole suggestARoleForApplicant(Applicant applicant,SuggestedRole suggestedRole) throws IOException {
kieSession.insert(applicant);
kieSession.setGlobal("suggestedRole",suggestedRole);
kieSession.fireAllRules();
System.out.println(suggestedRole.getRole());
return suggestedRole;
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.drools.service;
import com.baeldung.drools.config.DroolsBeanFactory;
import com.baeldung.drools.model.Product;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
public class ProductService {
private KieSession kieSession=new DroolsBeanFactory().getKieSession();
public Product applyLabelToProduct(Product product){
kieSession.insert(product);
kieSession.fireAllRules();
System.out.println(product.getLabel());
return product;
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.drools.rules;
import com.baeldung.drools.model.Applicant;
global com.baeldung.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,53 @@
package com.baeldung.drools.service;
import com.baeldung.drools.model.Applicant;
import com.baeldung.drools.model.SuggestedRole;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import static junit.framework.Assert.assertNull;
import static junit.framework.TestCase.assertEquals;
public class ApplicantServiceIntegrationTest {
private ApplicantService applicantService;
@Before
public void setup(){
applicantService = new ApplicantService();
}
@Test
public void whenCriteriaMatching_ThenSuggestManagerRole() throws IOException {
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() throws IOException {
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() throws IOException {
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() throws IOException {
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,32 @@
package com.baeldung.drools.service;
import com.baeldung.drools.model.Product;
import org.junit.Before;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
public class ProductServiceIntegrationTest {
private ProductService productService;
@Before
public void setup(){
productService=new 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());
}
}

View File

@ -221,6 +221,7 @@
<module>spring-data-gemfire</module>
<module>mybatis</module>
<module>spring-drools</module>
<module>drools</module>
</modules>
<dependencies>