欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot整合Drools的实现步骤

程序员文章站 2022-06-25 16:46:23
drools有什么用从我个人所待过的公司,其中做智能酒店这个项目时就用到规则引擎drools,将它用于处理优惠劵规则。springboot整合drools初步实战1.导入maven依赖

drools有什么用

从我个人所待过的公司,其中做智能酒店这个项目时就用到规则引擎drools,将它用于处理优惠劵规则。

springboot整合drools初步实战

1.导入maven依赖

<properties>
<drools.version>7.14.0.final</drools.version>
</properties>

<!-- drools -->
<dependency>
    <groupid>org.drools</groupid>
    <artifactid>drools-compiler</artifactid>
    <version>${drools.version}</version>
</dependency>

2.编写配置类

package com.springcloud.blog.admin.config;

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.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;


@configuration
public class kiasessionconfig {

    private static final string rules_path = "rules/";

    @bean
    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();
        final resource[] resources = resourcepatternresolver.getresources("classpath*:" + rules_path + "**/*.*");
        return resources;

    }

    @bean
    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
    public kiebase kiebase() throws ioexception {
        return kiecontainer().getkiebase();
    }

    @bean
    public kiesession kiesession() throws ioexception {
        return kiecontainer().newkiesession();
    }
}

3.resources目录新建rules目录

4.新建实体

package com.springcloud.blog.admin.drools;

public class people {
    private int sex;

    private string name;

    private string drltype;

    public int getsex() {
        return sex;
    }

    public void setsex(int sex) {
        this.sex = sex;
    }

    public string getname() {
        return name;
    }

    public void setname(string name) {
        this.name = name;
    }

    public string getdrltype() {
        return drltype;
    }

    public void setdrltype(string drltype) {
        this.drltype = drltype;
    }
}

5.编写规则文件

package com.springcloud.blog.admin.drools
import com.springcloud.blog.admin.drools.people
dialect  "java"

rule "man"
    when
        $p : people(sex == 1 && drltype == "people")
    then
        system.out.println($p.getname() + "是男孩");
end

6.单元测试(只要正常输出,表示整合是ok的,接下来就可以任意应用了)

package com.springcloud.blog.base.controller.test.task;

import com.springcloud.blog.admin.blogadminapplication;
import com.springcloud.blog.admin.drools.people;
import org.junit.test;
import org.junit.runner.runwith;
import org.kie.api.kiebase;
import org.kie.api.runtime.kiesession;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import org.springframework.test.context.junit4.springrunner;

@runwith(springrunner.class)
@springboottest(classes = blogadminapplication.class, webenvironment = springboottest.webenvironment.random_port)
public class droolsjunittest {

    @autowired
    private kiesession session;

    @test
    public void people() {

        people people = new people();
        people.setname("yc");
        people.setsex(1);
        people.setdrltype("people");
        session.insert(people);//插入
        session.fireallrules();//执行规则
    }


}

7.输出结果

yc是男孩

以上就是springboot整合drools的实现步骤的详细内容,更多关于springboot整合drools的资料请关注其它相关文章!

相关标签: SpringBoot Drools