码迷,mamicode.com
首页 > 数据库 > 详细

SpringBoot+Drools使用数据库

时间:2019-06-03 12:18:42      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:group   builder   fas   resource   pen   engine   数据   方式   区别   

说明

此配置方式为打成jar包,不直接连接数据库,请根据不同项目区别自行修改,

POM依赖

<?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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>com.drools</groupId>
    <artifactId>finance-api</artifactId>
    <packaging>jar</packaging>
    <version>2.0.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.jeval</groupId>
            <artifactId>jeval</artifactId>
            <version>0.9.4</version>
        </dependency>
        <!--规则引擎Dtools -->
        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-spring</artifactId>
            <version>7.0.0.Final</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-tx</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-core</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-compiler</artifactId>
            <version>7.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-core</artifactId>
            <version>7.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.28</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.9</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.0.12</version>
        </dependency>
    </dependencies>
</project>

建表SQL

CREATE TABLE `drools_rule` (
  `rule_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 主键,
  `rule_uuid` varchar(64) DEFAULT NULL COMMENT 业务主键,
  `rule_content` longtext COMMENT 规则,
  PRIMARY KEY (`rule_id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8;

使用

插入一条规则

INSERT INTO `drools_rule` (`rule_id`, `rule_uuid`, `rule_content`) VALUES (1, repayment, package rules\r\n\r\nimport com.utonw.finance.vo.FinanceRepaymentVo\r\nimport java.math.BigDecimal\r\n\r\nrule \"FirstInterest\"\r\n no-loop true\r\n lock-on-active true\r\n salience 1\r\n when\r\n repaymentVo : FinanceRepaymentVo(repaymentPlanVoList != null && repaymentPlanVoList.size() >= periods)\r\n then\r\n repaymentVo.addFee(\"repayInterest\",\"FirstInsterest_TotalInterestFee\");\r\n repaymentVo.addFee(\"repayManage\",\"FirstInsterest_TotalManagerFee\");\r\n repaymentVo.addFee(\"repayTotal\",\"FirstInsterest_TotalAllFee\");\r\n update(repaymentVo);\r\nend);

 

获取Drools的StatefulKnowledgeSession,然后加载规则

public class FinanceDroolsRuleServiceImpl implements FinanceDroolsRuleService {

    @Autowired
    FinanceDroolsRuleMapper droolsRuleMapper;

    @Override
    public List<FinanceDroolsRule> listRules() {
        return new FinanceDroolsRule().selectList(new EntityWrapper());
    }

    @Override
    public KnowledgeBase getKnowledgeBase() {

        try {
            KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
            //装入规则,可以装入多个
            List<FinanceDroolsRule> financeDroolsRuleList = listRules();
            for (FinanceDroolsRule financeDroolsRule : financeDroolsRuleList) {
                kb.add(ResourceFactory.newByteArrayResource(financeDroolsRule.getRuleContent().getBytes("utf-8")), ResourceType.DRL);
            }
            KnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
            kBase.addKnowledgePackages(kb.getKnowledgePackages());
            return kBase;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public StatefulKnowledgeSession getStatefulKnowledgeSession() {
        StatefulKnowledgeSession statefulKnowledgeSession = getKnowledgeBase().newStatefulKnowledgeSession();
        return statefulKnowledgeSession;
    }
}

使用规则

public class FinanceRepaymentServiceImpl implements FinanceRepaymentService {

    @Autowired
    private FinanceDroolsRuleService financeDroolsRuleService;

    @Override
    public FinanceRepaymentVo getRepayment() {try {
            StatefulKnowledgeSession statefulKnowledgeSession = financeDroolsRuleService.getStatefulKnowledgeSession();
            statefulKnowledgeSession.insert();
            statefulKnowledgeSession.fireAllRules();
            statefulKnowledgeSession.dispose();
            return repaymentVo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
}

规则怎么写其他博客中很多,就不写了,自行查找吧,代码已上传github

https://github.com/lwc398555797/springboot-drools.git

SpringBoot+Drools使用数据库

标签:group   builder   fas   resource   pen   engine   数据   方式   区别   

原文地址:https://www.cnblogs.com/li398555797/p/10966717.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!