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

How to loop through a list to check if an item is a memberOf another list(如何遍历列表以检查某项是否为另一个列表的成员)

本文介绍了如何遍历列表以检查某项是否为另一个列表的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我是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

这篇关于如何遍历列表以检查某项是否为另一个列表的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

基础教程推荐