如何遍历列表以检查某项是否为另一个列表的成员

2022-04-14 00:00:00 java drools

我想检查诊断代码列表中的任何项目是否存在于另一个列表中。

我是Drools的新手,我仍在尝试确定最佳方法,因为当前的方法不起作用(输出显示为NULL,而它应该返回‘true’)。

Java POJO

public class EligibilityDomainObject {
    
    private List<String> listOfString;  
    public List<String> getListOfString() {  
         return listOfString;  
    }  
    public void setListOfString(List<String> listOfString) {  
         this.listOfString = listOfString;  
    }  
    public void addString(String value) {  
         if (listOfString == null) {  
              listOfString = new ArrayList<String>();  
         }  
         listOfString.add(value);  
    }  
    private Boolean isComplexPhysicalHealth;
    public Boolean getIsComplexPhysicalHealth() {
        return this.isComplexPhysicalHealth;
    }

    public void setIsComplexPhysicalHealth(Boolean isComplexPhysicalHealth) {
        this.isComplexPhysicalHealth = isComplexPhysicalHealth;
    }

口水

rule "Problems related to social environment"
    when
        $eligibilityDomainObject:EligibilityDomainObject($listOfString : listOfString,
            $listOfString memberOf "F77 EOO F99" )
    then
        $eligibilityDomainObject.setIsComplexPhysicalHealth(true);
end

Main

    public static void main(String[] args) {
        SpringApplication.run(RulesEngineApplication.class, args);
        

        try {
            KieServices kieServices = KieServices.Factory.get();
            KieContainer kContainer = kieServices.getKieClasspathContainer();

            KieSession kSession = kContainer.newKieSession("ksession-rules");
            
            EligibilityDomainObject eligibilityDomainObject = new EligibilityDomainObject(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);

            eligibilityDomainObject.addString("F77");
            eligibilityDomainObject.addString("F99");
            System.out.println("List of diagnosis codes: "+ eligibilityDomainObject.getListOfString().toString());
            kSession.insert(eligibilityDomainObject);
            kSession.fireAllRules();
            System.out.println("complex phys health flag: "+ eligibilityDomainObject.getIsComplexPhysicalHealth());
            
            kSession.dispose(); 
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

输出

诊断代码列表:[Z590,E0800]

复杂物理层运行状况标志:空

预期输出

诊断代码列表:[Z590,E0800]

复杂物理层运行状况标志:TRUE


解决方案

您的规则将不符合‘F’、‘F7’、‘F9’...

考虑其他几种方法:

rule "Problems related to social environment"
    when
        $eligibilityDomainObject:EligibilityDomainObject($listOfString : listOfString)
            value: String(value in ("F77", "EOO", "F99") ) from $listOfString
    then
        $eligibilityDomainObject.setIsComplexPhysicalHealth(true);
end

根据documentation,需要

当您可以将所有事实插入到Drools引擎的工作内存中或在约束表达式中使用嵌套对象引用时,请避免使用from元素。

以下将导致规则针对每个EligibilityDomainObject仅触发一次,而不是针对每个匹配值:

import static org.apache.commons.collections4.CollectionUtils.containsAny;

rule "Problems related to social environment"
    when
        $eligibilityDomainObject:EligibilityDomainObject(containsAny(listOfString, "F77", "EOO", "F99"))
    then
        $eligibilityDomainObject.setIsComplexPhysicalHealth(true);
end

一次收集匹配值

import static org.apache.commons.collections4.CollectionUtils.intersection;
import static com.google.common.collect.Sets.newHashSet;

rule "Problems related to social environment"
    when
        $eligibilityDomainObject:EligibilityDomainObject(
            $values : intersection(listOfString, newHashSet("F77", "EOO", "F99")), 
            !$values.isEmpty())
    then
        $eligibilityDomainObject.setIsComplexPhysicalHealth(true);
        System.out.println($values);
end

相关文章