[drools]spring-boot使用drools规则引擎

一、springboot项目引用依赖如下(exclusion部分,大家根据自己的情况处理):

        <!--drools规则引擎-->
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-core</artifactId>
            <version>7.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-compiler</artifactId>
            <version>7.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-decisiontables</artifactId>
            <version>7.0.0.Final</version>
            <exclusions>
                <exclusion>
                    <artifactId>poi</artifactId>
                    <groupId>org.apache.poi</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>poi-ooxml</artifactId>
                    <groupId>org.apache.poi</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-templates</artifactId>
            <version>7.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-spring</artifactId>
            <version>7.0.0.Final</version>
        </dependency>

二、规则编写在如下图的层级(大家也可以按照情况自己选择)

在这里插入图片描述

三、编写config配置文件

可以看到,这里也指定了步骤二的规则文件的所在目录

@Configuration
public class DroolsAutoConfiguration {

    private static final String RULES_PATH = "rules/";

    @Bean
    @ConditionalOnMissingBean(KieFileSystem.class)
    public KieFileSystem kieFileSystem() throws IOException {
        KieFileSystem kieFileSystem = getKieServices().newKieFileSystem();
        for (Resource file : getRuleFiles()) {
            kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_PATH + file.getFilename(), "UTF-8"));
        }
        return kieFileSystem;
    }

    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(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 {
        KieSession kieSession = kieContainer().newKieSession();
        return kieSession;
    }

    @Bean
    @ConditionalOnMissingBean(KModuleBeanFactoryPostProcessor.class)
    public KModuleBeanFactoryPostProcessor kiePostProcessor() {
        return new KModuleBeanFactoryPostProcessor();
    }
}

四、开始使用

1)自动注入:

    @Autowired
    private KieSession kieSession;

2)例子:

    /**
     * 通过参数,获取对应的模板key
     * @param contractType 合同类型
     * @param subjectId 合同主体Id,对应t_contract_detail的id
     * @param subjectName 合同主体名称
     * @param workAreaName 工作地点
     * @return String
     */
    public String getContractTemplateKeyFormRules(String contractType,Long subjectId,String subjectName, String workAreaName){
        // load up the knowledge base
        EntryContractTemplateRuleParam ruleParam = new EntryContractTemplateRuleParam();
        ruleParam.setContractType(Optional.ofNullable(contractType).orElse(""));
        ruleParam.setSubjectId(Optional.ofNullable(subjectId).orElse(-1L));
        ruleParam.setSubjectName(Optional.ofNullable(subjectName).orElse(""));
        ruleParam.setWorkAreaName(Optional.ofNullable(workAreaName).orElse(""));
        ruleParam.setActive(active);
        //插入
        kieSession.insert(ruleParam);
        //执行规则
        kieSession.fireAllRules();
        return ruleParam.getTemplateKey();
    }

3)规则编写例子(就是写在EntryContractTemplateRule.drl中的内容):
通过在java代码中传入参数2)中的参数,规则引擎会解析到EntryContractTemplateRule.drl中配置的规则,然后规则中使用m.setTemplateKey(“0_0”);
进行赋值并返回,如果命中如下的规则getContractTemplateKeyFormRules方法就会返回字符串"0_0",完成本次规则的命中。

rule "XX劳动合同模板"
salience 0
no-loop true
lock-on-active
    when
        m : EntryContractTemplateRuleParam(!active.equals("production") && contractType.equals("1") &&
        subjectId != 590 &&
        subjectId != 154 &&
        subjectId != 589 &&
        subjectId != 67 &&
        subjectId != 75 &&
        subjectId != 173)
    then
        m.setTemplateKey("0_0");
        
end

五、快速教程结束

发布了130 篇原创文章 · 获赞 36 · 访问量 43万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览