此问题是先前问题 here 的延续。 所以我的数据结构与之前一样:
public class Premium{
private List<InsuranceType> insuranceTypes;
// getter and setter
}
public class InsuranceType {
private String name;
private String category;
// getter and setter
}
感谢@RoddyoftheFrozenPeas,这是我的规则
rule "Example"
when
$pcr:Premium( $insuranceTypes: insuranceTypes )
$ins:( InsuranceType( name == "TPD" , category == "D" ) from $insuranceTypes )
then
modify( $ins ){
setCategory("A");
}
end
但是,运行此规则时会出现异常:
java.lang.RuntimeException: Update error: handle not found for object: InsuranceType [name=TPD, category=A]. Is it in the working memory?
发生这种情况是因为我正在检查相同的属性并更新它吗? 我的用例是替换该值,因此我必须检查它是否存在(类别= D,然后将其设置为“A”)
最佳答案
InsuranceType 不在工作内存中,而 Premium 则在工作内存中。因此您需要修改保费,而不是保险类型。
确实没有什么好办法。您可以调用update
,但这将重新触发所有规则(相当于使用工作内存中的新数据重新开始。)
then
$ins.setCategory("A");
update( $pcr );
end
这可能会导致副作用,例如循环或过多的规则触发器。
或者,您可以通过设置列表值来修改溢价:
then
$ins.setCategory("A");
modify( $pcr ){
setInsuranceTypes($insuranceTypes)
}
end
...这很笨重,但只要我们没有丢失对列表的引用就应该做我们想要的事情。
此外,并不完全相关,但外部 ()
在您的 $ins
声明行中不是必需的。这就足够了:
$ins: InsuranceType( name == "TPD" , category == "D" ) from $insuranceTypes
我认为您的原始规则有一个 exists()
但由于您删除了该谓词,因此您也不需要括号。
关于流口水时出现错误 : handle not found for object is it in working memory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70193124/