Drools (一) - 简介

本贴最后更新于 1706 天前,其中的信息可能已经时移世改

简介

Drools 引擎是 Drools 的规则引擎。Drools 引擎存储,处理和评估数据,以执行您定义的业务规则或决策模型。Drools 引擎的基本功能是将传入的数据或事实与规则条件相匹配,并确定是否以及如何执行规则

样例

1.新建一个 spring boot 项目

2.引入 maven 库

 <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-core</artifactId>
            <version>7.34.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-compiler</artifactId>
            <version>7.34.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-templates</artifactId>
            <version>7.34.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-api</artifactId>
            <version>7.34.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-spring</artifactId>
            <version>7.34.0.Final</version>
        </dependency>

3.新建一个配置类如下

import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieRepository;
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.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author yudong
 * @version 1.0
 * @date 2020/3/23 5:37 下午
 * @modified By
 */
@Component
@Configurable
public class DroolsConfig {

    private final KieServices kieServices = KieServices.Factory.get();

    @Bean
    public KieFileSystem kieFileSystem() throws IOException {
        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        Resource[] files = resourcePatternResolver.getResources("classpath*:*.drl");
        for (Resource file : files) {
            kieFileSystem.write(ResourceFactory.newClassPathResource(file.getFilename(), "UTF-8"));
        }
        return kieFileSystem;
    }

    @Bean
    public KieContainer kieContainer( KieFileSystem kieFileSystem) throws IOException {
        KieRepository kieRepository = kieServices.getRepository();
        kieRepository.addKieModule(kieRepository::getDefaultReleaseId);
        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
        kieBuilder.buildAll();
        return kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
    }


    @Bean
    public KieBase kieBase( KieContainer kieContainer) throws IOException {
        return kieContainer.getKieBase();
    }
    @Bean
    public KieSession kieSession( KieContainer kieContainer) throws IOException {
        return kieContainer.newKieSession();
    }
    @Bean
    public KModuleBeanFactoryPostProcessor kiePostProcessor() {
        return new KModuleBeanFactoryPostProcessor();
    }
}

4.创建两个实体类 QueryParam 和 RuleResult

/**
 * @author yudong
 * @version 1.0
 * @date 2020/3/23 6:15 下午
 * @modified By
 */
public class QueryParam {

    private String test;

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }
}
/**
 * @author yudong
 * @version 1.0
 * @date 2020/3/23 6:16 下午
 * @modified By
 */
public class RuleResult {

    private Boolean result;

    public Boolean getResult() {
        return result;
    }

    public void setResult(Boolean result) {
        this.result = result;
    }
}

5.在 resources 资源文件夹下创建 test.drl 文件,内容如下

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...