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

[incubator-kie-drools-5712] Queries with arguments cannot be parsed #5811

Merged
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
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
Loading