本文介紹了如何遍歷列表以檢查某項是否為另一個列表的成員的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我想檢查診斷代碼列表中的任何項目是否存在于另一個列表中。
我是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
推薦答案
您的規(guī)則將不符合‘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
根據(jù)documentation,需要
當您可以將所有事實插入到Drools引擎的工作內(nèi)存中或在約束表達式中使用嵌套對象引用時,請避免使用from元素。
以下將導致規(guī)則針對每個EligibilityDomainObject僅觸發(fā)一次,而不是針對每個匹配值:
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
這篇關于如何遍歷列表以檢查某項是否為另一個列表的成員的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,