springboot整合drools

1、pon.xml中添加drools依赖包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bek</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        <!--druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>cn.easyproject</groupId>
            <artifactId>orai18n</artifactId>
            <version>12.1.0.2.0</version>
        </dependency>
        <!--devtools热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>true</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <exclusions>
                <!-- 单元测试不使用Junit4,使用Junit5 -->
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.github.hongwen1993</groupId>
            <artifactId>fast-drools-spring-boot-starter</artifactId>
            <version>8.0.7</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.4.4</version>
                <configuration>
                    <fork>true</fork> <!-- 如果没有该配置,devtools不会生效 -->
                </configuration>
            </plugin>
        </plugins>
    </build>
    <!--阿里中央仓库-->
    <repositories>
        <repository>
            <id>maven-ali</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public//</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>fail</checksumPolicy>
            </snapshots>
        </repository>
    </repositories>
</project>

2、创建UserInfo实体类

public class UserInfo {
    private String username;

    private String password;

    private int age;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                '}';
    }
}

3、properties文件中配置扫描规则文件

type=com.alibaba.druid.pool.DruidDataSource
server.port=8088
spring.datasource.druid.second.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=demo
spring.datasource.password=demo123456

mybatis.type-aliases-package=com.boerkang.**.entity
mybatis.mapper-locations=classpath:mapper/*.xml
spring.devtools.remote.restart.enabled=true
spring.drools.path=classpath:droolRule/*.drl

4、resources中创建META-INF文件夹,并在该文件夹中创建kmodule.xml文件

<kmodule xmlns="http://www.drools.org/xsd/kmodule">
<kbase name="myKbase1" packages="droolRule" default="true">
    <!--
        name:指定ksession名称,可以任意,但是需要唯一
        packages:指定规则文件的目录,需要根据实际情况填写,否则无法加载到规则文件
        default:指定当前session是否为默认
    -->
    <ksession name="ksession-rule" default="true"/>
</kbase>
</kmodule>

5、resources中创建放置.drl(规则文件)文件的文件夹,并创建测试规则

package droolRule //包名 必须

import com.boerkang.demo.entity.UserInfo
// 根据名字匹配指定的人
rule "1.姓名找到目标人"
    when
        $p : UserInfo( username == "张三" )
    then
        System.err.println("Rule name is [" + drools.getRule().getName() + "]");
end

// 根据年龄匹配指定人
rule "2.年龄找到目标人"
    when
        $p : UserInfo( age >= 25 && age < 65 )
    then
        System.err.println( $p.toString() + " is a work person!" );
end

6、进行单元测试

mport com.boerkang.demo.entity.UserInfo;
import com.drools.core.KieTemplate;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class DroolsApplicationTest {
    @Autowired
    private KieTemplate kieTemplate;

    private KieSession kSession;

    @BeforeAll
    public void initKieSession() {
        // 每次测试都要重新获取kSession
        // 同时可以指定要加载的规则文件
        kSession = kieTemplate.getKieSession("rules1.drl");
    }

    @AfterAll
    public void runDispose() {
        // 全部测试用例执行完毕后,需要手动释放资源
        kSession.dispose();
    }

    @Test
    public void test1findTargetPerson() {
        UserInfo user1 = new UserInfo();
        user1.setUsername("张三");
        kSession.insert(user1);

        UserInfo user2 = new UserInfo();
        user2.setUsername("李四");
        kSession.insert(user2);

        int rules = kSession.fireAllRules();
        System.err.println(rules);
    }

    @Test
    public void test2findWorkPerson() {
        UserInfo user1 = new UserInfo();
        user1.setAge(33);
        kSession.insert(user1);

        UserInfo user2 = new UserInfo();
        user2.setAge(28);
        kSession.insert(user2);

        int rules = kSession.fireAllRules();
        System.err.println(rules);
    }

6、运行结果

 

 7、项目结构

 

posted @ 2021-11-05 17:03  过氧化氢  阅读(380)  评论(0编辑  收藏  举报