This is an automated email from the ASF dual-hosted git repository.
tkobayas pushed a commit to branch dev-new-parser
in repository https://gitbox.apache.org/repos/asf/incubator-kie-drools.git
The following commit(s) were added to refs/heads/dev-new-parser by this push:
new 8621f68524 Improve the chunk rule not to include parentheses (#5801)
8621f68524 is described below
commit 8621f685244261c1bff7eeb00189c049d786e4f3
Author: Jiří Locker <[email protected]>
AuthorDate: Mon Mar 25 03:51:10 2024 +0100
Improve the chunk rule not to include parentheses (#5801)
---
.../drl/parser/antlr4/MiscDRLParserTest.java | 19 +++++++++++++++-
.../drl/parser/antlr4/rule_timer_attribute.drl | 26 ++++++++++++++++++++++
.../org/drools/drl/parser/antlr4/DRLParser.g4 | 6 ++---
.../drools/drl/parser/antlr4/DRLVisitorImpl.java | 6 ++---
4 files changed, 50 insertions(+), 7 deletions(-)
diff --git
a/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/MiscDRLParserTest.java
b/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/MiscDRLParserTest.java
index b7dd646269..1ae6522da0 100644
---
a/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/MiscDRLParserTest.java
+++
b/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/MiscDRLParserTest.java
@@ -1907,6 +1907,24 @@ class MiscDRLParserTest {
assertThat(at.getValue()).isEqualTo("true");
}
+ @Test
+ public void parse_Timer() throws Exception {
+ final RuleDescr rule =
parseAndGetFirstRuleDescrFromFile("rule_timer_attribute.drl" );
+ assertThat(rule.getName()).isEqualTo("simple_rule");
+ assertThat((String)
rule.getConsequence()).isEqualToIgnoringWhitespace( "bar();");
+
+ final Map<String, AttributeDescr> attrs = rule.getAttributes();
+ assertThat(attrs.size()).isEqualTo(2);
+
+ AttributeDescr at = (AttributeDescr) attrs.get( "timer" );
+ assertThat(at.getName()).isEqualTo("timer");
+ assertThat(at.getValue()).isEqualTo("int: 0 1; start=1_000_000,
repeat-limit=0");
+
+ at = (AttributeDescr) attrs.get( "lock-on-active" );
+ assertThat(at.getName()).isEqualTo("lock-on-active");
+ assertThat(at.getValue()).isEqualTo("true");
+ }
+
@Disabled("Priority : Low | Not written in docs nor other unit tests. Drop
the support?")
@Test
public void parse_Attributes_alternateSyntax() throws Exception {
@@ -3157,7 +3175,6 @@ class MiscDRLParserTest {
}
- @Disabled("Priority : Low | Implement multi-value annotation. Not written
in docs")
@Test
public void parse_MultiValueAnnotationsBackwardCompatibility() throws
Exception {
// multiple values with no keys are parsed as a single value
diff --git
a/drools-drl/drools-drl-parser-tests/src/test/resources/org/drools/drl/parser/antlr4/rule_timer_attribute.drl
b/drools-drl/drools-drl-parser-tests/src/test/resources/org/drools/drl/parser/antlr4/rule_timer_attribute.drl
new file mode 100644
index 0000000000..fe0377e25f
--- /dev/null
+++
b/drools-drl/drools-drl-parser-tests/src/test/resources/org/drools/drl/parser/antlr4/rule_timer_attribute.drl
@@ -0,0 +1,26 @@
+/**
+ * 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.
+ */
+
+
+rule simple_rule
+ timer (int: 0 1; start=1_000_000, repeat-limit=0)
+ lock-on-active true
+ when
+ Foo()
+ then
+ bar();
+end
diff --git
a/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRLParser.g4
b/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRLParser.g4
index fab8ee8fac..c7596d0144 100644
---
a/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRLParser.g4
+++
b/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRLParser.g4
@@ -460,7 +460,7 @@ type : (classOrInterfaceType | primitiveType)
typeArguments? ( DOT IDENTIFIER ty
drlArguments : LPAREN drlArgument (COMMA drlArgument)* RPAREN ;
drlArgument : ( stringId | floatLiteral | BOOL_LITERAL | NULL_LITERAL ) ;
-drlAnnotation : AT name=drlQualifiedName ((LPAREN ( drlElementValuePairs |
drlElementValue )? RPAREN) | chunk)? ;
+drlAnnotation : AT name=drlQualifiedName (LPAREN ( drlElementValuePairs |
drlElementValue | chunk )? RPAREN)? ;
drlElementValuePairs : drlElementValuePair (COMMA drlElementValuePair)* ;
drlElementValuePair : key=drlIdentifier ASSIGN value=drlElementValue ;
@@ -475,11 +475,11 @@ attribute : name=( 'salience' | 'enabled' )
conditionalOrExpression #expressionA
| name=( 'no-loop' | 'auto-focus' | 'lock-on-active' | 'refract' |
'direct' ) BOOL_LITERAL? #booleanAttribute
| name=( 'agenda-group' | 'activation-group' | 'ruleflow-group' |
'date-effective' | 'date-expires' | 'dialect' ) DRL_STRING_LITERAL
#stringAttribute
| name='calendars' DRL_STRING_LITERAL ( COMMA DRL_STRING_LITERAL )*
#stringListAttribute
- | name='timer' ( DECIMAL_LITERAL | chunk ) #intOrChunkAttribute
+ | name='timer' ( DECIMAL_LITERAL | LPAREN chunk RPAREN )
#intOrChunkAttribute
| name='duration' ( DECIMAL_LITERAL | TIME_INTERVAL | LPAREN
TIME_INTERVAL RPAREN ) #durationAttribute
;
-chunk : LPAREN .+? RPAREN;
+chunk : .+?;
assignmentOperator : ASSIGN
| ADD_ASSIGN
diff --git
a/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLVisitorImpl.java
b/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLVisitorImpl.java
index 6efefc7e60..0de2de1239 100644
---
a/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLVisitorImpl.java
+++
b/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLVisitorImpl.java
@@ -64,7 +64,6 @@ import static
org.drools.drl.parser.antlr4.Antlr4ParserStringUtils.getTokenTextP
import static org.drools.drl.parser.antlr4.Antlr4ParserStringUtils.trimThen;
import static
org.drools.drl.parser.antlr4.DRLParserHelper.getTextWithoutErrorNode;
import static org.drools.drl.parser.util.ParserStringUtils.appendPrefix;
-import static org.drools.drl.parser.util.ParserStringUtils.safeStripDelimiters;
import static
org.drools.drl.parser.util.ParserStringUtils.safeStripStringDelimiters;
import static org.drools.util.StringUtils.unescapeJava;
@@ -323,10 +322,11 @@ public class DRLVisitorImpl extends
DRLParserBaseVisitor<Object> {
AnnotationDescr annotationDescr = new
AnnotationDescr(ctx.name.getText());
if (ctx.drlElementValue() != null) {
annotationDescr.setValue(getTextPreservingWhitespace(ctx.drlElementValue()));
// single value
- } else if (ctx.chunk() != null) {
-
annotationDescr.setValue(safeStripDelimiters(getTextPreservingWhitespace(ctx.chunk()),
"(", ")"));
} else if (ctx.drlElementValuePairs() != null) {
visitDrlElementValuePairs(ctx.drlElementValuePairs(),
annotationDescr); // multiple values
+ } else if (ctx.chunk() != null) {
+ // A chunk that is neither a single value nor a list of key-value
pairs. For example `!*, age` in `@watch(!*, age)`.
+ annotationDescr.setValue(getTextPreservingWhitespace(ctx.chunk()));
}
return annotationDescr;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]