apache / incubator-kie-drools

Drools is a rule engine, DMN engine and complex event processing (CEP) engine for Java.
http://www.drools.org
Apache License 2.0
5.88k stars 2.5k forks source link

[new-parser] `drools-verifier` fails to remove comments from RHS consequence #5886

Closed yurloc closed 6 months ago

yurloc commented 6 months ago

Parent issue

Failing tests

Notes

This is, in my opinion, a minor bug in drools-verifier that manifests due to a slight change of behavior in the new parser.

The Verifier at some point cleans the RHS code by removing comments. While doing that, it assumes that each comment line (// asdf...) ends with a new-line character. This is no longer true with the new parser if the comment is the last line before the RHS end or before another named consequence (then[label]). This is simply because the whitespace part of the input between the default RHS consequence and any named consequence(s) or RHS end is not part of the consequence RuleContext. This is natural to how the ANTLR parser works.

The old parser's behavior could be replicated with some custom code but in this case, I think it's better to align with the new behavior and adapt the Verifier code to it.

Rule code snippet

rule "Missing equality 1"
    when
        MissingEqualityPattern1( value == 10 )
    then
        //actions
end

Error output

### parse : ANTLR4_PARSER_ENABLED = true
### parse : ANTLR4_PARSER_ENABLED = true

java.lang.StringIndexOutOfBoundsException: Range [0, -1) out of bounds for length 9

    at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
    at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
    at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
    at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
    at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
    at java.base/java.lang.AbstractStringBuilder.delete(AbstractStringBuilder.java:904)
    at java.base/java.lang.StringBuilder.delete(StringBuilder.java:289)
    at org.drools.verifier.visitor.RuleDescrVisitor.visitConsequence(RuleDescrVisitor.java:206)
    at org.drools.verifier.visitor.RuleDescrVisitor.visitRuleDescr(RuleDescrVisitor.java:70)
    at org.drools.verifier.visitor.PackageDescrVisitor.visitRuleDescr(PackageDescrVisitor.java:120)
    at org.drools.verifier.visitor.PackageDescrVisitor.visitRules(PackageDescrVisitor.java:113)
    at org.drools.verifier.visitor.PackageDescrVisitor.visitPackageDescr(PackageDescrVisitor.java:65)
    at org.drools.verifier.TestBaseOld.getTestData(TestBaseOld.java:124)
    at org.drools.verifier.missingEquality.MissingEqualityTest.testMissingEqualityInLiteralRestrictions(MissingEqualityTest.java:50)
yurloc commented 6 months ago

/take