Search in sources :

Example 1 with QUERY_INVOCATION_CALL

use of org.drools.modelcompiler.builder.generator.ModelGenerator.QUERY_INVOCATION_CALL 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());
    List<QueryParameter> parameters = packageModel.getQueryDefWithType().get(queryDef).getContext().getQueryParameters();
    for (int i = 0; i < parameters.size(); i++) {
        String queryName = context.getQueryName().orElseThrow(RuntimeException::new);
        ExprConstraintDescr variableExpr = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(i);
        String variableName = variableExpr.toString();
        int unifPos = variableName.indexOf(":=");
        if (unifPos > 0) {
            variableName = variableName.substring(0, unifPos).trim();
        }
        Optional<String> unificationId = context.getUnificationId(variableName);
        int queryIndex = i + 1;
        Expression parameterCall = unificationId.map(name -> (Expression) new NameExpr(toVar(name))).orElseGet(() -> new MethodCallExpr(new NameExpr(queryName), QueryGenerator.toQueryArg(queryIndex)));
        callMethod.addArgument(parameterCall);
    }
    context.addExpression(callMethod);
}
Also used : PackageModel(org.drools.modelcompiler.builder.PackageModel) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Expression(org.drools.javaparser.ast.expr.Expression) QUERY_INVOCATION_CALL(org.drools.modelcompiler.builder.generator.ModelGenerator.QUERY_INVOCATION_CALL) QueryGenerator(org.drools.modelcompiler.builder.generator.QueryGenerator) QueryParameter(org.drools.modelcompiler.builder.generator.QueryParameter) NameExpr(org.drools.javaparser.ast.expr.NameExpr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) List(java.util.List) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) DSLNode(org.drools.modelcompiler.builder.generator.visitor.DSLNode) DrlxParseUtil.toVar(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toVar) Optional(java.util.Optional) QueryParameter(org.drools.modelcompiler.builder.generator.QueryParameter) Expression(org.drools.javaparser.ast.expr.Expression) NameExpr(org.drools.javaparser.ast.expr.NameExpr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Aggregations

List (java.util.List)1 Optional (java.util.Optional)1 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)1 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)1 Expression (org.drools.javaparser.ast.expr.Expression)1 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)1 NameExpr (org.drools.javaparser.ast.expr.NameExpr)1 PackageModel (org.drools.modelcompiler.builder.PackageModel)1 DrlxParseUtil.toVar (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toVar)1 QUERY_INVOCATION_CALL (org.drools.modelcompiler.builder.generator.ModelGenerator.QUERY_INVOCATION_CALL)1 QueryGenerator (org.drools.modelcompiler.builder.generator.QueryGenerator)1 QueryParameter (org.drools.modelcompiler.builder.generator.QueryParameter)1 RuleContext (org.drools.modelcompiler.builder.generator.RuleContext)1 DSLNode (org.drools.modelcompiler.builder.generator.visitor.DSLNode)1