Search in sources :

Example 1 with ModelGeneratorVisitor

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);
}
Also used : VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) MethodDeclaration(org.drools.javaparser.ast.body.MethodDeclaration) RuleUnitDescr(org.drools.core.ruleunit.RuleUnitDescr) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) ReturnStmt(org.drools.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 2 with ModelGeneratorVisitor

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);
}
Also used : VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) Query(org.drools.model.Query) MethodDeclaration(org.drools.javaparser.ast.body.MethodDeclaration) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) NameExpr(org.drools.javaparser.ast.expr.NameExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) Type(org.drools.javaparser.ast.type.Type) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) ReturnStmt(org.drools.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Aggregations

MethodDeclaration (org.drools.javaparser.ast.body.MethodDeclaration)2 AssignExpr (org.drools.javaparser.ast.expr.AssignExpr)2 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)2 VariableDeclarationExpr (org.drools.javaparser.ast.expr.VariableDeclarationExpr)2 BlockStmt (org.drools.javaparser.ast.stmt.BlockStmt)2 ReturnStmt (org.drools.javaparser.ast.stmt.ReturnStmt)2 ModelGeneratorVisitor (org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor)2 RuleUnitDescr (org.drools.core.ruleunit.RuleUnitDescr)1 ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)1 NameExpr (org.drools.javaparser.ast.expr.NameExpr)1 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)1 ClassOrInterfaceType (org.drools.javaparser.ast.type.ClassOrInterfaceType)1 Type (org.drools.javaparser.ast.type.Type)1 Query (org.drools.model.Query)1