java Drools 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12604741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drools functions
提问by Fulgencio Jara
Recently I'm working with drools and I want to make some special checks on some objects. I need to use functions in the when
section of the rule, but I'm getting an error. Example:
最近我在使用drools,我想对一些对象进行一些特殊检查。我需要when
在规则部分使用函数,但出现错误。例子:
function boolean newFunction(int a){
if(a>0)
return true;
else
return false;
}
rule "new rule"
salience 100
dialect "mvel"
when
eval(newFunction(1))
then
System.out.println("OK");
end
The error i get always is:
我总是得到的错误是:
unable to resolve method using strict-mode: java.lang.Object.newFunction(java.lang.Integer)
Is there no support on drools for functions in when
section?
when
节中的功能是否不支持drools ?
Thanks!
谢谢!
回答by Augusto
The short answer is: no.
最简洁的答案是不。
This is because the facts need to be in the working memory.
这是因为事实需要在工作记忆中。
What you can do, is to have a rule that takes all types of a certain class from the working memory, applies a function in the thensection and inserts that new value in the working memory.
你可以做的是制定一个规则,从工作内存中获取某个类的所有类型,在then部分应用一个函数,然后在工作内存中插入新值。
edit
编辑
This answer, originally posted on 2012, is not more relevant as newer versions of drools do support functions on the when
clause.
这个答案最初发布于 2012 年,因为较新版本的 drools 确实支持该when
子句的功能,所以它的相关性并不高。
回答by Davide Sottara
Very likely an MVEL or an integration bug - function call adapters did not box/unbox primitive types. I see the question is quite old, but the problem has been since fixed (tested with 6.3.0-SNAPSHOT). For older versions, I'd try using boxed types : function boolean newFunction( Integer a ) ...
很可能是 MVEL 或集成错误 - 函数调用适配器没有装箱/拆箱原始类型。我看到这个问题很老了,但问题已经解决了(用 6.3.0-SNAPSHOT 测试)。对于旧版本,我会尝试使用盒装类型:function boolean newFunction(Integer a) ...
回答by ProfVersaggi
Along the lines of the selected answer above, after some experimentation I found that it is possible to create an external java method, whose class can be imported into the rules file, and wrapped in a MVEL function wrapper (Boolean) which can then can be called from the LHS as a parameter to an eval statement.
沿着上面选择的答案,经过一些实验,我发现可以创建一个外部 java 方法,它的类可以导入到规则文件中,并包装在一个 MVEL 函数包装器(布尔)中,然后可以从 LHS 调用作为 eval 语句的参数。
[External Java POJO_Class.myMethod]
import com.mypackage.POJO_Class;
function Boolean myFunctionName() {
POJO_Class myClass = new POJO_Class();
return myClass.myMethod(Parameters);
}
rule "Test Rule"
when
eval ( myFunctionName(parameters) )
then
end
回答by Borris
I'm not certain why you are getting the error you are getting, but I was using the following for some debugging yesterday in a query (an LHS so I'm guessing same as a rule)
我不确定为什么你会得到你得到的错误,但我昨天在查询中使用了以下进行一些调试(LHS,所以我猜测与规则相同)
function boolean say(Object s) {
Debug.log("Say %s\n", s);
return true;
}
the query just did eval(say($object))
to help me see whether it ever got invoked. I'm running a snapshot of 6.1 from a week or so ago. Maybe try getting it working (doing anything) just taking Object and work from there - it might be Number or Integer is where you end up on the argument rather than int?
该查询只是eval(say($object))
帮助我查看它是否曾经被调用过。我正在运行大约一周前 6.1 的快照。也许尝试让它工作(做任何事情),只是拿 Object 并从那里开始工作 - 它可能是 Number 或 Integer 是你最终在参数上而不是 int 的地方?