use of org.drools.modelcompiler.builder.generator.QueryParameter in project drools by kiegroup.
the class Query method buildPattern.
@Override
public void buildPattern() {
NameExpr queryCall = new NameExpr(toQueryDef(pattern.getObjectType()));
MethodCallExpr callCall = new MethodCallExpr(queryCall, QUERY_INVOCATION_CALL);
callCall.addArgument("" + !pattern.isQuery());
if (!constraintDescrs.isEmpty()) {
List<QueryParameter> queryParams = packageModel.queryVariables(queryName);
if (queryParams.size() != constraintDescrs.size()) {
context.addCompilationError(new InvalidExpressionErrorResult("Wrong number of argument invoking query '" + queryName + "'"));
return;
}
Expression[] queryArgs = new Expression[queryParams.size()];
for (int i = 0; i < constraintDescrs.size(); i++) {
BaseDescr baseDescr = constraintDescrs.get(i);
String itemText = baseDescr.getText();
boolean isPositional = baseDescr instanceof ExprConstraintDescr && ((ExprConstraintDescr) baseDescr).getType() == ExprConstraintDescr.Type.POSITIONAL;
boolean isBinding = baseDescr instanceof BindingDescr || itemText.contains(":");
if ((!isPositional) && (!isBinding)) {
// error, can't have non binding slots.
context.addCompilationError(new InvalidExpressionErrorResult("Query's must use positional or bindings, not field constraints: " + itemText));
} else if (isPositional && isBinding) {
// error, can't have positional binding slots.
context.addCompilationError(new InvalidExpressionErrorResult("Query's can't use positional bindings: " + itemText));
}
int colonPos = itemText.indexOf(':');
if (colonPos > 0) {
String bindingId = itemText.substring(0, colonPos).trim();
String paramName = itemText.substring(colonPos + 1).trim();
for (int j = 0; j < queryParams.size(); j++) {
if (queryParams.get(j).getName().equals(paramName)) {
addQueryArg(queryParams, queryArgs, bindingId, j);
break;
} else if (queryParams.get(j).getName().equals(bindingId)) {
addQueryArg(queryParams, queryArgs, paramName, j);
break;
}
}
} else {
addQueryArg(queryParams, queryArgs, itemText, i);
}
}
for (Expression queryArg : queryArgs) {
callCall.addArgument(queryArg);
}
}
context.addExpression(callCall);
}
use of org.drools.modelcompiler.builder.generator.QueryParameter in project drools by kiegroup.
the class QueryCall method buildPattern.
@Override
public void buildPattern() {
MethodCallExpr callMethod = new MethodCallExpr(new NameExpr(queryDef), QUERY_INVOCATION_CALL);
callMethod.addArgument("" + !pattern.isQuery());
String queryName = context.getQueryName().orElseThrow(RuntimeException::new);
List<QueryParameter> parameters = packageModel.getQueryDefWithType().get(queryDef).getContext().getQueryParameters();
for (int i = 0; i < parameters.size(); i++) {
String variableName = getVariableName(i);
Expression parameter = context.getQueryParameterByName(variableName).map(qp -> (Expression) new MethodCallExpr(new NameExpr(queryName), QueryGenerator.toQueryArg(qp.getIndex()))).orElseGet(() -> context.getUnificationId(variableName).map(name -> context.getVarExpr(name)).orElseGet(() -> context.getVarExpr(variableName)));
callMethod.addArgument(parameter);
}
context.addExpression(callMethod);
}
Aggregations