我对流口水很陌生,在编写规则时遇到了困难--这里是我的数据结构:
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 但是,app服务器无法从以下错误开始:
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']发布于 2021-11-30 15:42:07
我将假设在getName类上有一个公共getInsuranceTypes方法,在Premium类上有一个公共getInsuranceTypes方法。如果其中任何一个不是真,则需要添加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 )。
https://stackoverflow.com/questions/70163217
复制相似问题