Drools入门(一)——环境搭建

目的

本篇讲的是Drools规则引擎的基本使用,什么是规则引擎、以及搞清楚规则引擎在什么情况下比较适合使用。Drools支持以drl文件结尾的文件作为规则文件,也支持其他结尾的文件作为规则文件比如gdrlrdrldsl等(详情可见ResourceType.class),Drools还支持工作流等其他功能,本篇主要讲解的是drl规则文件的使用,不涉及工作流等其他功能,将分为几个章节进行讲解

准备环境

JDK (版本随意,作者使用1.8)
Maven (版本随意,作者使用3.6.3)
IDEA (版本随意,作者使用2020.1)

准备工作

新建一个maven项目,打开pom.xml导入Drools依赖,下面为剔除spring依赖的导入,没有spring依赖的项目可以不导入kie-spring

<!--drools-->
<dependency>
    <groupId>org.kie</groupId>
    <artifactId>kie-spring</artifactId>
    <version>7.37.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>
        <exclusion>
            <artifactId>commons-lang3</artifactId>
            <groupId>org.apache.commons</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.drools</groupId>
    <artifactId>drools-compiler</artifactId>
    <version>7.37.0.Final</version>
</dependency>

在maven项目的resources下面新建一个rules目录并在rules目录下新建demo.drl规则文件,目录的路径于规则里面的包名保持一致,否则会报警告虽然不影响使用

package rules

rule "test"
    when
        eval(true)
    then
        System.out.println("规则中打印日志:校验通过!");
end

main方法中添加一下代码并运行

public static void main(String[] args) {
    KieServices kieServices = KieServices.Factory.get();
    KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
    try {
        Enumeration<URL> resources = Demo.class.getClassLoader().getResources("rules/demo.drl");
        while (resources.hasMoreElements()){
            URL url = resources.nextElement();
            kieFileSystem.write(ResourceFactory.newFileResource(URLDecoder.decode(url.getPath())));
        }
        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
        kieBuilder.buildAll();
        KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
        KieSession defaultKieSession = kieContainer.newKieSession(); //等价于KieSession defaultKieSession = kieContainer.newKieSession("defaultKieSession");
        defaultKieSession.fireAllRules();
        defaultKieSession.dispose();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

运行结果如下,打印出了规则中的结果

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
规则中打印日志:校验通过!

Process finished with exit code 0
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

友情链接更多精彩内容