springboot整合drools规则引擎 示例入门

实现需求:

年龄>=18可以玩游戏,否则不可以玩游戏
整体文件目录结构如下

1 pom.xml

复制代码
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.drools</groupId>
                <artifactId>drools-bom</artifactId>
                <type>pom</type>
                <version>7.69.0.Final</version>
                <scope>import</scope>
            </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>

2 drools配置文件

复制代码
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DroolsConfig {

    @Bean
    public KieContainer kieContainer() {
        KieServices kieServices = KieServices.get();
        return kieServices.getKieClasspathContainer();
    }
}

3 META-INF/kmodule.xml 文件编写

复制代码
<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>

4 rules/rule1.drl规则文件编写

复制代码
package rules

import cn.beijing.model.Person

// 用户可以玩游戏
rule "用户可以玩游戏"
    when
        $person: Person( age >= 18)
    then
        $person.setCanPlayGame(true);
        System.out.println("触发规则:" + drools.getRule().getName());
end

// 用户不可以玩游戏
rule "用户不可以玩游戏"
    when
        $person: Person( age < 18)
    then
        $person.setCanPlayGame(false);
        System.out.println("触发规则:" + drools.getRule().getName());
end

5 pojo文件

复制代码
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Person {
    private String name;
    private Integer age;
    // 是否可以玩游戏,此字段的值,由 drools 引擎计算得出
    private Boolean canPlayGame;
}

6 调用测试

复制代码
import cn.beijing.model.Person;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PersonController {

    @Autowired
    private 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;
    }
}
相关推荐
LiRuiJie8 分钟前
深入剖析Spring Boot / Spring 应用中可自定义的扩展点
java·spring boot·spring
尚学教辅学习资料2 小时前
Ruoyi-vue-plus-5.x第五篇Spring框架核心技术:5.1 Spring Boot自动配置
vue.js·spring boot·spring
晚安里3 小时前
Spring 框架(IoC、AOP、Spring Boot) 的必会知识点汇总
java·spring boot·spring
上官浩仁3 小时前
springboot ioc 控制反转入门与实战
java·spring boot·spring
叫我阿柒啊4 小时前
从Java全栈到前端框架:一位程序员的实战之路
java·spring boot·微服务·消息队列·vue3·前端开发·后端开发
中国胖子风清扬4 小时前
Rust 序列化技术全解析:从基础到实战
开发语言·c++·spring boot·vscode·后端·中间件·rust
JosieBook8 小时前
【SpringBoot】21-Spring Boot中Web页面抽取公共页面的完整实践
前端·spring boot·python
刘一说9 小时前
Spring Boot+Nacos+MySQL微服务问题排查指南
spring boot·mysql·微服务
叫我阿柒啊12 小时前
从Java全栈到云原生:一场技术深度对话
java·spring boot·docker·微服务·typescript·消息队列·vue3
计算机毕设定制辅导-无忧学长13 小时前
MQTT 与 Java 框架集成:Spring Boot 实战(一)
java·网络·spring boot