[英]how to compare values of nested object inside a drool rule in java
I have a pojo like below:我有一个像下面这样的pojo:
class A{
private List<B> b;
//getters and setters
}
class B{
private String group;
private String status;
//getters and setters
}
I have defined the Drool rule as:我将 Drool 规则定义为:
import A;
rule "rule 1"
ruleflow-group "status"
when
a : A(b.( group == "ABC", status == "Approved"))
then
System.out.println("------------Executing rule 1");
end;
rule "rule 2"
ruleflow-group "status"
when
a : A(b.( group == "XYZ", status == "Approved"))
then
System.out.println("-----------Executing rule 2");
end;
Considering all configurations are in place,when executing the rule, getting below error:考虑到所有配置都已到位,执行规则时,出现以下错误:
Caused by: org.mvel2.PropertyAccessException: [Error: unable to resolve method: java.util.ArrayList.group() [arglength=0]]引起:org.mvel2.PropertyAccessException:[错误:无法解决方法:java.util.ArrayList.group() [arglength=0]]
Can someone please help with the modification which I need to do in my rule file so that the nested values are compared.有人可以帮助我在规则文件中进行修改,以便比较嵌套值。
You've not really described what your use case is, but given your attempt, this is what I interpret you want to do:您还没有真正描述您的用例是什么,但是鉴于您的尝试,这就是我解释您想要做的事情:
I further presume the following:我进一步假设如下:
Your rules would, therefore, look like this:因此,您的规则将如下所示:
rule "Rule 1"
when
A( $bList: b != null )
exists( B( group == "ABC", status == "approved" ) from $bList )
then
//...
end
rule "Rule 2"
when
A( $bList: b != null )
exists( B( group == "XYZ", status == "approved") from $bList )
then
// ...
end
In both rules, the first thing we do is extract the sub-list of B's if it is not null.在这两个规则中,如果不是 null,我们首先要做的是提取 B 的子列表。 Then we check that there exists at least one B instance which meets your criteria.
然后我们检查是否存在至少一个符合您条件的 B 实例。 Since you don't actually do anything with this B instance, we can use the
exists
operation to simply check for its presence.由于您实际上并没有对这个 B 实例做任何事情,我们可以使用
exists
操作来简单地检查它的存在。
If you actually do need to do something with the B instance, you'd assign it to a variable rather than using exists
:如果您确实需要对 B 实例做某事,则将其分配给变量而不是使用
exists
:
rule "Rule 1 with captured B"
when
A( $bList: b != null )
$b: B(group == "ABC", status == "approved") from $bList
then
// can reference $b here
end
Note that this version may trigger multiple times if there are multiple B present which meet your criteria.请注意,如果存在多个符合您条件的 B,则此版本可能会触发多次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.