JTryon
JTryon

Reputation: 33

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

Upvotes: 1

Views: 4296

Answers (2)

Jose
Jose

Reputation: 289

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.

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

Upvotes: 1

Master Drools
Master Drools

Reputation: 738

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")

Upvotes: 2

Related Questions