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;
    }
}
相关推荐
计算机毕业设计木哥2 小时前
计算机毕业设计选题推荐:基于SpringBoot和Vue的快递物流仓库管理系统【源码+文档+调试】
java·vue.js·spring boot·后端·课程设计
Chan165 小时前
流量安全优化:基于 Sentinel 实现网站流量控制和熔断
java·spring boot·安全·sentinel·intellij-idea·进程
勇往直前plus6 小时前
如何利用docker部署springboot应用
spring boot·docker·容器
ZhengEnCi6 小时前
@RequestParam 注解完全指南-从参数绑定到接口调用的Web开发利器
java·spring boot
=>>漫反射=>>6 小时前
单元测试 vs Main方法调试:何时使用哪种方式?
java·spring boot·单元测试
ZhengEnCi6 小时前
@Parameter 注解技术解析-从 API 文档生成到接口描述清晰的 SpringBoot 利器
java·spring boot
junnhwan8 小时前
【苍穹外卖笔记】Day04--套餐管理模块
java·数据库·spring boot·后端·苍穹外卖·crud
低音钢琴8 小时前
【SpringBoot从初学者到专家的成长15】MVC、Spring MVC与Spring Boot:理解其差异与联系
spring boot·spring·mvc
摇滚侠8 小时前
Spring Boot 3零基础教程,条件注解,笔记09
java·spring boot·笔记
南瓜小米粥、8 小时前
从可插拔拦截器出发:自定义、注入 Spring Boot、到生效路径的完整实践(Demo 版)
java·spring boot·后端