Drools 7 Modify 和对象直接赋值差异

  • modify代表修改fact,会再次触发符合条件的rule
  • 对象直接修改只是java 操作,不会会再次触发符合条件的rule

以下为测试代码-drl部分

java 复制代码
package org.drools.learn

import org.drools.learn.ModifyTest.Message;


global java.util.List list


rule "Stateless modify"
// 注意使用了mvel,用java会报status 不是public
    dialect "mvel"
    when
        m : Message( status == Message.HELLO, message : message )
    then
        System.out.println( message + " in thread " + Thread.currentThread().getId()+","+System.currentTimeMillis());
        modify ( m ) { message = "Goodbye cruel world",
                   status = Message.GOODBYE };

end



rule "Stateless Good Bye"
// 注意使用了mvel
    dialect "mvel"
    when
        o : Message( status == Message.GOODBYE, message : message )
    then
        System.out.println( " Stateless Good Bye in thread " + Thread.currentThread().getId()+","+System.currentTimeMillis());
        // 使用set 不会触发rule执行
        o.setMessage("changed by drl");
        o.setStatus (100);

        //  modify ( o ) { message = "100 cruel world",
        //                    status = 100 };

 end

rule "Stateless 100 "
    dialect "java"
    auto-focus true
    when
        o : Message( status == 100, message : message )
    then
        System.out.println( " Stateless 100 should not be printed ,but it printed in  " + Thread.currentThread().getId()+","+System.currentTimeMillis());


 end
java 复制代码
测试java代码
 
package org.drools.learn;

import org.drools.core.io.impl.ClassPathResource;
import org.kie.api.KieBase;
import org.kie.api.KieBaseConfiguration;
import org.kie.api.KieServices;
import org.kie.api.conf.EqualityBehaviorOption;
import org.kie.api.conf.SequentialOption;
import org.kie.api.io.Resource;
import org.kie.api.logger.KieRuntimeLogger;
import org.kie.api.runtime.StatelessKieSession;
import org.kie.internal.conf.ConsequenceExceptionHandlerOption;
import org.kie.internal.conf.MultithreadEvaluationOption;
import org.kie.internal.utils.KieHelper;

import java.util.ArrayList;
import java.util.List;

 
public class ModifyTest {

    private static KieBase kieBase;
    private static KieBaseConfiguration kieBaseConf;
   // private static KieSessionConfiguration kieBaseConf;
    public static final void main(final String[] args) {
        // From the kie services, a container is created from the classpath
        KieServices ks=KieServices.get();
        kieBaseConf= ks.newKieBaseConfiguration();
        //设置SequentialOption以提升性能
        kieBaseConf.setOption(SequentialOption.YES);
        //设置使用对象的equals函数来进行对象比较
        kieBaseConf.setOption(EqualityBehaviorOption.EQUALITY);
        //设置exception 捕获,不设置为默认使用org.drools.core.runtime.rule.impl.DefaultConsequenceExceptionHandler
        kieBaseConf.setOption(ConsequenceExceptionHandlerOption
                .get(DroolsConsequenceExceptionHandler.class));

        // kieBaseConf.setOption(MultithreadEvaluationOption.YES);

        //使用resource模式装载,参考https://zhuanlan.zhihu.com/p/519969197
        Resource resource =
                new ClassPathResource("org/drools/learn/ModifyTest.drl");
        KieBase base=new KieHelper().addResource(resource)
                .build(kieBaseConf);
 
        StatelessKieSession ksession = base.newStatelessKieSession();
 
        ArrayList result=new ArrayList<Object>();
        ksession.setGlobal( "list",  result);

 



        // The application can insert facts into the session
          Message message = new Message();
        message.setMessage( "Hello World" );
        message.setStatus( Message.HELLO );
        ksession.execute( message );

        System.out.println("message status change to "+message.getStatus());


    }

    public static class Message {
        public static final int HELLO   = 0;
        public static final int GOODBYE = 1;

        private String          message;

        private int             status;

        public Message() {

        }

        public String getMessage() {
            return this.message;
        }

        public void setMessage(final String message) {
            this.message = message;
        }

        public int getStatus() {
            return this.status;
        }

        public void setStatus(final int status) {
            this.status = status;
        }

        public static Message doSomething(Message message) {
            return message;
        }

        public boolean isSomething(String msg,
                                   List<Object> list) {
            list.add( this );
            return this.message.equals( msg );
        }
    }

}
相关推荐
Daniel 大东16 分钟前
BugJson因为json格式问题OOM怎么办
java·安全
Theodore_10224 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
冰帝海岸5 小时前
01-spring security认证笔记
java·笔记·spring
世间万物皆对象6 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
没书读了6 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
小二·6 小时前
java基础面试题笔记(基础篇)
java·笔记·python
开心工作室_kaic7 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
懒洋洋大魔王7 小时前
RocketMQ的使⽤
java·rocketmq·java-rocketmq
武子康7 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
转世成为计算机大神7 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式