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());
}
}
Aggregations