1. Background
In the previous article, we learned some basic syntax of drools
, in this article, we implemented the integration in SpringBoot
drools
.
2. Demand
We need to determine whether the user can play the game in drools
.
规则一:
If the user's age age>=18
, then 可以
play the game.
规则二:
if the user's age age<18
, then 不可以
play the game.
3. Realize
3.1 Introducing jar package
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-bom</artifactId>
<type>pom</type>
<version>7.69.0.Final</version>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.6.7</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-mvel</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
</dependencies>
The drools
springboot
jar
package is introduced here.
3.2 Write drools configuration class
@Configuration
public class DroolsConfig {
@Bean
public KieContainer kieContainer() {
KieServices kieServices = KieServices.get();
return kieServices.getKieClasspathContainer();
}
}
3.3 Writing the Person object
It can be simply understood as the drools
Fact
object in ---93bec35fad740d619c00c57d3f4bcfe2---. Inserted into working memory in drools is the Fact
object.
@Data
@AllArgsConstructor
public class Person {
private String name;
private Integer age;
// 是否可以玩游戏,此字段的值,由 drools 引擎计算得出
private Boolean canPlayGame;
}
canPlayGame
: The value of this field is calculated by the drools
engine.
3.4 Write drl file
package rules
import com.huan.drools.fact.Person
// 用户可以玩游戏
rule "rule_person_can_play_game"
when
$person: Person( age >= 18)
then
$person.setCanPlayGame(true);
System.out.println("触发规则:" + drools.getRule().getName());
end
// 用户不可以玩游戏
rule "rule_person_Can't_play_game"
when
$person: Person( age < 18)
then
$person.setCanPlayGame(false);
System.out.println("触发规则:" + drools.getRule().getName());
end
There are two rules written above, both of which are judged according to age
, so as to calculate whether the user can play the game canPlayGame
.
3.5 Write the kmodule.xml file
The path of ---aa24484f2aedef4b0a0b0fa5a067d205 kmodule.xml
is located in the src/main/resources/META-INF
directory.
<kmodule xmlns="http://www.drools.org/xsd/kmodule">
<kbase name="kabse" packages="rules" default="false">
<ksession name="ksession" default="false" type="stateful">
<consoleLogger/>
<listeners>
<ruleRuntimeEventListener type="org.kie.api.event.rule.DebugRuleRuntimeEventListener"/>
<processEventListener type="org.drools.core.event.DebugProcessEventListener"/>
<agendaEventListener type="org.drools.core.event.DebugAgendaEventListener"/>
</listeners>
</ksession>
</kbase>
</kmodule>
3.6 Write Controller trigger rules
@RestController
public class PersonController {
private final KieContainer kieContainer;
public PersonController(KieContainer kieContainer) {
this.kieContainer = kieContainer;
}
/**
* 调用drools rule判断用户是否可以玩游戏
*/
@GetMapping("canPlayGame")
public Person canPlayGame(Person person) {
KieSession kieSession = kieContainer.newKieSession("ksession");
try {
kieSession.insert(person);
kieSession.fireAllRules();
} finally {
kieSession.dispose();
}
return person;
}
}
3.7 Testing
3.7.1 User's age >= 18 years old
➜ curl http://localhost:8081/canPlayGame\?name\=huan\&age\=20
{"name":"huan","age":20,"canPlayGame":true}
It can be seen that the value calculated in drools canPlayGame
value is `true
3.7.2 User's age < 18 years old
➜ curl http://localhost:8081/canPlayGame\?name\=huan\&age\=16
{"name":"huan","age":20,"canPlayGame":false}
It can be seen that the value calculated in drools canPlayGame
is false
4. Complete code
https://gitee.com/huan1993/spring-cloud-parent/tree/master/drools/drools-integrated-springboot
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。