侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

崛起于Springboot2.X + 规则引擎Drools7.0(41)

2023-12-11 星期一 / 0 评论 / 0 点赞 / 47 阅读 / 7246 字

《SpringBoot2.X心法总纲》 1、创建Springboot项目 2、添加pom其他依赖 <dependency> <groupId>org.kie</groupId> <ar

《SpringBoot2.X心法总纲》

1、创建Springboot项目

2、添加pom其他依赖

<dependency>    <groupId>org.kie</groupId>    <artifactId>kie-spring</artifactId>    <version>7.11.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.11.0.Final</version></dependency>

3、创建实体类

@Datapublic class Address {    private String postCode;    private String street;    private String state;}
@Datapublic class AddressCheckResult {    //true通过校验,false 未通过    private boolean postCodeResult = false;}

4、配置类

import org.kie.api.KieBase;import org.kie.api.KieServices;import org.kie.api.builder.*;import org.kie.api.runtime.KieContainer;import org.kie.api.runtime.KieSession;import org.kie.internal.io.ResourceFactory;import org.kie.spring.KModuleBeanFactoryPostProcessor;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.Resource;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.core.io.support.ResourcePatternResolver;import java.io.IOException;@Configurationpublic class DroolsAutoConfiguration {    //rule的存放位置    private static final String RULES_PATH = "rules/";    @Bean    @ConditionalOnMissingBean(KieFileSystem.class)    public KieFileSystem kieFileSystem() throws IOException {        KieFileSystem kieFileSystem = getKieServices().newKieFileSystem();        for (Resource file : getRuleFiles()) {            String path = RULES_PATH + file.getFilename();            kieFileSystem.write(ResourceFactory.newClassPathResource(path, "UTF-8"));        }        return kieFileSystem;    }    /**     * 这里要引入 org.springframework.core.io.Resource  包     *     * @return     * @throws IOException     */    private Resource[] getRuleFiles() throws IOException {        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();        return resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "**/*.*");    }    @Bean    @ConditionalOnMissingBean(KieContainer.class)    public KieContainer kieContainer() throws IOException {        final KieRepository kieRepository = getKieServices().getRepository();        kieRepository.addKieModule(new KieModule() {            public ReleaseId getReleaseId() {                return kieRepository.getDefaultReleaseId();            }        });        KieBuilder kieBuilder = getKieServices().newKieBuilder(kieFileSystem());        kieBuilder.buildAll();        return getKieServices().newKieContainer(kieRepository.getDefaultReleaseId());    }    private KieServices getKieServices() {        return KieServices.Factory.get();    }    @Bean    @ConditionalOnMissingBean(KieBase.class)    public KieBase kieBase() throws IOException {        return kieContainer().getKieBase();    }    @Bean    @ConditionalOnMissingBean(KieSession.class)    public KieSession kieSession() throws IOException {        return kieContainer().newKieSession();    }    @Bean    @ConditionalOnMissingBean(KModuleBeanFactoryPostProcessor.class)    public KModuleBeanFactoryPostProcessor kiePostProcessor() {        return new KModuleBeanFactoryPostProcessor();    }}

5、创建drools特有的xml

<?xml version="1.0" encoding="UTF-8" ?><kmodule xmlns="http://www.drools.org/xsd/kmodule">    <kbase name="rules" packages="mujiutian.rules">        <ksession name="ksession-rule"/>    </kbase></kmodule>

      这个packages 要和下面的规则文件对应的名字一致,不然会报错。

6、编写drools规则

package mujiutian.rules;import com.mjt.drools.entity.Address;import com.mjt.drools.entity.AddressCheckResult;rule "Rules"    when        adress : Address(postCode != null,postCode matches "([0-9]{5})")        checkResult: AddressCheckResult();    then        checkResult.setPostCodeResult(true);        System.out.println("规则中打印日志:校验通过");end

      这个package默认生成是static,要修改一些,不然会报错,同时要和上面xml中的内容一样才行,rule 后面加的"" 是描述内容,可以写任何内容。

7、编写controller层

import org.kie.api.runtime.KieSession;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController@RequestMapping(value = "/drools")public class DroolsController {    @Resource    KieSession kieSession;    @GetMapping(value = "/address")    public void test1(){        Address address = new Address();        address.setPostCode("99425");        AddressCheckResult result = new AddressCheckResult();        kieSession.insert(address);        kieSession.insert(result);        int ruleFiredCount = kieSession.fireAllRules();        System.out.println("触发了"+ruleFiredCount+"条规则");        if (result.isPostCodeResult()){            System.out.println("规则校验通过");        }    }}

8、运行测试

      借鉴于:https://blog.csdn.net/qq_21383435/article/details/82873711  基本完全复制的代码,如果可以也可以进入他的博客专栏,感谢!

      ok~

广告 广告

评论区