0
votes

I am new to drools and tried searching the API doc but couldn't find anything about the comparison keyword 'LIKE'. I have successfully created rules with <, >, <=, >=, ==, != but now I am trying to create a rule using 'LIKE' keyword. I was told by the seniors that it should work like SQL like keyword. Below is my rule from the drl file but I am getting an error about mismatched input 'LIKE' in rule 5495Y

using drools 5.6.0

**From DRL File:**
rule "5495Y"
agenda-group "1"
date-effective "04-Sep-2017"
when
JournalEntry : JournalEntry( (ledgerCd=="COMN") && accountNumber LIKE 1234 
&& entryTypeCode == "Y" );
DroolsUtil : DroolsUtil(  );
then
JournalEntry.addConsequence("rejectReason","kk like test");
JournalEntry.addConsequence("sendToLedger","N");
DroolsUtil.reclassEntry(JournalEntry,"NOCHG", "5495Y");
end

**Exact Error I am getting:**
[38583,71]: [ERR 102] Line 38583:71 mismatched input 'LIKE' in rule "5495R"
[38596,71]: [ERR 102] Line 38596:71 mismatched input 'LIKE' in rule "5495Y"
[0,0]: Parser returned a null Package

11:09:17,118 ERROR main ReclassAccountingRuleJob:132 - java.lang.Exception: Know
ledgeBuilder has errors. DRL File parsing error
java.lang.Exception: KnowledgeBuilder has errors. DRL File parsing error
1
What do you expect it to do? What should "like 1234" mean? - SQL "like" does pattern matching, and the Drools manual will tell you that there is a "matches" operator for pattern matching. - laune
may be "like 1234*" or "like 1234%" should mean anything that begins with 1234. I am sure about the correct syntax. - Kaushal Kathwadia

1 Answers

0
votes

Drools has support for regular expression matching (much more powerful than the LIKE operator in SQL) for String type attributes:

rule "5495Y"
when
    JournalEntry : JournalEntry(
        ledgerCd == "COMN",
        accountNumber matches ".*1234.*", //accountNumber must be of type String. Otherwise, you will need to convert it first.
        entryTypeCode == "Y" 
    )
    DroolsUtil : DroolsUtil(  )
then
    ...
end

For more information about the matches operator you can read this section of the documentation.

Hope it helps,