Search in sources :

Example 1 with RuleUnitDescription

use of org.kie.internal.ruleunit.RuleUnitDescription in project drools by kiegroup.

the class ModelGenerator method processRule.

private static void processRule(PackageDescr packageDescr, RuleContext context) {
    PackageModel packageModel = context.getPackageModel();
    RuleDescr ruleDescr = context.getRuleDescr();
    context.addGlobalDeclarations();
    context.setDialectFromAttributes(ruleDescr.getAttributes().values());
    for (Entry<String, Object> kv : ruleDescr.getNamedConsequences().entrySet()) {
        context.addNamedConsequence(kv.getKey(), kv.getValue().toString());
    }
    RuleUnitDescription ruleUnitDescr = context.getRuleUnitDescr();
    BlockStmt ruleVariablesBlock = context.getRuleVariablesBlock();
    new ModelGeneratorVisitor(context, packageModel).visit(getExtendedLhs(packageDescr, ruleDescr));
    if (context.hasCompilationError()) {
        return;
    }
    final String ruleMethodName = "rule_" + toId(ruleDescr.getName());
    MethodDeclaration ruleMethod = new MethodDeclaration(NodeList.nodeList(Modifier.publicModifier(), Modifier.staticModifier()), toClassOrInterfaceType(Rule.class), ruleMethodName);
    ruleMethod.setJavadocComment(" Rule name: " + ruleDescr.getName() + " ");
    VariableDeclarationExpr ruleVar = new VariableDeclarationExpr(toClassOrInterfaceType(Rule.class), "rule");
    MethodCallExpr ruleCall = createDslTopLevelMethod(RULE_CALL);
    if (!ruleDescr.getNamespace().isEmpty()) {
        ruleCall.addArgument(toStringLiteral(ruleDescr.getNamespace()));
    }
    ruleCall.addArgument(toStringLiteral(ruleDescr.getName()));
    MethodCallExpr buildCallScope = ruleUnitDescr != null ? new MethodCallExpr(ruleCall, UNIT_CALL).addArgument(new ClassExpr(toClassOrInterfaceType(ruleUnitDescr.getCanonicalName()))) : 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()));
    createVariables(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"));
    packageModel.putRuleMethod(ruleUnitDescr != null ? ruleUnitDescr.getSimpleName() : DEFAULT_RULE_UNIT, ruleMethod, context.getRuleIndex());
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) RuleUnitDescription(org.kie.internal.ruleunit.RuleUnitDescription) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) PackageModel(org.drools.modelcompiler.builder.PackageModel) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) Rule(org.drools.model.Rule) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 2 with RuleUnitDescription

use of org.kie.internal.ruleunit.RuleUnitDescription in project drools by kiegroup.

the class ModelGenerator method processRuleDescr.

private static void processRuleDescr(RuleContext context, PackageDescr packageDescr) {
    if (context.getRuleDescr() instanceof QueryDescr) {
        QueryGenerator.processQuery(context.getPackageModel(), (QueryDescr) context.getRuleDescr());
        return;
    }
    context.setDialectFromAttributes(packageDescr.getAttributes());
    processRule(packageDescr, context);
    RuleUnitDescription rud = context.getRuleUnitDescr();
    if (rud != null) {
        context.getPackageModel().addRuleUnit(rud);
    }
}
Also used : QueryDescr(org.drools.drl.ast.descr.QueryDescr) RuleUnitDescription(org.kie.internal.ruleunit.RuleUnitDescription)

Example 3 with RuleUnitDescription

use of org.kie.internal.ruleunit.RuleUnitDescription in project drools by kiegroup.

the class RuleContext method findUnitDescr.

private void findUnitDescr() {
    if (ruleDescr == null) {
        return;
    }
    boolean useNamingConvention = false;
    String unitName;
    AnnotationDescr unitAnn = ruleDescr.getAnnotation("Unit");
    if (unitAnn != null) {
        unitName = (String) unitAnn.getValue();
        unitName = unitName.substring(0, unitName.length() - ".class".length());
    } else if (ruleDescr.getUnit() != null) {
        unitName = ruleDescr.getUnit().getTarget();
    } else {
        if (ruleDescr.getResource() == null) {
            return;
        }
        String drlFile = ruleDescr.getResource().getSourcePath();
        if (drlFile != null) {
            String drlFileName = drlFile.substring(drlFile.lastIndexOf('/') + 1);
            unitName = packageModel.getName() + '.' + drlFileName.substring(0, drlFileName.length() - ".drl".length());
            useNamingConvention = true;
        } else {
            return;
        }
    }
    RuleUnitDescriptionLoader ruleUnitDescriptionLoader = kbuilder.getPackageRegistry(packageModel.getName()).getPackage().getRuleUnitDescriptionLoader();
    Optional<RuleUnitDescription> ruDescr = ruleUnitDescriptionLoader.getDescription(unitName);
    if (ruDescr.isPresent()) {
        ruleUnitDescr = ruDescr.get();
    } else if (!useNamingConvention) {
        throw new UnknownRuleUnitException(unitName);
    }
}
Also used : RuleUnitDescription(org.kie.internal.ruleunit.RuleUnitDescription) UnknownRuleUnitException(org.drools.modelcompiler.builder.errors.UnknownRuleUnitException) RuleUnitDescriptionLoader(org.drools.core.ruleunit.RuleUnitDescriptionLoader) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr)

Example 4 with RuleUnitDescription

use of org.kie.internal.ruleunit.RuleUnitDescription 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

RuleUnitDescription (org.kie.internal.ruleunit.RuleUnitDescription)4 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)2 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)2 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)2 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)2 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)2 ModelGeneratorVisitor (org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor)2 ClassExpr (com.github.javaparser.ast.expr.ClassExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1 Type (com.github.javaparser.ast.type.Type)1 HashMap (java.util.HashMap)1 RuleUnitDescriptionLoader (org.drools.core.ruleunit.RuleUnitDescriptionLoader)1 AnnotationDescr (org.drools.drl.ast.descr.AnnotationDescr)1 QueryDescr (org.drools.drl.ast.descr.QueryDescr)1 RuleDescr (org.drools.drl.ast.descr.RuleDescr)1 Rule (org.drools.model.Rule)1 PackageModel (org.drools.modelcompiler.builder.PackageModel)1 QueryModel (org.drools.modelcompiler.builder.QueryModel)1