kkarthikmurthy
kkarthikmurthy

Reputation: 1

drools rule get value from a map

How to get the perticular value in the drools when block.

I am looking for something like this but its not working:

I have inserted Hashmap into Working memory and trying to retrieve it in When

$expiry_date:HashMap(get("CREDIT_CARD_EXPIRATION_DATE"));
eval(ageInDays($expiry_date)>10) ;

I get below error

[42,37]: [ERR 101] Line 42:37 no viable alternative at input '"CREDIT_CARD_EXPIRATION_DATE"' in rule "Rule1" in pattern HashMap

Upvotes: 0

Views: 6690

Answers (2)

salaboy
salaboy

Reputation: 4133

Usually its better to insert more typed objects than just a hash map. Can you explain the information that are you trying to process and why do you choose to insert a hashmap instead of a typed object?

I'm pretty sure that you can do something like:

HashMap($expire: keys["CREDIT_CARD_EXPIRATION_DATE"] )
eval(ageInDays($expire) > 10)

I didn't test it but you should look in that direction if you can't insert more typed facts. Cheers

Upvotes: 0

Edson Tirelli
Edson Tirelli

Reputation: 3901

For maps/lists/arrays, you can use [] syntax to access elements. Also, if you are using Drools 5.3+, evals are basically irrelevant now.

rule X
when
    HashMap( ageInDays(this["CREDIT_CARD_EXPIRATION_DATE"]) > 10 )
then
    ...
end

With Drools 5.1/5.2, you could do:

rule X
when
    HashMap( eval( ageInDays(this["CREDIT_CARD_EXPIRATION_DATE"]) > 10 ) )
then
    ...
end

Upvotes: 4

Related Questions