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.\");"));
}
}
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());
}
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())));
}
}
}
}
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;
}
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);
}
}
}
Aggregations