Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO-NOT-MERGE][WIP][incubator-kie-drools-6220] testing default DRL6 #6240

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/pr-downstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ on:

jobs:
kogito-downstream-build:
if: false
concurrency:
group: pr-${{ matrix.job_name }}_${{ matrix.os }}_${{ matrix.java-version }}_${{ matrix.maven-version }}_${{ github.head_ref }}
cancel-in-progress: true
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ nbproject
*.ipr
*.iws
*.iml
build.log

# generated files
dependency-reduced-pom.xml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
public class OperatorDescr extends BaseDescr {
private static final long serialVersionUID = 520l;

// prefix for custom operators which are not built-in
public static final String CUSTOM_OPERATOR_PREFIX = "##";

private String operator;
private boolean negated;
private List<String> parameters;
Expand Down Expand Up @@ -55,7 +58,15 @@ public String getOperator() {
}

public void setOperator( String operator ) {
this.operator = operator != null ? operator.trim() : null;
this.operator = operator != null ? trimOperator(operator) : null;
}

private String trimOperator(String operator) {
operator = operator.trim();
if (operator.startsWith(CUSTOM_OPERATOR_PREFIX)) {
operator = operator.substring(CUSTOM_OPERATOR_PREFIX.length());
}
return operator;
}

public boolean isNegated() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,14 @@ public DescrBuilder<P, T> endLocation( int line,
* @return
*/
public P end();

/**
* Returns the parent container of this descr builder.
* Example: ruleDescrBuilder.getParent() will return the
* PackageDescrBuilder as that is its parent container
* without ending the current construction.
*
* @return
*/
public P getParent();
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,8 @@ public P end() {
return parent;
}

@Override
public P getParent() {
return parent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.drools.drl.parser.antlr4.DRLParserHelper.computeTokenIndex;
import static org.drools.drl.parser.antlr4.DRLParserHelper.createDrlParser;
import static org.drools.drl.parser.antlr4.DRLParserHelper.parse;
import static org.drools.drl.parser.antlr4.DRL10ParserHelper.computeTokenIndex;
import static org.drools.drl.parser.antlr4.DRL10ParserHelper.createDrlParser;
import static org.drools.drl.parser.antlr4.DRL10ParserHelper.parse;

/**
* This test class is specific to new antlr4 parser.
*/
class DRLParserTest {
class DRL10ParserTest {

private static final String drl =
"package org.test;\n" +
Expand Down Expand Up @@ -94,7 +94,7 @@ void parse_basicRule() {

@Test
void computeTokenIndex_basicRule() {
DRLParser parser = createDrlParser(drl);
DRL10Parser parser = createDrlParser(drl);
parser.compilationUnit();

assertThat((int) computeTokenIndex(parser, 1, 0)).isEqualTo(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.drools.drl.ast.descr.ConstraintConnectiveDescr;
import org.drools.drl.ast.descr.RelationalExprDescr;
import org.drools.drl.parser.DrlExprParser;
import org.drools.drl.parser.DrlExprParserFactory;
import org.drools.drl.parser.DrlParser;
import org.drools.drl.parser.DroolsParserException;
import org.drools.drl.parser.impl.Operator;
Expand All @@ -38,7 +37,6 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.kie.internal.builder.conf.LanguageLevelOption;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
Expand All @@ -52,7 +50,7 @@ class DRLExprParserTest {

@BeforeEach
void setUp() {
this.parser = DrlExprParserFactory.getDrlExprParser(LanguageLevelOption.DRL6);
this.parser = ParserTestUtils.getExprParser();
}

@AfterEach
Expand Down Expand Up @@ -175,7 +173,7 @@ void bindingConstraint() {

@Test
void bindingWithRestrictions() {
String source = "$x : property > value && < 20";
String source = "$x : property > value && property < 20";
ConstraintConnectiveDescr result = parser.parse( source );
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

Expand Down Expand Up @@ -458,7 +456,7 @@ void noViableAlt() {
assertThat(exception.getColumn()).isEqualTo(2);
assertThat(exception.getOffset()).isEqualTo(2);
assertThat(exception.getMessage())
.isEqualToIgnoringCase("[ERR 101] Line 1:2 no viable alternative at input 'a'");
.isEqualToIgnoringCase("[ERR 101] Line 1:2 no viable alternative at input '~a'");
} else {
assertThat(parser.hasErrors()).isFalse();
}
Expand Down Expand Up @@ -578,4 +576,48 @@ void newBigDecimal() {
assertThat(bind.getVariable()).isEqualTo("$bd");
assertThat(bind.getExpression()).isEqualTo("new BigDecimal(30)");
}

@Test
void halfConstraintAnd() {
String source = "age > 10 && < 20";
parser.parse(source);

if (DrlParser.ANTLR4_PARSER_ENABLED) {
// half constraint is dropped in DRL10
assertThat(parser.hasErrors()).isTrue();
} else {
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();
}
}

@Test
void halfConstraintOr() {
String source = "name == \"John\" || == \"Paul\"";
parser.parse(source);

if (DrlParser.ANTLR4_PARSER_ENABLED) {
// half constraint is dropped in DRL10
assertThat(parser.hasErrors()).isTrue();
} else {
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();
}
}

@Test
void customOperator() {
Operator.addOperatorToRegistry("supersetOf", false);
// prefix '##' is required for custom operators in DRL10
String source = DrlParser.ANTLR4_PARSER_ENABLED ? "this ##supersetOf $list" : "this supersetOf $list";
ConstraintConnectiveDescr result = parser.parse(source);
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

RelationalExprDescr rel = (RelationalExprDescr) result.getDescrs().get(0);
assertThat(rel.getOperator()).isEqualTo("supersetOf");

AtomicExprDescr left = (AtomicExprDescr) rel.getLeft();
assertThat(left.getExpression()).isEqualTo("this");

AtomicExprDescr right = (AtomicExprDescr) rel.getRight();
assertThat(right.getExpression()).isEqualTo("$list");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ void typeFieldDescr() {
void attributeDescr() {
final String source = "rule R1\n" +
" salience 42\n" +
" agenda-group \"my_group\"\n" +
" ruleflow-group \"my_group\"\n" +
" when\n" +
" then\n" +
"end";
Expand All @@ -257,10 +257,10 @@ void attributeDescr() {
// Backward compatibility doesn't seem to be required in this case. (If do it, the code would be unnecessarily complicated.)
if (DrlParser.ANTLR4_PARSER_ENABLED) {
assertProperties(rule.getAttributes().get("salience"), 10, 21, 2, 2, 2, 12);
assertProperties(rule.getAttributes().get("agenda-group"), 24, 47, 3, 2, 3, 24);
assertProperties(rule.getAttributes().get("ruleflow-group"), 24, 49, 3, 2, 3, 26);
} else {
assertProperties(rule.getAttributes().get("salience"), 19, 21, 2, 11, 2, 12);
assertProperties(rule.getAttributes().get("agenda-group"), 37, 47, 3, 15, 3, 24);
assertProperties(rule.getAttributes().get("ruleflow-group"), 39, 49, 3, 17, 3, 26);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
import org.drools.drl.ast.descr.BindingDescr;
import org.drools.drl.ast.descr.ConstraintConnectiveDescr;
import org.drools.drl.parser.DrlExprParser;
import org.drools.drl.parser.DrlExprParserFactory;
import org.drools.mvel.evaluators.MatchesEvaluatorsDefinition;
import org.drools.mvel.evaluators.SetEvaluatorsDefinition;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.kie.internal.builder.conf.LanguageLevelOption;
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -47,6 +46,7 @@ void setUp() {
dumper = new DescrDumper();
}

@DisabledIfSystemProperty(named = "drools.drl.antlr4.parser.enabled", matches = "true")
@Test
void dump() {
String input = "price > 10 && < 20 || == $val || == 30";
Expand Down Expand Up @@ -147,6 +147,7 @@ void dumpWithDateAttr() {
assertThat(result).isEqualTo(expected);
}

@DisabledIfSystemProperty(named = "drools.drl.antlr4.parser.enabled", matches = "true")
@Test
void dumpComplex() {
String input = "a ( > 60 && < 70 ) || ( > 50 && < 55 ) && a3 == \"black\" || a == 40 && a3 == \"pink\" || a == 12 && a3 == \"yellow\" || a3 == \"blue\"";
Expand Down Expand Up @@ -220,6 +221,7 @@ void dumpBindings4() {
assertThat(result).isEqualTo(expected);
}

@DisabledIfSystemProperty(named = "drools.drl.antlr4.parser.enabled", matches = "true")
@Test
void dumpBindingsWithRestriction() {
String input = "$x : age > 10 && < 20 || > 30";
Expand Down Expand Up @@ -360,7 +362,7 @@ void processImplicitConstraints() {
}

public ConstraintConnectiveDescr parse(final String constraint) {
DrlExprParser parser = DrlExprParserFactory.getDrlExprParser(LanguageLevelOption.DRL6);
DrlExprParser parser = ParserTestUtils.getExprParser();
ConstraintConnectiveDescr result = parser.parse(constraint);
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

Expand Down
Loading