Skip to content

Commit 5f02c64

Browse files
baljeet20maibin
authored andcommitted
BAEL-804 A guide to Spring Drools (eugenp#1776)
* BAEL-804 A guide to Spring Drools * BAEL-804 A guide to Spring Drools * BAEL-804 A guide to Spring Drools
1 parent 223cc53 commit 5f02c64

13 files changed

Lines changed: 510 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@
212212
<module>spring-data-gemfire</module>
213213
<module>cucumber</module>
214214
<module>mybatis</module>
215+
<module>spring-drools</module>
215216
</modules>
216217

217218
<build>

‎spring-drools/pom.xml‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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>spring-drools</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<properties>
11+
<spring-boot-version>1.1.1.RELEASE</spring-boot-version>
12+
<http-component-version>4.0-alpha6</http-component-version>
13+
<drools-version>7.0.0.CR1</drools-version>
14+
<apache-poi-version>3.13</apache-poi-version>
15+
<surefire-plugin-version>2.19.1</surefire-plugin-version>
16+
</properties>
17+
<dependencyManagement>
18+
<dependencies>
19+
<dependency>
20+
<groupId>io.spring.platform</groupId>
21+
<artifactId>platform-bom</artifactId>
22+
<version>${spring-boot-version}</version>
23+
<type>pom</type>
24+
<scope>import</scope>
25+
</dependency>
26+
</dependencies>
27+
</dependencyManagement>
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-web</artifactId>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-test</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-actuator</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.apache.httpcomponents</groupId>
45+
<artifactId>httpcore</artifactId>
46+
<version>${http-component-version}</version>
47+
</dependency>
48+
<!-- ... -->
49+
<dependency>
50+
<groupId>org.kie</groupId>
51+
<artifactId>kie-ci</artifactId>
52+
<version>${drools-version}</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.drools</groupId>
56+
<artifactId>drools-decisiontables</artifactId>
57+
<version>${drools-version}</version>
58+
</dependency>
59+
60+
<dependency>
61+
<groupId>org.drools</groupId>
62+
<artifactId>drools-core</artifactId>
63+
<version>${drools-version}</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.drools</groupId>
67+
<artifactId>drools-compiler</artifactId>
68+
<version>${drools-version}</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.apache.poi</groupId>
72+
<artifactId>poi</artifactId>
73+
<version>${apache-poi-version}</version>
74+
</dependency>
75+
76+
<dependency>
77+
<groupId>org.apache.poi</groupId>
78+
<artifactId>poi-ooxml</artifactId>
79+
<version>${apache-poi-version}</version>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.kie</groupId>
83+
<artifactId>kie-spring</artifactId>
84+
<version>6.4.0.Final</version>
85+
<exclusions>
86+
<exclusion>
87+
<groupId>org.springframework</groupId>
88+
<artifactId>spring-tx</artifactId>
89+
</exclusion>
90+
<exclusion>
91+
<groupId>org.springframework</groupId>
92+
<artifactId>spring-beans</artifactId>
93+
</exclusion>
94+
<exclusion>
95+
<groupId>org.springframework</groupId>
96+
<artifactId>spring-core</artifactId>
97+
</exclusion>
98+
<exclusion>
99+
<groupId>org.springframework</groupId>
100+
<artifactId>spring-context</artifactId>
101+
</exclusion>
102+
</exclusions>
103+
</dependency>
104+
</dependencies>
105+
<build>
106+
<plugins>
107+
<plugin>
108+
<groupId>org.springframework.boot</groupId>
109+
<artifactId>spring-boot-maven-plugin</artifactId>
110+
<executions>
111+
<execution>
112+
<goals>
113+
<goal>repackage</goal>
114+
</goals>
115+
</execution>
116+
</executions>
117+
</plugin>
118+
<plugin>
119+
<groupId>org.apache.maven.plugins</groupId>
120+
<artifactId>maven-surefire-plugin</artifactId>
121+
<version>${surefire-plugin-version}</version>
122+
<configuration>
123+
<excludes>
124+
<exclude>**/*IntegrationTest.java</exclude>
125+
</excludes>
126+
</configuration>
127+
</plugin>
128+
</plugins>
129+
</build>
130+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.spring.drools;
2+
3+
4+
import com.baeldung.spring.drools.model.Applicant;
5+
import com.baeldung.spring.drools.model.Product;
6+
import com.baeldung.spring.drools.model.SuggestedRole;
7+
import com.baeldung.spring.drools.service.ApplicantService;
8+
import com.baeldung.spring.drools.service.ProductService;
9+
import org.kie.api.KieServices;
10+
import org.kie.api.runtime.KieContainer;
11+
import org.springframework.boot.SpringApplication;
12+
import org.springframework.boot.autoconfigure.SpringBootApplication;
13+
import org.springframework.context.ApplicationContext;
14+
import org.springframework.context.annotation.Bean;
15+
import org.springframework.context.annotation.Import;
16+
17+
@SpringBootApplication
18+
@Import(DroolConfiguration.class)
19+
public class Application {
20+
21+
public static void main(String[] args) {
22+
ApplicationContext ctx = SpringApplication.run(Application.class, args);
23+
ApplicantService applicantService=(ApplicantService)ctx.getBean("applicantService");
24+
applicantService.suggestARoleForApplicant(new Applicant("Baljeet",37,1200000.0,8),new SuggestedRole());
25+
26+
ProductService productService=(ProductService)ctx.getBean("productService");
27+
Product returnedProduct=productService.applyLabelToProduct(new Product("Microwave","Book"));
28+
}
29+
30+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.baeldung.spring.drools;
2+
3+
import com.baeldung.spring.drools.service.ApplicantService;
4+
import com.baeldung.spring.drools.service.ProductService;
5+
import org.kie.api.KieBase;
6+
import org.kie.api.KieServices;
7+
import org.kie.api.builder.*;
8+
import org.kie.api.runtime.KieContainer;
9+
import org.kie.api.runtime.KieSession;
10+
import org.kie.internal.io.ResourceFactory;
11+
import org.kie.spring.KModuleBeanFactoryPostProcessor;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.context.annotation.Configuration;
14+
import org.springframework.core.io.Resource;
15+
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
16+
import org.springframework.core.io.support.ResourcePatternResolver;
17+
18+
import java.io.IOException;
19+
20+
@Configuration
21+
public class DroolConfiguration {
22+
23+
private static final String RULES_PATH = "com/baeldung/spring/drools/rules/";
24+
25+
@Bean
26+
public KieFileSystem kieFileSystem() throws IOException {
27+
KieFileSystem kieFileSystem = getKieServices().newKieFileSystem();
28+
for (Resource file : getRuleFiles()) {
29+
kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_PATH + file.getFilename(), "UTF-8"));
30+
}
31+
return kieFileSystem;
32+
}
33+
34+
private Resource[] getRuleFiles() throws IOException {
35+
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
36+
return resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "**/*.*");
37+
}
38+
39+
@Bean
40+
public KieContainer kieContainer() throws IOException {
41+
final KieRepository kieRepository = getKieServices().getRepository();
42+
43+
kieRepository.addKieModule(new KieModule() {
44+
public ReleaseId getReleaseId() {
45+
return kieRepository.getDefaultReleaseId();
46+
}
47+
});
48+
49+
KieBuilder kieBuilder = getKieServices().newKieBuilder(kieFileSystem());
50+
kieBuilder.buildAll();
51+
52+
return getKieServices().newKieContainer(kieRepository.getDefaultReleaseId());
53+
}
54+
55+
private KieServices getKieServices() {
56+
return KieServices.Factory.get();
57+
}
58+
59+
@Bean
60+
public KieBase kieBase() throws IOException {
61+
return kieContainer().getKieBase();
62+
}
63+
64+
@Bean
65+
public KieSession kieSession() throws IOException {
66+
return kieContainer().newKieSession();
67+
}
68+
69+
@Bean
70+
public ApplicantService getApplicantService(){
71+
return new ApplicantService();
72+
}
73+
74+
@Bean
75+
public ProductService getProductService(KieContainer kieContainer){
76+
return new ProductService();
77+
}
78+
79+
@Bean
80+
public KModuleBeanFactoryPostProcessor kiePostProcessor() {
81+
return new KModuleBeanFactoryPostProcessor();
82+
}
83+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.spring.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.spring.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.spring.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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.spring.drools.service;
2+
3+
import com.baeldung.spring.drools.model.Applicant;
4+
import com.baeldung.spring.drools.model.SuggestedRole;
5+
import org.kie.api.runtime.KieContainer;
6+
import org.kie.api.runtime.KieSession;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Service;
9+
10+
@Service
11+
public class ApplicantService {
12+
13+
@Autowired
14+
private KieContainer kieContainer;
15+
16+
public SuggestedRole suggestARoleForApplicant(Applicant applicant,SuggestedRole suggestedRole){
17+
KieSession kieSession = kieContainer.newKieSession();
18+
kieSession.insert(applicant);
19+
kieSession.setGlobal("suggestedRole",suggestedRole);
20+
kieSession.fireAllRules();
21+
System.out.println(suggestedRole.getRole());
22+
return suggestedRole;
23+
24+
}
25+
}

0 commit comments

Comments
 (0)