Drools LHS Evaluation: matches vs String.contains()

11.5k views Asked by At

I've been reading through the docs, but can't seem to find an answer to the following. I need a Drools rule to check and see whether or not a string contains another string. I was wondering what the difference (if any) in evaluation would be in writing it like this:

rule "String Matches"
   when
      Address(street matches ".*apt.*")
   then
      log.info("Someone likely lives in an apartment");
end

versus writing the same logic like this:

rule "String Contains"
   when
      Address(street.contains("apt"))
   then
      log.info("Someone likely lives in an apartment");
end

Is there any difference between how the rules engine will evaluate the two in terms of safety, optimization, or performance?

And I'm aware that the example above is not a great way to check for apartments in addresses, was just a simple example off the top of my head using a concept with which most folks are familiar.

1

There are 1 answers

0
laune On BEST ANSWER

The difference is about the same as using String.matches( Regex regex ) versus String.contains( CharSequence charSeq ) in Java. Regular expressions need to be compiled and interpreted when evaluated against a CharSequence; looking for a substring in a string matching the given string requires less effort.

And it's quite easy to write an inefficient regular expression. ".*apt.*" will be executed by matching all of the target string's characters to .*, then the RE engine will backup, try again, and so on, until the last occurrence of apt is matched. Much better is ".*?apt.*", which will continue to match after the leftmost apt is detected.

Safety? From what?