Search in sources :

Example 1 with QueryParameter

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);
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) QueryParameter(org.drools.modelcompiler.builder.generator.QueryParameter) NameExpr(com.github.javaparser.ast.expr.NameExpr) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) Expression(com.github.javaparser.ast.expr.Expression) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) BaseDescr(org.drools.drl.ast.descr.BaseDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 2 with QueryParameter

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);
}
Also used : PackageModel(org.drools.modelcompiler.builder.PackageModel) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) List(java.util.List) DSLNode(org.drools.modelcompiler.builder.generator.visitor.DSLNode) QueryGenerator(org.drools.modelcompiler.builder.generator.QueryGenerator) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) QueryParameter(org.drools.modelcompiler.builder.generator.QueryParameter) QUERY_INVOCATION_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.QUERY_INVOCATION_CALL) Expression(com.github.javaparser.ast.expr.Expression) PatternDescr(org.drools.drl.ast.descr.PatternDescr) NameExpr(com.github.javaparser.ast.expr.NameExpr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) QueryParameter(org.drools.modelcompiler.builder.generator.QueryParameter) Expression(com.github.javaparser.ast.expr.Expression) NameExpr(com.github.javaparser.ast.expr.NameExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

Expression (com.github.javaparser.ast.expr.Expression)2 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)2 NameExpr (com.github.javaparser.ast.expr.NameExpr)2 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)2 QueryParameter (org.drools.modelcompiler.builder.generator.QueryParameter)2 StaticJavaParser.parseExpression (com.github.javaparser.StaticJavaParser.parseExpression)1 List (java.util.List)1 BaseDescr (org.drools.drl.ast.descr.BaseDescr)1 BindingDescr (org.drools.drl.ast.descr.BindingDescr)1 PatternDescr (org.drools.drl.ast.descr.PatternDescr)1 PackageModel (org.drools.modelcompiler.builder.PackageModel)1 InvalidExpressionErrorResult (org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult)1 QUERY_INVOCATION_CALL (org.drools.modelcompiler.builder.generator.DslMethodNames.QUERY_INVOCATION_CALL)1 QueryGenerator (org.drools.modelcompiler.builder.generator.QueryGenerator)1 RuleContext (org.drools.modelcompiler.builder.generator.RuleContext)1 DSLNode (org.drools.modelcompiler.builder.generator.visitor.DSLNode)1