Drools LHS score: matches vs. String.contains ()

I have read the docs but cannot find an answer to the following. I need a Drools rule to check if a string contains another string. I was wondering what the difference (if any) in the score would be written like this:

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

      

compared to 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 a difference between how the rule engine will evaluate the two in terms of security, optimization, or performance?

And I know the above example is not a great way to check apartments in addresses, was just a simple example off my head using a concept that most people are familiar with.

+3


source to share


1 answer


The difference is about the same as when using String.matches( Regex regex )

versus String.contains( CharSequence charSeq )

in Java. Regular expressions must be compiled and interpreted when evaluated against a CharSequence; finding a substring in a string that matches a given string requires less effort.

And it's pretty easy to write an ineffective regex. ".*apt.*"

will be done by matching all the characters of the target string with .*

, then the RE engine will backup, retry, and so on until the last occurrence is matched apt

. Much better ".*?apt.*"

, which will continue to match after finding the leftmost one apt

.



Safety? From what?

+5


source







All Articles