drools rule get value from a map

自闭症网瘾萝莉.ら 提交于 2019-12-11 22:44:26

问题


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

回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/7764465/drools-rule-get-value-from-a-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!