Search in sources :

Example 1 with QueryModel

use of org.drools.modelcompiler.builder.QueryModel in project drools by kiegroup.

the class QueryGenerator method processQuery.

public static void processQuery(PackageModel packageModel, QueryDescr queryDescr) {
    String queryDefVariableName = toQueryDef(queryDescr.getName());
    RuleContext context = packageModel.getQueryDefWithType().get(queryDefVariableName).getContext();
    context.addGlobalDeclarations();
    context.setDialectFromAttributes(queryDescr.getAttributes().values());
    new ModelGeneratorVisitor(context, packageModel).visit(queryDescr.getLhs());
    if (context.getRuleUnitDescr() != null) {
        Map<String, Class<?>> queryBindings = new HashMap<>();
        for (DeclarationSpec declr : context.getAllDeclarations()) {
            if (!declr.isGlobal() && !declr.getBindingId().startsWith(GENERATED_VARIABLE_PREFIX)) {
                queryBindings.put(declr.getBindingId(), declr.getDeclarationClass());
            }
        }
        QueryModel queryModel = new QueryModel(queryDescr.getName(), queryDescr.getNamespace(), queryDescr.getParameters(), queryBindings);
        packageModel.addQueryInRuleUnit(context.getRuleUnitDescr(), queryModel);
    }
    final Type queryType = toClassOrInterfaceType(Query.class);
    MethodDeclaration queryMethod = new MethodDeclaration(NodeList.nodeList(Modifier.privateModifier()), queryType, QUERY_METHOD_PREFIX + toId(queryDescr.getName()));
    BlockStmt queryBody = new BlockStmt();
    ModelGenerator.createVariables(queryBody, packageModel, context);
    queryMethod.setBody(queryBody);
    String queryBuildVarName = toId(queryDescr.getName()) + "_build";
    VariableDeclarationExpr queryBuildVar = new VariableDeclarationExpr(queryType, queryBuildVarName);
    MethodCallExpr buildCall = new MethodCallExpr(new NameExpr(queryDefVariableName), BUILD_CALL);
    context.getExpressions().forEach(buildCall::addArgument);
    AssignExpr queryBuildAssign = new AssignExpr(queryBuildVar, buildCall, AssignExpr.Operator.ASSIGN);
    queryBody.addStatement(queryBuildAssign);
    queryBody.addStatement(new ReturnStmt(queryBuildVarName));
    packageModel.putQueryMethod(queryMethod);
    RuleUnitDescription ruleUnitDescr = context.getRuleUnitDescr();
    if (ruleUnitDescr != null) {
        packageModel.putRuleUnit(ruleUnitDescr.getSimpleName());
    }
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) RuleUnitDescription(org.kie.internal.ruleunit.RuleUnitDescription) HashMap(java.util.HashMap) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) QueryModel(org.drools.modelcompiler.builder.QueryModel) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1 Type (com.github.javaparser.ast.type.Type)1 HashMap (java.util.HashMap)1 QueryModel (org.drools.modelcompiler.builder.QueryModel)1 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)1 ModelGeneratorVisitor (org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor)1 RuleUnitDescription (org.kie.internal.ruleunit.RuleUnitDescription)1