简体   繁体   English

如何在 java 的流口水规则内比较嵌套 object 的值

[英]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:您还没有真正描述您的用例是什么,但是鉴于您的尝试,这就是我解释您想要做的事情:

  1. Rule 1 should trigger if A contains at least 1 B which has group "ABC" and status "approved".如果 A 包含至少 1 个具有组“ABC”且状态为“已批准”的 B,则规则 1 应触发。
  2. Rule 2 should trigger if A contains at least 1 B which has group "XYZ" and status "approved".如果 A 包含至少 1 个具有组“XYZ”且状态为“已批准”的 B,则规则 2 应触发。

I further presume the following:我进一步假设如下:

  • Since you don't attempt to extract values or B instances, you don't actually care about the B beyond the fact that it exists.由于您不尝试提取值或 B 实例,因此您实际上并不关心 B 存在的事实。

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.

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