Skip to content

Commit 1a10fda

Browse files
baljeet20maibin
authored andcommitted
BAEL-928 Introduction to drools (eugenp#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
1 parent b2828f5 commit 1a10fda

12 files changed

Lines changed: 411 additions & 0 deletions

File tree

drools/pom.xml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>drools</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<properties>
11+
<http-component-version>4.4.6</http-component-version>
12+
<drools-version>7.0.0.CR1</drools-version>
13+
<apache-poi-version>3.13</apache-poi-version>
14+
<surefire-plugin-version>2.19.1</surefire-plugin-version>
15+
<junit-version>4.11</junit-version>
16+
</properties>
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.apache.httpcomponents</groupId>
20+
<artifactId>httpcore</artifactId>
21+
<version>${http-component-version}</version>
22+
</dependency>
23+
<!-- ... -->
24+
<dependency>
25+
<groupId>org.kie</groupId>
26+
<artifactId>kie-ci</artifactId>
27+
<version>${drools-version}</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.drools</groupId>
31+
<artifactId>drools-decisiontables</artifactId>
32+
<version>${drools-version}</version>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.drools</groupId>
37+
<artifactId>drools-core</artifactId>
38+
<version>${drools-version}</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.drools</groupId>
42+
<artifactId>drools-compiler</artifactId>
43+
<version>${drools-version}</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.apache.poi</groupId>
47+
<artifactId>poi</artifactId>
48+
<version>${apache-poi-version}</version>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>org.apache.poi</groupId>
53+
<artifactId>poi-ooxml</artifactId>
54+
<version>${apache-poi-version}</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>junit</groupId>
58+
<artifactId>junit</artifactId>
59+
<version>${junit-version}</version>
60+
<scope>test</scope>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.springframework</groupId>
64+
<artifactId>spring-core</artifactId>
65+
<version>4.3.6.RELEASE</version>
66+
</dependency>
67+
68+
</dependencies>
69+
<build>
70+
<plugins>
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-surefire-plugin</artifactId>
74+
<version>${surefire-plugin-version}</version>
75+
<configuration>
76+
<excludes>
77+
<exclude>**/*IntegrationTest.java</exclude>
78+
</excludes>
79+
</configuration>
80+
</plugin>
81+
</plugins>
82+
</build>
83+
84+
</project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.drools.config;
2+
3+
import org.kie.api.KieServices;
4+
import org.kie.api.builder.*;
5+
import org.kie.api.io.KieResources;
6+
import org.kie.api.runtime.KieContainer;
7+
import org.kie.api.runtime.KieSession;
8+
import org.kie.internal.io.ResourceFactory;
9+
import java.io.IOException;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
public class DroolsBeanFactory {
14+
15+
private static final String RULES_PATH = "com/baeldung/drools/rules/";
16+
private KieServices kieServices=KieServices.Factory.get();
17+
18+
private KieFileSystem getKieFileSystem() throws IOException{
19+
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
20+
List<String> rules=Arrays.asList("SuggestApplicant.drl","Product_rules.xls");
21+
for(String rule:rules){
22+
kieFileSystem.write(ResourceFactory.newClassPathResource(rule));
23+
}
24+
return kieFileSystem;
25+
26+
}
27+
28+
public KieContainer getKieContainer() throws IOException {
29+
getKieRepository();
30+
31+
KieBuilder kb = kieServices.newKieBuilder(getKieFileSystem());
32+
kb.buildAll();
33+
34+
KieModule kieModule = kb.getKieModule();
35+
KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId());
36+
37+
return kContainer;
38+
39+
}
40+
41+
private void getKieRepository() {
42+
final KieRepository kieRepository = kieServices.getRepository();
43+
kieRepository.addKieModule(new KieModule() {
44+
public ReleaseId getReleaseId() {
45+
return kieRepository.getDefaultReleaseId();
46+
}
47+
});
48+
}
49+
50+
public KieSession getKieSession(){
51+
getKieRepository();
52+
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
53+
54+
kieFileSystem.write(ResourceFactory.newClassPathResource("com/baeldung/drools/rules/SuggestApplicant.drl"));
55+
kieFileSystem.write(ResourceFactory.newClassPathResource("com/baeldung/drools/rules/Product_rules.xls"));
56+
57+
KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);
58+
kb.buildAll();
59+
KieModule kieModule = kb.getKieModule();
60+
61+
KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId());
62+
63+
return kContainer.newKieSession();
64+
65+
}
66+
67+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.drools.model;
2+
3+
public class Applicant {
4+
5+
private String name;
6+
private int age;
7+
private double currentSalary;
8+
private int experienceInYears;
9+
10+
public Applicant(String name, int age, Double currentSalary, int experienceInYears) {
11+
this.name = name;
12+
this.age = age;
13+
this.currentSalary = currentSalary;
14+
this.experienceInYears = experienceInYears;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public void setName(String name) {
22+
this.name = name;
23+
}
24+
25+
public int getAge() {
26+
return age;
27+
}
28+
29+
public void setAge(int age) {
30+
this.age = age;
31+
}
32+
33+
public Double getCurrentSalary() {
34+
return currentSalary;
35+
}
36+
37+
public void setCurrentSalary(Double currentSalary) {
38+
this.currentSalary = currentSalary;
39+
}
40+
41+
public int getExperienceInYears() {
42+
return experienceInYears;
43+
}
44+
45+
public void setExperienceInYears(int experienceInYears) {
46+
this.experienceInYears = experienceInYears;
47+
}
48+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.drools.model;
2+
3+
public class Product {
4+
private String name;
5+
private String type;
6+
7+
private String label;
8+
9+
public Product(String name, String type) {
10+
this.name = name;
11+
this.type = type;
12+
}
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
22+
public String getType() {
23+
return type;
24+
}
25+
26+
public void setType(String type) {
27+
this.type = type;
28+
}
29+
30+
public String getLabel() {
31+
return label;
32+
}
33+
34+
public void setLabel(String label) {
35+
this.label = label;
36+
}
37+
38+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.drools.model;
2+
3+
public class SuggestedRole {
4+
5+
private String role;
6+
7+
public String getRole() {
8+
return role;
9+
}
10+
11+
public void setRole(String role) {
12+
this.role = role;
13+
}
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.drools.service;
2+
3+
import com.baeldung.drools.config.DroolsBeanFactory;
4+
import com.baeldung.drools.model.Applicant;
5+
import com.baeldung.drools.model.SuggestedRole;
6+
import org.kie.api.runtime.KieContainer;
7+
import org.kie.api.runtime.KieSession;
8+
9+
import java.io.IOException;
10+
11+
public class ApplicantService {
12+
13+
KieSession kieSession=new DroolsBeanFactory().getKieSession();
14+
15+
public SuggestedRole suggestARoleForApplicant(Applicant applicant,SuggestedRole suggestedRole) throws IOException {
16+
kieSession.insert(applicant);
17+
kieSession.setGlobal("suggestedRole",suggestedRole);
18+
kieSession.fireAllRules();
19+
System.out.println(suggestedRole.getRole());
20+
return suggestedRole;
21+
22+
}
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.drools.service;
2+
3+
import com.baeldung.drools.config.DroolsBeanFactory;
4+
import com.baeldung.drools.model.Product;
5+
import org.kie.api.runtime.KieContainer;
6+
import org.kie.api.runtime.KieSession;
7+
8+
public class ProductService {
9+
10+
private KieSession kieSession=new DroolsBeanFactory().getKieSession();
11+
12+
public Product applyLabelToProduct(Product product){
13+
kieSession.insert(product);
14+
kieSession.fireAllRules();
15+
System.out.println(product.getLabel());
16+
return product;
17+
18+
}
19+
20+
}
Binary file not shown.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.drools.rules;
2+
3+
import com.baeldung.drools.model.Applicant;
4+
5+
global com.baeldung.drools.model.SuggestedRole suggestedRole;
6+
7+
dialect "mvel"
8+
9+
rule "Suggest Manager Role"
10+
when
11+
Applicant(experienceInYears > 10)
12+
Applicant(currentSalary > 1000000 && currentSalary <= 2500000)
13+
then
14+
suggestedRole.setRole("Manager");
15+
end
16+
17+
rule "Suggest Senior developer Role"
18+
when
19+
Applicant(experienceInYears > 5 && experienceInYears <= 10)
20+
Applicant(currentSalary > 500000 && currentSalary <= 1500000)
21+
then
22+
suggestedRole.setRole("Senior developer");
23+
end
24+
25+
rule "Suggest Developer Role"
26+
when
27+
Applicant(experienceInYears > 0 && experienceInYears <= 5)
28+
Applicant(currentSalary > 200000 && currentSalary <= 1000000)
29+
then
30+
suggestedRole.setRole("Developer");
31+
end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.drools.service;
2+
3+
import com.baeldung.drools.model.Applicant;
4+
import com.baeldung.drools.model.SuggestedRole;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import java.io.IOException;
9+
10+
import static junit.framework.Assert.assertNull;
11+
import static junit.framework.TestCase.assertEquals;
12+
13+
14+
public class ApplicantServiceIntegrationTest {
15+
16+
17+
private ApplicantService applicantService;
18+
19+
@Before
20+
public void setup(){
21+
applicantService = new ApplicantService();
22+
}
23+
24+
@Test
25+
public void whenCriteriaMatching_ThenSuggestManagerRole() throws IOException {
26+
Applicant applicant=new Applicant("Davis",37,1600000.0,11);
27+
SuggestedRole suggestedRole=new SuggestedRole();
28+
applicantService.suggestARoleForApplicant(applicant,suggestedRole);
29+
assertEquals("Manager",suggestedRole.getRole());
30+
}
31+
32+
@Test
33+
public void whenCriteriaMatching_ThenSuggestSeniorDeveloperRole() throws IOException {
34+
Applicant applicant=new Applicant("John",37,1200000.0,8);
35+
SuggestedRole suggestedRole=new SuggestedRole();
36+
applicantService.suggestARoleForApplicant(applicant,suggestedRole);
37+
assertEquals("Senior developer",suggestedRole.getRole());
38+
}
39+
@Test
40+
public void whenCriteriaMatching_ThenSuggestDeveloperRole() throws IOException {
41+
Applicant applicant=new Applicant("Davis",37,800000.0,3);
42+
SuggestedRole suggestedRole=new SuggestedRole();
43+
applicantService.suggestARoleForApplicant(applicant,suggestedRole);
44+
assertEquals("Developer",suggestedRole.getRole());
45+
}
46+
@Test
47+
public void whenCriteriaNotMatching_ThenNoRole() throws IOException {
48+
Applicant applicant=new Applicant("John",37,1200000.0,5);
49+
SuggestedRole suggestedRole=new SuggestedRole();
50+
applicantService.suggestARoleForApplicant(applicant,suggestedRole);
51+
assertNull(suggestedRole.getRole());
52+
}
53+
}

0 commit comments

Comments
 (0)