简体   繁体   English

Drools Java更新

[英]Drools java update

I am new to drool, please help me understand if I update the object after I call fire all rules, will it fire the rules as it does in the drools file? 我是流口水的新手,请在调用所有规则后更新对象,是否会像在流口水文件中一样触发规则,以帮助我理解?

like 喜欢

rule "Hello World"
no-loop true
when
    message:Message (type=='Hello')
then
    modify(message){
        setType("Hi")//Fires other rule below
    }
    message.setMsgtext("Msg: Hello World, Drools");
    System.out.println("Hello World, Drools!");
end

rule "Hi World"
when
    message:Message (type=='Hi')
then
    modify(message){
        setType("Hello")
    }
    System.out.println("Hi World, Drools!");
end

whereas in JAVA code, 而在JAVA代码中,

Message msg = new Message();
msg.setType("Hello");
//sessionObject = rbase.newStatefulSession();
sessionObject.insert(msg);
sessionobject.fireAllrules();
msg.setType("Hi"); //Here can it fire the rule? for me its not doing it

If it doesn't fire, does drool engine keep a msg as entirely different session object from Java runtime object, so its its not affected. 如果不触发,则Drool引擎会将msg保留为与Java运行时对象完全不同的会话对象,因此其不受影响。 If its not the concept behind, please help me understand 如果不是背后的概念,请帮助我理解

Thanks Chakri 谢谢查克里

The rules are not going to react upon your change. 规则不会对您的更改做出反应。 Drools will not keep a different 'copy' of the object, it just will not evaluate the rules. 流口水不会保留对象的其他“副本”,只是不会评估规则。 If you want to make drools aware of your modification you must use sessionObject.update(). 如果您想让流口水知道您的修改,则必须使用sessionObject.update()。 And then don't forget to fireAllRules() again. 然后不要忘记再次触发fireAllRules()。 Your code should look something similar to this: 您的代码应类似于以下内容:

Message msg = new Message();
msg.setType("Hello");
//sessionObject = rbase.newStatefulSession();
FactHandle handle = sessionObject.insert(msg);
sessionobject.fireAllrules();
msg.setType("Hi");
sessionObject.update(handle, msg) // rules are re-evaluated here
session.fireAllRules(); // fire any pre-activated rule

Hope it helps, 希望能帮助到你,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM