Drools出现错误:找不到对象的句柄它是否在工作内存中 [英] drools getting error : handle not found for object is it in working memory
本文介绍了Drools出现错误:找不到对象的句柄它是否在工作内存中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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
但是,在运行此规则时出现异常:
:更新错误:找不到对象的句柄:保险类型[名称=TPD,类别=A]。它在工作内存中吗?发生这种情况是因为我正在检查相同的属性并对其进行更新吗? 我的用例是替换该值,因此我必须检查它是否存在(CATEGORY=D,然后将其设置为‘A’)
推荐答案
保险类型不在工作内存中--保费在工作内存中。因此,您需要修改保费,而不是保险类型。
确实没有好的方法来做到这一点。您可以调用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()
,但由于您删除了该谓词,因此也不需要括号。
这篇关于Drools出现错误:找不到对象的句柄它是否在工作内存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文