Drools - 如何比較存在於列表中的 Java 對像中的字符串值

薩蒂什馬拉特

我是 Drools 的新手,在編寫規則時遇到了困難 這是我的數據結構:

public class Premium{
   private List<InsuranceType> insuranceTypes;
}

public class InsuranceType {
   private String name;
}

所以一個 Premium 對象將包含一個保險類型列表,我需要檢查是否有任何保險類型的名稱為“TPD”

嘗試了以下方法:

rule "rule#3"
when
    $fact:Premium($insuranceTypes : InsuranceType(name == 'TPD'))
then
    System.out.println("Error");
end 

但是應用服務器無法啟動並出現以下錯誤:

2021-11-30 12:16:37.004 ERROR 23500 --- [           main] 
o.d.c.k.builder.impl.AbstractKieModule   : Unable to build KieBaseModel:defaultKieBase
Unable to Analyse Expression InsuranceType(name == "TPD"):
[Error: unable to resolve method using strict-mode: com.xyz.Premium.name()]
[Near : {... InsuranceType(name == "TPD") ....}]
                       ^
[Line: 29, Column: 5] : [Rule name='rule#3']
Unable to analyze expression 'InsuranceType(name == "TPD")' : [Rule name='rule#3']
Field Reader does not exist for declaration '$insuranceTypes' in '$insuranceTypes : 
InsuranceType(name == "TPD")' in the rule 'rule#3' : [Rule name='rule#3']
冷凍豌豆的羅迪

我將假設getNameInsuranceType 類中有一個公共getInsuranceTypes方法,Premium 類中有一個公共方法。如果其中任何一個都不正確,您需要添加 getter 或公開這些屬性。


你的規則非常接近。但是,您遇到的問題是這insuranceTypes是一個列表,但您將其視為對象。

您在此處有多種選擇,具體取決於您的需要。但是我會選擇最簡單的,那就是:

rule "Example"
when
  Premium( $insuranceTypes: insuranceTypes )
  exists( InsuranceType( name == "TPD" ) from $insuranceTypes )
then
  System.out.println("Error");
end

在第一行,我獲取保險類型並將它們分配給變量$insuranceTypes這個變量現在是類型列表。

然後在第二行,我斷言列表中至少存在一個名為“TPD”的 InsuranceType。


請注意,Drools 也有一個memberOf運算符和一個contains運算符,在處理列表和其他可迭代集合時非常有用。這些是彼此的倒數,例如。你會做Example( foo memberOf $someList )Example( myList contains $something )

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章