Java教程

drools规则的入门使用

本文主要是介绍drools规则的入门使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1:pom文件

 

<!--drools-->
<dependency>
    <groupId>org.drools</groupId>
    <artifactId>drools-compiler</artifactId>
    <version>7.10.0.Final</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

2.新建 kmodule.xml

路径 resources\META-INF\kmodule.xml

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

3.drl 文件
路径 resources\rules\test.drl
import com.example.serviceorder.vo.Person

dialect  "mvel"

rule "age"
    when
        $person : Person(name=='小明')
    then
        $person.setAge(11);
       System.out.println("我叫:"+$person.getName()+",现在:"+$person.getAge()+"岁");
end

rule  "age1"
    when
        $person : Person(name=='小黑')
    then
        $person.setAge(13);
      System.out.println("我叫:"+$person.getName()+",现在:"+$person.getAge()+"岁");
end

rule "age2"
    when
        $person : Person(name=='小白')
    then
        $person.setAge(14);
      System.out.println("我叫:"+$person.getName()+",现在:"+$person.getAge()+"岁");
end
4. 实体
package com.example.serviceorder.vo;

/**
 * @author lwq
 * @date 2022/6/5 21:38
 * @desciption: 测试
 */
public class Person {

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

5.测试类

private static KieContainer container = null;
private KieSession statefulKieSession = null;

@Test
public void test111() {

    KieServices kieServices = KieServices.Factory.get();
    container = kieServices.getKieClasspathContainer();
    statefulKieSession = container.newKieSession("all-rules");
    Person person = new Person();


    person.setName("小白");

    statefulKieSession.insert(person);
    statefulKieSession.fireAllRules();
    statefulKieSession.dispose();
  System.out.println(JSON.toJSON(person));
}
6.打印结果
{"name":"小白","age":14}

7.动态加载规则工具类

package com.example.serviceorder.util;

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;

/**
 * @author lwq
 * @date 2022/6/5 22:02
 * @desciption: Drools工具类
 */
public class DroolsUtil {
    private static KieContainer container = null;

    public static <T> T execute(T t, String rule) {

        container = load(rule);
        KieSession kieSession = container.newKieSession();
        kieSession.insert(t);
        kieSession.fireAllRules();
        kieSession.dispose();
        return t;
    }


    private static KieContainer load(String rule) {

        KieServices kieServices = KieServices.Factory.get();
        KieRepository repository = kieServices.getRepository();

        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
        kieFileSystem.write("src/main/resources/rules" + "test.drl", rule);
        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
        kieBuilder.buildAll();

        return kieServices.newKieContainer(repository.getDefaultReleaseId());

    }
}
8.测试动态加载规则工具类
@Test
public void test1112() {
//rule 规则可以存在数据库一个字段里面 oracle 数据库可以使用 clob 类型
//mysql 数据库可以使用 longtext类型
//也可以放到 redis 里面
    String rule="\n" +
            "import com.example.serviceorder.vo.Person\n" +
            "\n" +
            "dialect  \"mvel\"\n" +
            "\n" +
            "rule \"age\"\n" +
            "    when\n" +
            "        $person : Person(name=='小明')\n" +
            "    then\n" +
            "        $person.setAge(11);\n" +
            "       System.out.println(\"我叫:\"+$person.getName()+\",现在:\"+$person.getAge()+\"岁\");\n" +
            "end\n" +
            "\n" +
            "rule  \"age1\"\n" +
            "    when\n" +
            "        $person : Person(name=='小黑')\n" +
            "    then\n" +
            "        $person.setAge(13);\n" +
            "      System.out.println(\"我叫:\"+$person.getName()+\",现在:\"+$person.getAge()+\"岁\");\n" +
            "end\n" +
            "\n" +
            "rule \"age2\"\n" +
            "    when\n" +
            "        $person : Person(name=='小白')\n" +
            "    then\n" +
            "        $person.setAge(14);\n" +
            "      System.out.println(\"我叫:\"+$person.getName()+\",现在:\"+$person.getAge()+\"岁\");\n" +
            "end";

    Person person = new Person();

 
    person.setName("小明");

    DroolsUtil.execute(person, rule);

    System.out.println(JSON.toJSON(person));
}

6.打印结果
{"name":"小明","age":11}


 
这篇关于drools规则的入门使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!