use of org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor in project drools by kiegroup.
the class ModelGenerator method processRule.
private static void processRule(KnowledgeBuilderImpl kbuilder, TypeResolver typeResolver, PackageModel packageModel, PackageDescr packageDescr, RuleDescr ruleDescr, boolean isPattern) {
RuleContext context = new RuleContext(kbuilder, packageModel, ruleDescr, typeResolver, isPattern);
for (Entry<String, Object> kv : ruleDescr.getNamedConsequences().entrySet()) {
context.addNamedConsequence(kv.getKey(), kv.getValue().toString());
}
new ModelGeneratorVisitor(context, packageModel).visit(getExtendedLhs(packageDescr, ruleDescr));
final String ruleMethodName = "rule_" + toId(ruleDescr.getName());
MethodDeclaration ruleMethod = new MethodDeclaration(EnumSet.of(Modifier.PUBLIC, Modifier.STATIC), RULE_TYPE, ruleMethodName);
ruleMethod.setJavadocComment(" Rule name: " + ruleDescr.getName() + " ");
VariableDeclarationExpr ruleVar = new VariableDeclarationExpr(RULE_TYPE, RULE_CALL);
MethodCallExpr ruleCall = new MethodCallExpr(null, RULE_CALL);
if (!ruleDescr.getNamespace().isEmpty()) {
ruleCall.addArgument(new StringLiteralExpr(ruleDescr.getNamespace()));
}
ruleCall.addArgument(new StringLiteralExpr(ruleDescr.getName()));
RuleUnitDescr ruleUnitDescr = context.getRuleUnitDescr();
MethodCallExpr buildCallScope = ruleUnitDescr != null ? new MethodCallExpr(ruleCall, UNIT_CALL).addArgument(new ClassExpr(classToReferenceType(ruleUnitDescr.getRuleUnitClass()))) : ruleCall;
for (MethodCallExpr attributeExpr : ruleAttributes(context, ruleDescr)) {
attributeExpr.setScope(buildCallScope);
buildCallScope = attributeExpr;
}
for (MethodCallExpr metaAttributeExpr : ruleMetaAttributes(context, ruleDescr)) {
metaAttributeExpr.setScope(buildCallScope);
buildCallScope = metaAttributeExpr;
}
MethodCallExpr buildCall = new MethodCallExpr(buildCallScope, BUILD_CALL, NodeList.nodeList(context.getExpressions()));
BlockStmt ruleVariablesBlock = new BlockStmt();
createUnitData(ruleUnitDescr, ruleVariablesBlock);
createVariables(kbuilder, ruleVariablesBlock, packageModel, context);
ruleMethod.setBody(ruleVariablesBlock);
MethodCallExpr executeCall = new Consequence(context).createCall(ruleDescr, ruleDescr.getConsequence().toString(), ruleVariablesBlock, false);
buildCall.addArgument(executeCall);
ruleVariablesBlock.addStatement(new AssignExpr(ruleVar, buildCall, AssignExpr.Operator.ASSIGN));
ruleVariablesBlock.addStatement(new ReturnStmt(RULE_CALL));
packageModel.putRuleMethod(ruleMethodName, ruleMethod);
}
use of org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor in project drools by kiegroup.
the class QueryGenerator method processQuery.
public static void processQuery(KnowledgeBuilderImpl kbuilder, PackageModel packageModel, QueryDescr queryDescr) {
String queryDefVariableName = toQueryDef(queryDescr.getName());
RuleContext context = packageModel.getQueryDefWithType().get(queryDefVariableName).getContext();
new ModelGeneratorVisitor(context, packageModel).visit(queryDescr.getLhs());
final Type queryType = JavaParser.parseType(Query.class.getCanonicalName());
MethodDeclaration queryMethod = new MethodDeclaration(EnumSet.of(Modifier.PRIVATE), queryType, "query_" + toId(queryDescr.getName()));
BlockStmt queryBody = new BlockStmt();
ModelGenerator.createVariables(kbuilder, 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);
}
Aggregations