Search in sources :

Example 1 with MissingDependencyError

use of org.drools.compiler.compiler.MissingDependencyError in project drools by kiegroup.

the class AbstractASMConsequenceBuilder method consequenceContext.

private Map<String, Object> consequenceContext(RuleBuildContext context, String consequenceName) {
    String className = consequenceName + "Consequence";
    Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule(), consequenceName);
    JavaAnalysisResult analysis = JavaRuleBuilderHelper.createJavaAnalysisResult(context, consequenceName, decls);
    if (analysis == null) {
        // not possible to get the analysis results
        return null;
    }
    // this will fix modify, retract, insert, update, entrypoints and channels
    try {
        String fixedConsequence = KnowledgeHelperFixer.fix(AsmUtil.fixBlockDescr(context, analysis, decls));
        return JavaRuleBuilderHelper.createConsequenceContext(context, consequenceName, className, fixedConsequence, decls, analysis.getBoundIdentifiers());
    } catch (MissingDependencyException e) {
        context.addError(new MissingDependencyError(context.getRuleDescr().getResource(), e));
    }
    return null;
}
Also used : JavaAnalysisResult(org.drools.mvel.java.JavaAnalysisResult) MissingDependencyError(org.drools.compiler.compiler.MissingDependencyError) Declaration(org.drools.core.rule.Declaration) MissingDependencyException(org.drools.core.common.MissingDependencyException)

Example 2 with MissingDependencyError

use of org.drools.compiler.compiler.MissingDependencyError in project drools by kiegroup.

the class Consequence method rewriteRHS.

private boolean rewriteRHS(BlockStmt ruleBlock, BlockStmt rhs) {
    AtomicBoolean requireDrools = new AtomicBoolean(false);
    List<MethodCallExpr> methodCallExprs = rhs.findAll(MethodCallExpr.class);
    List<AssignExpr> assignExprs = rhs.findAll(AssignExpr.class);
    List<MethodCallExpr> updateExprs = new ArrayList<>();
    Map<String, Type> rhsBodyDeclarations = new HashMap<>();
    for (VariableDeclarator variableDeclarator : rhs.findAll(VariableDeclarator.class)) {
        rhsBodyDeclarations.put(variableDeclarator.getNameAsString(), variableDeclarator.getType());
    }
    for (MethodCallExpr methodCallExpr : methodCallExprs) {
        if (!methodCallExpr.getScope().isPresent() && isImplicitDroolsMethod(methodCallExpr)) {
            if (methodCallExpr.getNameAsString().equals("insertLogical") && !TruthMaintenanceSystemFactory.present()) {
                context.addCompilationError(new MissingDependencyError(TruthMaintenanceSystemFactory.NO_TMS));
            }
            methodCallExpr.setScope(new NameExpr("drools"));
        }
        if (hasDroolsScope(methodCallExpr) || hasDroolsAsParameter(methodCallExpr)) {
            if (knowledgeHelperMethods.contains(methodCallExpr.getNameAsString())) {
                methodCallExpr.setScope(createAsKnowledgeHelperExpression());
            } else if (methodCallExpr.getNameAsString().equals("update")) {
                if (methodCallExpr.toString().contains("FactHandle")) {
                    methodCallExpr.setScope(new NameExpr("((org.drools.modelcompiler.consequence.DroolsImpl) drools)"));
                }
                updateExprs.add(methodCallExpr);
            } else if (methodCallExpr.getNameAsString().equals("retract")) {
                methodCallExpr.setName(new SimpleName("delete"));
            }
            requireDrools.set(true);
        }
    }
    Set<String> initializedBitmaskFields = new HashSet<>();
    for (MethodCallExpr updateExpr : updateExprs) {
        Expression argExpr = updateExpr.getArgument(0);
        if (argExpr instanceof NameExpr) {
            String updatedVar = ((NameExpr) argExpr).getNameAsString();
            Class<?> updatedClass = classFromRHSDeclarations(rhsBodyDeclarations, updatedVar);
            // We might need to generate the domain metadata class for types used in consequence
            // without an explicit pattern. See CompilerTest.testConsequenceInsertThenUpdate
            context.getPackageModel().registerDomainClass(updatedClass);
            if (context.isPropertyReactive(updatedClass)) {
                if (!initializedBitmaskFields.contains(updatedVar)) {
                    Set<String> modifiedProps = findModifiedProperties(methodCallExprs, updateExpr, updatedVar, updatedClass);
                    modifiedProps.addAll(findModifiedPropertiesFromAssignment(assignExprs, updateExpr, updatedVar, updatedClass));
                    MethodCallExpr bitMaskCreation = createBitMaskInitialization(updatedClass, modifiedProps);
                    AssignExpr bitMaskAssign = createBitMaskField(updatedVar, bitMaskCreation);
                    if (!DrlxParseUtil.hasDuplicateExpr(ruleBlock, bitMaskAssign)) {
                        ruleBlock.addStatement(bitMaskAssign);
                    }
                }
                updateExpr.addArgument("mask_" + updatedVar);
                initializedBitmaskFields.add(updatedVar);
            }
        }
    }
    return requireDrools.get();
}
Also used : HashMap(java.util.HashMap) SimpleName(com.github.javaparser.ast.expr.SimpleName) ArrayList(java.util.ArrayList) NameExpr(com.github.javaparser.ast.expr.NameExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Type(com.github.javaparser.ast.type.Type) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) MissingDependencyError(org.drools.compiler.compiler.MissingDependencyError) Expression(com.github.javaparser.ast.expr.Expression) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) HashSet(java.util.HashSet)

Aggregations

MissingDependencyError (org.drools.compiler.compiler.MissingDependencyError)2 StaticJavaParser.parseExpression (com.github.javaparser.StaticJavaParser.parseExpression)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 SimpleName (com.github.javaparser.ast.expr.SimpleName)1 Type (com.github.javaparser.ast.type.Type)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 MissingDependencyException (org.drools.core.common.MissingDependencyException)1 Declaration (org.drools.core.rule.Declaration)1 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)1 JavaAnalysisResult (org.drools.mvel.java.JavaAnalysisResult)1