如何从Drl文件的Cosequence返回值到Java [英] How to return the value from Cosequence of drl file to java

查看:107
本文介绍了如何从Drl文件的Cosequence返回值到Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果规则匹配,那么我将由于drl而执行java函数,因此我希望将结果返回给调用执行drl的函数.

If rule matches then i am executing the java function in consequence of drl and i want that result of function in consequence to be returned back to the function which calls to execute the drl.

推荐答案

您需要确保将规则的副作用传递回调用方.如何执行此操作取决于您的特定规则集和传递给它的对象.

You need to make sure that the side effect of the rule is passed back to the caller. How you do this depends on your particular rule set and the objects you pass into it.

例如,一种解决方案可能是更改您在工作内存中具有的对象的值.

One solution, for example, might be to change a value on an object you have in working memory.

rule "Example rule changing a value"
when
  $purchase: Purchase( includesAlcohol == true )
then
  $purchase.shouldIncludeExciseTax(true); // set a value on the object in working memory.
end

另一种被某些人认为不太好的做法是使用全局变量.在Drools 5.0和更早的日子里,这很普遍.

Another alternative, considered a not-so-good practice by some, would be to use a global. This was pretty common back in the Drools 5.0 and earlier days.

global Discounts appliedDiscounts;

rule "Example rule setting a property on a global"
when
  DiscountCode( value == "FOOBAR~DISCOUNT" )
then
  appliedDiscounts.addDiscount( 0.3 );
end

基本上,您在右侧进行的任何操作都必须对呼叫者可见.在第一个示例中,您在调用规则时仍应具有对插入到工作内存中的对象的引用.因此,一旦规则执行完毕,您就可以获取该引用并从中查询值.

Basically whatever you do on the right hand side needs to be visible to the caller. In the first example, you should still have a reference to the object you inserted into working memory when you called the rules. So once the rules finish executing, you'll be able to take that reference and query values from it.

第二个示例类似地使用传递到规则中的对象,但是将其用作全局对象而不是位于工作内存中.使用状态会话时,两者之间存在细微的差异,但是它们都利用了相同的基本原理.

The second example similarly uses an object passed into the rules, but as a global instead of being in working memory. There are subtle differences here between the two when using stateful session, but they're both leveraging the same basic principles.

这篇关于如何从Drl文件的Cosequence返回值到Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆