Question

Is there a way I can check the value of a HashMap in "when" block, by passing a key.

Something like(may not be the actual way)

rule "checkHM"
when
  $account : Account( hm.get("A") == "B") 
then
  System.out.println("HashMap has value B" );
end  

Here Account is a class and hm is a HashMap in this class. hm has key/value pair as "A"/"B".

I hope the question is clear.

Was it helpful?

Solution

It should work just like

rule "checkHM"
when
  $account : Account( hm[ "A" ] == "B" )
then
  System.out.println( "HashMap has value B" );
end

P.S. The reason why get() with HashMap might not work is probably due to the fact that Drools expects the facts to follow JavaBeans standard, and thus the method would need to be getGet() for it to work directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top