Skip to content

Commit

Permalink
Improve the chunk rule not to include parentheses (apache#5801)
Browse files Browse the repository at this point in the history
  • Loading branch information
yurloc authored and rgdoliveira committed Oct 24, 2024
1 parent ac300e3 commit 51bb4f2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,24 @@ public void parse_Calendars2() throws Exception {
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 {
Expand Down Expand Up @@ -3157,7 +3175,6 @@ public void parse_FromWithTernaryFollowedByQuery() throws Exception {

}

@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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
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;

Expand Down Expand Up @@ -323,10 +322,11 @@ public AnnotationDescr visitDrlAnnotation(DRLParser.DrlAnnotationContext ctx) {
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;
}
Expand Down

0 comments on commit 51bb4f2

Please sign in to comment.