码迷,mamicode.com
首页 > 其他好文 > 详细

drools7 (四、FactHandle 介绍)

时间:2018-01-24 00:35:22      阅读:838      评论:0      收藏:0      [点我收藏+]

标签:finish   default   resources   mcr   aws   nbsp   process   gen   介绍   

先看代码

Base.java
package cn.xiaojf.drools7.base;

import org.apache.commons.lang3.StringUtils;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

public class Base {
    public KieSession getSession() {
        KieServices kieServices = KieServices.Factory.get();
        KieContainer kieContainer = kieServices.getKieClasspathContainer();
        //kmodule.xml 中定义的 ksession name
        KieSession kieSession = kieContainer.newKieSession("all-rules");
        return kieSession;
    }

    public KieSession getSession(String agendaGroupName) {
        KieSession session = this.getSession();
        if (StringUtils.isNoneBlank(agendaGroupName)) {
            session.getAgenda().getAgendaGroup(agendaGroupName).setFocus();
        }

        return session;
    }
}
FaceHandleTest.java

package day20180123;

import cn.xiaojf.drools7.base.Base;
import cn.xiaojf.drools7.model.Person;
import org.junit.Test;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.FactHandle;

import java.util.logging.Handler;

/**
 * 测试 Facehandler
 * @author xiaojf 2018/1/23 21:35
 */
public class FaceHandleTest extends Base {
    @Test
    public void test() {
        KieSession kieSession = super.getSession("faceHandleTest");

        Person p = new Person();
        p.setAge(30);

        FactHandle faceHandle = kieSession.insert(p);
        //触发rules
        int count = kieSession.fireAllRules();

        System.out.println(p.getName());

        //在 work memory 中的对象描述信息
        System.out.println(faceHandle.toExternalForm());
        p.setAge(10);

        //更新work memory 中的对象信息
        kieSession.getAgenda().getAgendaGroup("faceHandleTest").setFocus();
        kieSession.update(faceHandle,p);
        kieSession.fireAllRules();

        Person p2 = (Person) kieSession.getObject(faceHandle);
        System.out.println(p2.getName());
      //删除
      //kieSession.delete(faceHandle);
} }

faceHandle.drl

package cn.xiaojf.rules

import cn.xiaojf.drools7.model.Person
rule "faceHandleTest-less-20"
agenda-group "faceHandleTest"
when
    $p : Person(age < 20)
    then
      $p.setName("less 20");
    end

rule "faceHandleTest-more-20"
agenda-group "faceHandleTest"
when
    $p : Person(age > 20)
    then
      $p.setName("more 20");
    end

 

结果

D:\soft\java\jdk1.8.0_91\bin\java -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\soft\JetBrains\IntelliJ IDEA 2017.2.1\lib\idea_rt.jar=9137:D:\soft\JetBrains\IntelliJ IDEA 2017.2.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\soft\JetBrains\IntelliJ IDEA 2017.2.1\lib\idea_rt.jar;D:\soft\JetBrains\IntelliJ IDEA 2017.2.1\plugins\junit\lib\junit-rt.jar;D:\soft\JetBrains\IntelliJ IDEA 2017.2.1\plugins\junit\lib\junit5-rt.jar;D:\soft\java\jdk1.8.0_91\jre\lib\charsets.jar;D:\soft\java\jdk1.8.0_91\jre\lib\deploy.jar;D:\soft\java\jdk1.8.0_91\jre\lib\ext\access-bridge-64.jar;D:\soft\java\jdk1.8.0_91\jre\lib\ext\cldrdata.jar;D:\soft\java\jdk1.8.0_91\jre\lib\ext\dnsns.jar;D:\soft\java\jdk1.8.0_91\jre\lib\ext\jaccess.jar;D:\soft\java\jdk1.8.0_91\jre\lib\ext\jfxrt.jar;D:\soft\java\jdk1.8.0_91\jre\lib\ext\localedata.jar;D:\soft\java\jdk1.8.0_91\jre\lib\ext\nashorn.jar;D:\soft\java\jdk1.8.0_91\jre\lib\ext\sunec.jar;D:\soft\java\jdk1.8.0_91\jre\lib\ext\sunjce_provider.jar;D:\soft\java\jdk1.8.0_91\jre\lib\ext\sunmscapi.jar;D:\soft\java\jdk1.8.0_91\jre\lib\ext\sunpkcs11.jar;D:\soft\java\jdk1.8.0_91\jre\lib\ext\zipfs.jar;D:\soft\java\jdk1.8.0_91\jre\lib\javaws.jar;D:\soft\java\jdk1.8.0_91\jre\lib\jce.jar;D:\soft\java\jdk1.8.0_91\jre\lib\jfr.jar;D:\soft\java\jdk1.8.0_91\jre\lib\jfxswt.jar;D:\soft\java\jdk1.8.0_91\jre\lib\jsse.jar;D:\soft\java\jdk1.8.0_91\jre\lib\management-agent.jar;D:\soft\java\jdk1.8.0_91\jre\lib\plugin.jar;D:\soft\java\jdk1.8.0_91\jre\lib\resources.jar;D:\soft\java\jdk1.8.0_91\jre\lib\rt.jar;E:\workspace\drools7-demo\target\test-classes;E:\workspace\drools7-demo\target\classes;E:\m2\repository\junit\junit\4.12\junit-4.12.jar;E:\m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;E:\m2\repository\org\kie\kie-api\7.5.0.Final\kie-api-7.5.0.Final.jar;E:\m2\repository\org\kie\soup\kie-soup-maven-support\7.5.0.Final\kie-soup-maven-support-7.5.0.Final.jar;E:\m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;E:\m2\repository\org\drools\drools-compiler\7.5.0.Final\drools-compiler-7.5.0.Final.jar;E:\m2\repository\org\kie\kie-internal\7.5.0.Final\kie-internal-7.5.0.Final.jar;E:\m2\repository\org\kie\soup\kie-soup-commons\7.5.0.Final\kie-soup-commons-7.5.0.Final.jar;E:\m2\repository\org\kie\soup\kie-soup-project-datamodel-commons\7.5.0.Final\kie-soup-project-datamodel-commons-7.5.0.Final.jar;E:\m2\repository\org\kie\soup\kie-soup-project-datamodel-api\7.5.0.Final\kie-soup-project-datamodel-api-7.5.0.Final.jar;E:\m2\repository\org\antlr\antlr-runtime\3.5.2\antlr-runtime-3.5.2.jar;E:\m2\repository\org\eclipse\jdt\core\compiler\ecj\4.4.2\ecj-4.4.2.jar;E:\m2\repository\org\mvel\mvel2\2.4.0.Final\mvel2-2.4.0.Final.jar;E:\m2\repository\com\thoughtworks\xstream\xstream\1.4.10\xstream-1.4.10.jar;E:\m2\repository\xmlpull\xmlpull\1.1.3.1\xmlpull-1.1.3.1.jar;E:\m2\repository\xpp3\xpp3_min\1.1.4c\xpp3_min-1.1.4c.jar;E:\m2\repository\com\google\protobuf\protobuf-java\2.6.0\protobuf-java-2.6.0.jar;E:\m2\repository\org\drools\drools-core\7.5.0.Final\drools-core-7.5.0.Final.jar;E:\m2\repository\commons-codec\commons-codec\1.10\commons-codec-1.10.jar;E:\m2\repository\org\apache\commons\commons-lang3\3.7\commons-lang3-3.7.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 day20180123.FaceHandleTest,test
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.
more 20
0:1:429058804:429058804:1:DEFAULT:NON_TRAIT:cn.xiaojf.drools7.model.Person
less 20

Process finished with exit code 0

 

facehandler 相当于是work memory 中对象的句柄,通过facehandler 可以直接定位、修改、删除操作。

drools7 (四、FactHandle 介绍)

标签:finish   default   resources   mcr   aws   nbsp   process   gen   介绍   

原文地址:https://www.cnblogs.com/xiaojf/p/8338206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!