drools getting error : handle not found for object is it in working memory

Viewed 196

This question is in continuation to an earlier question here so my data structure is as earlier :

public class Premium{
  private List<InsuranceType> insuranceTypes;
  // getter and setter
  }

public class InsuranceType {
  private String name;
  private String category;
  // getter and setter
}

And here is my rule thanks to @RoddyoftheFrozenPeas

rule "Example"
when
  $pcr:Premium( $insuranceTypes: insuranceTypes )
  $ins:( InsuranceType( name == "TPD" , category == "D" ) from $insuranceTypes )      
then
  modify( $ins ){ 
        setCategory("A"); 
  }     
end

However when running this rule get an exception:

java.lang.RuntimeException: Update error: handle not found for object: InsuranceType [name=TPD, category=A]. Is it in the working memory?

Is this occurring because I am checking for the same attribute and also updating it ? My use case is to replace the value so I have to check for its presence ( category = D and then set it to 'A' )

1 Answers

InsuranceType is not in working memory -- Premium is. So you need to modify Premium, not insurance type.

There's really not a good way to do this. You could call update, but this will refire all rules (it's the equivalent of starting over with new data in working memory.)

then
  $ins.setCategory("A");
  update( $pcr );
end

This might cause side effects like looping or excessive rule triggers.

Alternatively you could modify the Premium by setting the List value:

then
  $ins.setCategory("A");
  modify( $pcr ){ 
    setInsuranceTypes($insuranceTypes)
  }     
end

... which is clunky but as long as we've not lost the reference to the list should do what we want.


Also, not entirely related, but the outer () are not necessary on your $ins declaration line. This is sufficient:

$ins: InsuranceType( name == "TPD" , category == "D" ) from $insuranceTypes

I think your original rule had an exists() but since you removed that predicate you don't need the parentheses either.

Related