Search in sources :

Example 1 with CompiledBlockResult

use of org.drools.mvelcompiler.CompiledBlockResult in project drools by kiegroup.

the class AccumulateInline method parseReverseBlock.

private void parseReverseBlock(Set<String> externalDeclarations, Collection<String> allNamesInActionBlock) {
    String reverseCode = accumulateDescr.getReverseCode();
    CompiledBlockResult reverseBlockCompilationResult = mvelCompiler.compileStatement(addCurlyBracesToBlock(reverseCode));
    BlockStmt reverseBlock = reverseBlockCompilationResult.statementResults();
    if (reverseCode != null) {
        if (context.getRuleDialect() == RuleContext.RuleDialect.MVEL) {
            reverseCode = addSemicolonWhenMissing(reverseCode);
        } else if (nonEmptyBlockIsNotTerminated(reverseCode)) {
            throw new MissingSemicolonInlineAccumulateException(REVERSE);
        }
        Collection<String> allNamesInReverseBlock = collectNamesInBlock(reverseBlock, context);
        if (allNamesInReverseBlock.size() == 1) {
            MethodDeclaration reverseMethod = getMethodFromTemplateClass(REVERSE);
            reverseMethod.getParameter(1).setName(allNamesInReverseBlock.iterator().next());
            writeAccumulateMethod(contextFieldNames, reverseMethod, reverseBlock);
            MethodDeclaration supportsReverseMethod = getMethodFromTemplateClass("supportsReverse");
            supportsReverseMethod.getBody().orElseThrow(InvalidInlineTemplateException::new).addStatement(parseStatement("return true;"));
        } else {
            allNamesInActionBlock.removeIf(name -> !externalDeclarations.contains(name));
            usedExternalDeclarations.addAll(allNamesInActionBlock);
            throw new UnsupportedInlineAccumulate();
        }
    } else {
        MethodDeclaration supportsReverseMethod = getMethodFromTemplateClass("supportsReverse");
        supportsReverseMethod.getBody().orElseThrow(InvalidInlineTemplateException::new).addStatement(parseStatement("return false;"));
        MethodDeclaration reverseMethod = getMethodFromTemplateClass(REVERSE);
        reverseMethod.getBody().orElseThrow(InvalidInlineTemplateException::new).addStatement(parseStatement("throw new UnsupportedOperationException(\"This function does not support reverse.\");"));
    }
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) CompiledBlockResult(org.drools.mvelcompiler.CompiledBlockResult)

Example 2 with CompiledBlockResult

use of org.drools.mvelcompiler.CompiledBlockResult in project drools by kiegroup.

the class Consequence method createExecuteCallMvel.

private MethodCallExpr createExecuteCallMvel(String consequenceString, BlockStmt ruleVariablesBlock, Set<String> usedDeclarationInRHS, MethodCallExpr onCall) {
    String mvelBlock = addCurlyBracesToBlock(consequenceString);
    CompiledBlockResult compile;
    try {
        compile = DrlxParseUtil.createMvelCompiler(context).compileStatement(mvelBlock);
    } catch (MvelCompilerException e) {
        context.addCompilationError(new CompilationProblemErrorResult(new MvelCompilationError(e)));
        return null;
    }
    replaceKcontext(compile.statementResults());
    rewriteChannels(compile.statementResults());
    return executeCall(ruleVariablesBlock, compile.statementResults(), usedDeclarationInRHS, onCall, compile.getUsedBindings());
}
Also used : MvelCompilationError(org.drools.modelcompiler.builder.errors.MvelCompilationError) CompilationProblemErrorResult(org.drools.modelcompiler.builder.errors.CompilationProblemErrorResult) MvelCompilerException(org.drools.mvelcompiler.MvelCompilerException) CompiledBlockResult(org.drools.mvelcompiler.CompiledBlockResult)

Example 3 with CompiledBlockResult

use of org.drools.mvelcompiler.CompiledBlockResult in project drools by kiegroup.

the class AccumulateInline method parseInitBlock.

private void parseInitBlock() {
    MethodDeclaration initMethod = getMethodFromTemplateClass("init");
    String mvelBlock = addCurlyBracesToBlock(addSemicolon(accumulateDescr.getInitCode()));
    CompiledBlockResult initCodeCompilationResult = mvelCompiler.compileStatement(mvelBlock);
    BlockStmt initBlock = initCodeCompilationResult.statementResults();
    for (Statement stmt : initBlock.getStatements()) {
        final BlockStmt initMethodBody = initMethod.getBody().orElseThrow(InvalidInlineTemplateException::new);
        if (stmt.isExpressionStmt() && stmt.asExpressionStmt().getExpression().isVariableDeclarationExpr()) {
            VariableDeclarationExpr vdExpr = stmt.asExpressionStmt().getExpression().asVariableDeclarationExpr();
            for (VariableDeclarator vd : vdExpr.getVariables()) {
                final String variableName = vd.getNameAsString();
                contextFieldNames.add(variableName);
                contextData.addField(vd.getType(), variableName, Modifier.publicModifier().getKeyword());
                Optional<Expression> optInitializer = vd.getInitializer();
                optInitializer.ifPresent(initializer -> {
                    Expression target = new FieldAccessExpr(getDataNameExpr(), variableName);
                    Statement initStmt = new ExpressionStmt(new AssignExpr(target, initializer, AssignExpr.Operator.ASSIGN));
                    initMethodBody.addStatement(initStmt);
                    initStmt.findAll(NameExpr.class).stream().map(Node::toString).filter(context::hasDeclaration).forEach(usedExternalDeclarations::add);
                });
                accumulateDeclarations.add(new DeclarationSpec(variableName, DrlxParseUtil.getClassFromContext(context.getTypeResolver(), vd.getType().asString())));
            }
        }
    }
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Node(com.github.javaparser.ast.Node) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) Expression(com.github.javaparser.ast.expr.Expression) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) CompiledBlockResult(org.drools.mvelcompiler.CompiledBlockResult)

Example 4 with CompiledBlockResult

use of org.drools.mvelcompiler.CompiledBlockResult in project drools by kiegroup.

the class AccumulateInline method parseActionBlock.

private Collection<String> parseActionBlock(Set<String> externalDeclarations) {
    MethodDeclaration accumulateMethod = getMethodFromTemplateClass("accumulate");
    String actionCode = accumulateDescr.getActionCode();
    if (context.getRuleDialect() == RuleContext.RuleDialect.MVEL) {
        actionCode = addSemicolonWhenMissing(actionCode);
    } else if (nonEmptyBlockIsNotTerminated(actionCode)) {
        throw new MissingSemicolonInlineAccumulateException("action");
    }
    CompiledBlockResult actionBlockCompilationResult = mvelCompiler.compileStatement(addCurlyBracesToBlock(actionCode));
    BlockStmt actionBlock = actionBlockCompilationResult.statementResults();
    Collection<String> allNamesInActionBlock = collectNamesInBlock(actionBlock, context);
    if (allNamesInActionBlock.size() == 1) {
        String nameExpr = allNamesInActionBlock.iterator().next();
        accumulateMethod.getParameter(1).setName(nameExpr);
        singleAccumulateType = context.getDeclarationById(nameExpr).orElseThrow(() -> new IllegalStateException("Cannot find declaration by name " + nameExpr + "!")).getBoxedType();
        writeAccumulateMethod(contextFieldNames, accumulateMethod, actionBlock);
    } else {
        allNamesInActionBlock.removeIf(name -> !externalDeclarations.contains(name));
        usedExternalDeclarations.addAll(allNamesInActionBlock);
        throw new UnsupportedInlineAccumulate();
    }
    return allNamesInActionBlock;
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) CompiledBlockResult(org.drools.mvelcompiler.CompiledBlockResult)

Example 5 with CompiledBlockResult

use of org.drools.mvelcompiler.CompiledBlockResult in project drools by kiegroup.

the class AccumulateInline method parseAccumulatePattern.

void parseAccumulatePattern() {
    PatternDescr pattern = accumulateDescr.getInputPattern();
    if (pattern == null || pattern.getSource() == null) {
        return;
    }
    PatternSourceDescr sourceDescr = pattern.getSource();
    if (sourceDescr instanceof FromDescr) {
        DeclarativeInvokerDescr invokerDescr = ((FromDescr) sourceDescr).getDataSource();
        String mvelBlock = addCurlyBracesToBlock(addSemicolon(invokerDescr.getText()));
        CompiledBlockResult fromCodeCompilationResult = mvelCompiler.compileStatement(mvelBlock);
        BlockStmt fromBlock = fromCodeCompilationResult.statementResults();
        for (Statement stmt : fromBlock.getStatements()) {
            stmt.findAll(NameExpr.class).stream().map(Node::toString).filter(context::hasDeclaration).forEach(usedExternalDeclarations::add);
        }
    }
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) Statement(com.github.javaparser.ast.stmt.Statement) PatternSourceDescr(org.drools.drl.ast.descr.PatternSourceDescr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Node(com.github.javaparser.ast.Node) FromDescr(org.drools.drl.ast.descr.FromDescr) CompiledBlockResult(org.drools.mvelcompiler.CompiledBlockResult) DeclarativeInvokerDescr(org.drools.drl.ast.descr.DeclarativeInvokerDescr)

Aggregations

CompiledBlockResult (org.drools.mvelcompiler.CompiledBlockResult)6 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)4 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)3 StaticJavaParser.parseStatement (com.github.javaparser.StaticJavaParser.parseStatement)2 Node (com.github.javaparser.ast.Node)2 Statement (com.github.javaparser.ast.stmt.Statement)2 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)1 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)1 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)1 DeclarativeInvokerDescr (org.drools.drl.ast.descr.DeclarativeInvokerDescr)1 FromDescr (org.drools.drl.ast.descr.FromDescr)1 PatternDescr (org.drools.drl.ast.descr.PatternDescr)1 PatternSourceDescr (org.drools.drl.ast.descr.PatternSourceDescr)1 CompilationProblemErrorResult (org.drools.modelcompiler.builder.errors.CompilationProblemErrorResult)1 MvelCompilationError (org.drools.modelcompiler.builder.errors.MvelCompilationError)1 DeclarationSpec (org.drools.modelcompiler.builder.generator.DeclarationSpec)1 ModifyCompiler (org.drools.mvelcompiler.ModifyCompiler)1