drools决策表及实例

drools决策表及实例

决策表

  1. 决策表又称判断表,适用于描述处理判断条件较多,各条件又相互组合、有多种决策方案的情况。精确而简洁描述复杂逻辑的方式,将多个条件与这些条件满足后需要执行动作相对应。

  2. 决策表语法

关键字说明
RuleSet等价于drl文件的package,必须,如果没有设置对应的值默认为rule_table
Sequential布尔类型,true表示规则按照表格自上而下顺序执行,false表示乱序,可选
Import等价于drl文件中的import,如果引入多个类,则类之间使用逗号分隔,可选
Variables等价于drl文件中的global,定义全局变量,多个全局变量则使用逗号分隔,可选
RuleTable指示后面会有一批rule,RuleTable的名称会作为以后生成rule的前缀,必须
CONDITION规则条件关键字,相当于drl文件中的when。下面两行则表示LHS部分,第三行则为注释行,从第四行开始每一行表示一条规则,每个规则表至少有一个
ACTION规则结果关键字,相当于drl文件中的then,每个规则表至少有一个
NO-LOOP相当于drl文件中的no-loop,可选
AGENDA-GROUP相当于drl文件中的agenda-group,可选

在决策表中经常使用到的占位符,语法是使用$加数字,用于替换每条规则中设置的具体值。

决策表实例

  1. 创建maven工程drools_demo,并设置pom.xml文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.lxx</groupId>
    <artifactId>drools_demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.12</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-compiler</artifactId>
            <version>7.10.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-templates</artifactId>
            <version>7.10.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-spring</artifactId>
            <version>7.10.0.Final</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-core</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-tx</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.2</version>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <!--修改maven默认的JDK设置-->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
  1. 创建application.yaml配置spring应用
server:
  port: 8088

spring:
  application:
    name: drools_demo
  1. 创建事实对象Person.java
package org.lxx.drools.entity;

import lombok.Data;

@Data
public class Person {

    private int age;
    private String sex;
    private double salary;

}
  1. 创建工具类解析Excel决策表KieSessionUtils.java
package org.lxx.drools.utils;

import org.drools.decisiontable.InputType;
import org.drools.decisiontable.SpreadsheetCompiler;
import org.kie.api.builder.Message;
import org.kie.api.builder.Results;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.internal.utils.KieHelper;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

public class KieSessionUtils {

    private KieSessionUtils(){}

    public static String getDrl(String path) throws Exception {
        File file = new File(path);
        InputStream is = new FileInputStream(file);
        SpreadsheetCompiler compiler = new SpreadsheetCompiler();
        String drl = compiler.compile(is, InputType.XLS);
        System.out.println(drl);
        return drl;
    }

    public static KieSession createKieSessionFromDrl(String drl) throws Exception {
        KieHelper kieHelper = new KieHelper();
        kieHelper.addContent(drl, ResourceType.DRL);
        Results results = kieHelper.verify();
        if (results.hasMessages(Message.Level.WARNING, Message.Level.ERROR)) {
            List<Message> messages = results.getMessages(Message.Level.WARNING, Message.Level.ERROR);
            for (Message message : messages) {
                System.out.println("Error:" + message.getText());
            }
        }
        return kieHelper.build().newKieSession();
    }

    public static KieSession getKieSessionFromXls(String path) throws Exception {
        return createKieSessionFromDrl(getDrl(path));
    }
}
  1. 创建服务类RuleService.java
package org.lxx.drools.service;

import org.kie.api.runtime.KieSession;
import org.lxx.drools.entity.Person;
import org.lxx.drools.utils.KieSessionUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class RuleService {

    public List<String> personCheck(Person person) throws Exception {
        List<String> listRules = new ArrayList<>();
        KieSession session = KieSessionUtils.getKieSessionFromXls("C:\\Users\\asus\\IdeaProjects\\drools_demo\\src\\main\\resources\\rule.xls");
        session.getAgenda().getAgendaGroup("sign").setFocus();
        session.insert(person);
        session.setGlobal("listRules", listRules);
        session.fireAllRules();
        return listRules;
    }


}
  1. 创建控制器RuleController.java
package org.lxx.drools.controller;

import org.apache.commons.collections4.CollectionUtils;
import org.lxx.drools.entity.Person;
import org.lxx.drools.service.RuleService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rule/")
public class RuleController {

    @Resource
    private RuleService ruleService;

    @RequestMapping("personCheck")
    public Map personCheck() {
        Map map = new HashMap();
        Person person = new Person();
        person.setAge(24);
        person.setSalary(15000);
        person.setSex("男");
        try {
            List<String> list = ruleService.personCheck(person);
            if (CollectionUtils.isNotEmpty(list)) {
                map.put("checkResult", false);
                map.put("msg", "失败");
                map.put("detail", list);
            } else {
                map.put("checkResult", true);
                map.put("msg", "成功");
            }
            return map;
        } catch (Exception e) {
            map.put("checkResult", false);
            map.put("msg", "未知错误");
            return map;
        }
    }

}
  • 26
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐江小鱼

知识创造财富,期待您的慷慨解囊

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值