简体   繁体   中英

How to evaluate if a value is in a list of values in DROOLS

In DROOLS, if we are looking to see if the value "foo" is in the list {fu, fa, fe, fi, fo, foo, fum, doodle, dee}, when MVEL parses that into a DRL we get something like this:

if("foo" == "fu" || "foo" == "fa" || "foo" == "fe" || ...)

Which is fine as long as the list is relatively small, but we need to see if a provided zipCode is in a list of zipCodes, so what we need it to create is something more like this:

Set zipCodes = getAllZipCodesInNHNYandHalfOfCA() [So you know there are approximately 40k zipCodes in the US]

if(zipCodes.contains(customer.getZipCode()){ ... rule evaluates to true }

I'm looking for how to teach DROOLS to do this comparison the way we need it to rather than the way it is for us now.

Thoughts, comments, suggestions?

Thanks,

Jason

You can call a Java method from your Drools rule. So you write a method

boolean isZipCodeInArea(String area, String zipCode) {
  return getZipCodesOf(area).contains(zipCode);
}

And then in Drools rule when part you can call this function:

when 
  isZipCodeInArea("area", "foo")

You can use the in operator and specify the values in a comma separated list. If your intention is to validate the input, then you can use " not in " and take appropriate action when the input value is not in the list.

eg when Model( fieldName in ("fu", "fa", "foo") ) then <do - something> end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM