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;
    }
}
相关推荐
记得开心一点嘛7 小时前
手搓Springboot
java·spring boot·spring
shallwe小威11 小时前
SpringBoot集成ElasticSearch
数据库·spring boot·elasticsearch
Q_Q196328847512 小时前
python+springboot+uniapp微信小程序题库系统 在线答题 题目分类 错题本管理 学习记录查询系统
spring boot·python·django·uni-app·node.js·php
AD钙奶-lalala15 小时前
Spring Initializr(或者 IDEA 里新建 Spring Boot 项目)时 Dependencies 的选择
spring boot·spring·intellij-idea
风槐啊18 小时前
邪修实战系列(3)
java·ide·spring boot·spring·tomcat
毕设源码-朱学姐18 小时前
【开题答辩全过程】以 _基于SpringBoot技术的“树洞”心理咨询服务平台的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
哈喽姥爷19 小时前
Spring Boot--yml配置信息书写和获取
java·数据库·spring boot·mybatis
武昌库里写JAVA19 小时前
Java设计模式中的几种常用设计模式
vue.js·spring boot·sql·layui·课程设计