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][incubator-kie-drools-6220] Slim down DRL syntax with New Antlr4 Parser #6225

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
2 changes: 1 addition & 1 deletion .github/workflows/pr-drools.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
- name: Build Chain
uses: apache/incubator-kie-kogito-pipelines/.ci/actions/build-chain@main
env:
BUILD_MVN_OPTS_CURRENT: '-Dfull -Dreproducible'
BUILD_MVN_OPTS_CURRENT: '-Dfull -Dreproducible -DenableNewParser'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a temporal change to test the new parser (= DRL10). I'll revert this after review.

MAVEN_OPTS: "-Dfile.encoding=UTF-8"
with:
definition-file: https://raw.githubusercontent.com/${GROUP:apache}/incubator-kie-kogito-pipelines/${BRANCH:main}/.ci/pull-request-config.yaml
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
@@ -0,0 +1,146 @@
////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
////

[id='language-level_drl-rules']

= Language Level

Language Level is a feature that allows you to specify the version of the DRL (Drools Rule Language) syntax you want to use.

Supported language levels are:

* DRL6 (default)
* DRL10

Some old language levels are deprecated and will be removed in the future:

* DRL5
* DRL6_STRICT

DRL6 is compatible with the previous Drools versions, while DRL10 introduces some changes to reduce ambiguity.

Language Level can be specified by kmodule.xml or system property. Note that the configuration should be effective when rules are built.

.kmodule.xml example
[source,xml]
----
<kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.drools.org/xsd/kmodule">
<configuration>
<property key="drools.lang.level" value="DRL10"/>
</configuration>
</kmodule>
----

.System property example
[source,java]
----
System.setProperty("drools.lang.level", "DRL10");
----

== DRL10

DRL10 has been added, which is handled by the new parser based on ANTLR4. DRL10 introduces the following changes mainly to reduce ambiguity:

* Require a prefix `##` to custom operators
.DRL6
[source]
----
Person(addresses supersetOf $alice.addresses)
----

.DRL10
[source]
----
Person(addresses ##supersetOf $alice.addresses)
----

The custom operator's implementation Java code wouldn't need to be changed.

* Drop half constraint syntax

.DRL6
[source]
----
Person(age > 10 && < 20)
----

.DRL10 (also valid in DRL6)
[source]
----
Person(age > 10 && age < 20)
----

* Drop double ampersand as infix and

.DRL6
[source]
----
( Person(age == 10)
&&
Person(age == 20) )
----

.DRL10 (also valid in DRL6)
[source]
----
( Person(age == 10)
and
Person(age == 20) )
----

`&&` in a constraint will remain supported. For example, `Person(age == 10 && age == 20)` is valid in DRL6 and DRL10.

* Drop double pipe as infix or

.DRL6
[source]
----
( Person(age == 10)
||
Person(age == 20) )
----

.DRL10 (also valid in DRL6)
[source]
----
( Person(age == 10)
or
Person(age == 20) )
----

`||` in a constraint will remain supported. For example, `Person(age == 10 || age == 20)` is valid in DRL6 and DRL10.

* Drop annotation inside LHS pattern

.DRL6
[source]
----
( Double()
or @Annot1 String()
or @Annot2 Integer() )
----

There is no equivalent expression in DRL10.

Annotation in other places will remain supported. For example, `Person(name == "Mario") @watch(*)` is valid in DRL6 and DRL10.

Those syntaxes are deprecated and will be removed in the future. Warnings are logged when the deprecated syntax is used in DRL6.

Warnings are logged when the deprecated syntax is used in DRL6. The warning logs can be suppressed by the log level of `org.drools.drl.parser.lang.ParserHelper`.
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ include::_drl-rules.adoc[leveloffset=+1]
include::_performance-tuning-drl-ref.adoc[leveloffset=+2]

include::_XLSDecisionTable-section.adoc[leveloffset=+1]

include::_language-level.adoc[leveloffset=+1]
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
////

== 10.1.0 release notes

=== Language Level DRL10
New language level DRL10 has been added. DRL10 is handled by the new parser based on ANTLR4. The default language level is still DRL6.

DRL10 introduces the following changes mainly to reduce ambiguity:

* Require a prefix '##' to custom operators
* Drop half constraint syntax
* Drop double ampersand as infix and
* Drop double pipe as infix or
* Drop annotation inside LHS pattern

Those syntaxes are deprecated and will be removed in the future. Warnings are logged when the deprecated syntax is used in DRL6.

You can refer xref:language-reference/index.adoc#language-level_drl-rules[Language Level] for details.
2 changes: 2 additions & 0 deletions drools-docs/src/modules/ROOT/pages/release-notes/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ This chapter contains the release notes for the {PRODUCT} 10-series.

For the release notes of the _previous releases_, you can refer to the {PRODUCT} documentation of link:https://docs.drools.org/8.44.0.Final/drools-docs/drools/release-notes/index.html[version 8].

include::_release-notes-10.1.adoc[leveloffset=0]

include::_release-notes-10.0.adoc[leveloffset=0]

== Drools 10-series release notes
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");
}
}
Loading