Skip to content

Commit

Permalink
[incubator-kie-drools-5712] Queries with arguments cannot be parsed (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
tkobayas committed Oct 11, 2024
1 parent 3ed1305 commit c991702
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3909,4 +3909,74 @@ void inlineCastThis() {
ExprConstraintDescr constraintDescr = getFirstExprConstraintDescr(packageDescr);
assertThat(constraintDescr.toString()).isEqualToIgnoringWhitespace("this#Person.name == \"Mark\"");
}

@Test
public void queryArgumentWithoutType() throws Exception {
final String text = "package org.drools\n" +
"query olderThan( $age )\n" +
" $p : Person(age > (Integer)$age)\n" +
"end ";
final QueryDescr query = parseAndGetFirstQueryDescr(text);

assertThat(query).isNotNull();
assertThat(query.getName()).isEqualTo("olderThan");
assertThat(query.getParameterTypes()).containsExactly("Object");
assertThat(query.getParameters()).containsExactly("$age");
}

@Test
public void queryMultipleArguments() throws Exception {
final String text = "package org.drools\n" +
"query olderThan( String $name, int $age )\n" +
" $p : Person(age > $age)\n" +
"end ";
final QueryDescr query = parseAndGetFirstQueryDescr(text);

assertThat(query).isNotNull();
assertThat(query.getName()).isEqualTo("olderThan");
assertThat(query.getParameterTypes()).containsExactly("String", "int");
assertThat(query.getParameters()).containsExactly("$name", "$age");
}

@Test
public void queryArrayArgument() throws Exception {
final String text = "package org.drools\n" +
"query olderThan( int[] $ages )\n" +
" $p : Person(age > $ages[0])\n" +
"end ";
final QueryDescr query = parseAndGetFirstQueryDescr(text);

assertThat(query).isNotNull();
assertThat(query.getName()).isEqualTo("olderThan");
assertThat(query.getParameterTypes()).containsExactly("int[]");
assertThat(query.getParameters()).containsExactly("$ages");
}

@Test
public void queryZeroArgument() throws Exception {
final String text = "package org.drools\n" +
"query olderThan()\n" +
" $p : Person()\n" +
"end ";
final QueryDescr query = parseAndGetFirstQueryDescr(text);

assertThat(query).isNotNull();
assertThat(query.getName()).isEqualTo("olderThan");
assertThat(query.getParameterTypes()).isEmpty();
assertThat(query.getParameters()).isEmpty();
}

@Test
public void queryNoArgument() throws Exception {
final String text = "package org.drools\n" +
"query olderThan\n" +
" $p : Person()\n" +
"end ";
final QueryDescr query = parseAndGetFirstQueryDescr(text);

assertThat(query).isNotNull();
assertThat(query.getName()).isEqualTo("olderThan");
assertThat(query.getParameterTypes()).isEmpty();
assertThat(query.getParameters()).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ ruledef : DRL_RULE name=stringId (EXTENDS parentName=stringId)? drlAnnotation* a

// query := QUERY stringId parameters? annotation* lhsExpression END

querydef : DRL_QUERY name=stringId formalParameters? drlAnnotation* lhsExpression+ DRL_END SEMI?;
querydef : DRL_QUERY name=stringId parameters? drlAnnotation* lhsExpression+ DRL_END SEMI?;

// parameters := LEFT_PAREN ( parameter ( COMMA parameter )* )? RIGHT_PAREN
parameters : LPAREN ( parameter ( COMMA parameter )* )? RPAREN ;

// parameter := ({requiresType}?=>type)? ID (LEFT_SQUARE RIGHT_SQUARE)*
parameter : type? drlIdentifier ; // type is optional. Removed (LEFT_SQUARE RIGHT_SQUARE)* as it doesn't make sense in the grammar

lhs : DRL_WHEN lhsExpression* ;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,13 @@ private void slimLhsRootDescr(AndDescr root) {
public QueryDescr visitQuerydef(DRLParser.QuerydefContext ctx) {
QueryDescr queryDescr = new QueryDescr(safeStripStringDelimiters(ctx.name.getText()));

DRLParser.FormalParametersContext formalParametersContext = ctx.formalParameters();
if (formalParametersContext != null) {
DRLParser.FormalParameterListContext formalParameterListContext = formalParametersContext.formalParameterList();
List<DRLParser.FormalParameterContext> formalParameterContexts = formalParameterListContext.formalParameter();
formalParameterContexts.forEach(formalParameterContext -> {
DRLParser.TypeTypeContext typeTypeContext = formalParameterContext.typeType();
DRLParser.VariableDeclaratorIdContext variableDeclaratorIdContext = formalParameterContext.variableDeclaratorId();
queryDescr.addParameter(typeTypeContext.getText(), variableDeclaratorIdContext.getText());
DRLParser.ParametersContext parametersContext = ctx.parameters();
if (parametersContext != null) {
List<DRLParser.ParameterContext> parameterContexts = parametersContext.parameter();
parameterContexts.forEach(parameterContext -> {
String type = parameterContext.type() != null ? parameterContext.type().getText() : "Object"; // default type is Object
String name = parameterContext.drlIdentifier().getText();
queryDescr.addParameter(type, name);
});
}

Expand Down

0 comments on commit c991702

Please sign in to comment.