use of org.drools.javaparser.ast.body.Parameter in project drools by kiegroup.
the class Consequence method executeCall.
private MethodCallExpr executeCall(BlockStmt ruleVariablesBlock, BlockStmt ruleConsequence, Collection<String> verifiedDeclUsedInRHS, MethodCallExpr onCall) {
boolean requireDrools = rewriteRHS(ruleVariablesBlock, ruleConsequence);
MethodCallExpr executeCall = new MethodCallExpr(onCall, EXECUTE_CALL);
LambdaExpr executeLambda = new LambdaExpr();
executeCall.addArgument(executeLambda);
executeLambda.setEnclosingParameters(true);
if (requireDrools) {
executeLambda.addParameter(new Parameter(new UnknownType(), "drools"));
}
verifiedDeclUsedInRHS.stream().map(x -> new Parameter(new UnknownType(), x)).forEach(executeLambda::addParameter);
executeLambda.setBody(ruleConsequence);
return executeCall;
}
use of org.drools.javaparser.ast.body.Parameter in project drools by kiegroup.
the class AccumulateVisitorFlowDSL method replaceBindingWithPatternBinding.
private void replaceBindingWithPatternBinding(MethodCallExpr bindExpression, MethodCallExpr lastPattern) {
if (lastPattern == null) {
return;
}
final Expression bindingId = lastPattern.getArgument(1);
bindExpression.findFirst(NameExpr.class, e -> e.equals(bindingId)).ifPresent(name -> {
LambdaExpr lambda = (LambdaExpr) bindExpression.getArgument(bindExpression.getArguments().size() - 1);
if (lambda.getParameters().size() > 1) {
String formalArg = fromVar(name.getNameAsString());
for (Parameter param : lambda.getParameters()) {
if (param.getNameAsString().equals(formalArg)) {
lambda.getParameters().remove(param);
lambda.getParameters().add(0, param);
break;
}
}
}
bindExpression.getArguments().remove(name);
bindExpression.getArguments().add(0, name);
});
}
use of org.drools.javaparser.ast.body.Parameter in project drools by kiegroup.
the class AccumulateVisitorPatternDSL method replaceBindingWithPatternBinding.
private MethodCallExpr replaceBindingWithPatternBinding(MethodCallExpr bindExpression, MethodCallExpr lastPattern) {
final Expression bindingId = lastPattern.getArgument(0);
bindExpression.findFirst(NameExpr.class, e -> e.equals(bindingId)).ifPresent(name -> {
bindExpression.remove(name);
LambdaExpr lambda = (LambdaExpr) bindExpression.getArgument(bindExpression.getArguments().size() - 1);
if (lambda.getParameters().size() > 1) {
String formalArg = fromVar(name.getNameAsString());
for (Parameter param : lambda.getParameters()) {
if (param.getNameAsString().equals(formalArg)) {
lambda.getParameters().remove(param);
lambda.getParameters().add(0, param);
break;
}
}
}
});
return (MethodCallExpr) bindExpression;
}
use of org.drools.javaparser.ast.body.Parameter in project drools by kiegroup.
the class DrlxParseUtil method generateLambdaWithoutParameters.
public static Expression generateLambdaWithoutParameters(Collection<String> usedDeclarations, Expression expr, boolean skipFirstParamAsThis) {
if (skipFirstParamAsThis && usedDeclarations.isEmpty()) {
return expr;
}
LambdaExpr lambdaExpr = new LambdaExpr();
lambdaExpr.setEnclosingParameters(true);
if (!skipFirstParamAsThis) {
lambdaExpr.addParameter(new Parameter(new UnknownType(), "_this"));
}
usedDeclarations.stream().map(s -> new Parameter(new UnknownType(), s)).forEach(lambdaExpr::addParameter);
lambdaExpr.setBody(new ExpressionStmt(expr));
return lambdaExpr;
}
use of org.drools.javaparser.ast.body.Parameter in project drools by kiegroup.
the class FlowExpressionBuilder method buildIndexedBy.
private MethodCallExpr buildIndexedBy(DrlxParseSuccess drlxParseResult, MethodCallExpr exprDSL) {
if (!hasIndex(drlxParseResult)) {
return exprDSL;
}
TypedExpression left = drlxParseResult.getLeft();
TypedExpression right = drlxParseResult.getRight();
Class<?> indexType = Stream.of(left, right).map(TypedExpression::getType).filter(Objects::nonNull).findFirst().get();
ClassExpr indexedBy_indexedClass = new ClassExpr(JavaParser.parseType(indexType.getCanonicalName()));
// not 100% accurate as the type in "nameExpr" is actually parsed if it was JavaParsers as a big chain of FieldAccessExpr
FieldAccessExpr indexedBy_constraintType = new FieldAccessExpr(new NameExpr("org.drools.model.Index.ConstraintType"), drlxParseResult.getDecodeConstraintType().toString());
LambdaExpr indexedBy_leftOperandExtractor = new LambdaExpr();
indexedBy_leftOperandExtractor.addParameter(new Parameter(new UnknownType(), "_this"));
boolean leftContainsThis = left.getExpression().toString().contains("_this");
indexedBy_leftOperandExtractor.setBody(new ExpressionStmt(leftContainsThis ? left.getExpression() : right.getExpression()));
MethodCallExpr indexedByDSL = new MethodCallExpr(exprDSL, INDEXED_BY_CALL);
indexedByDSL.addArgument(indexedBy_indexedClass);
indexedByDSL.addArgument(indexedBy_constraintType);
indexedByDSL.addArgument("" + indexIdGenerator.getFieldId(drlxParseResult.getPatternType(), left.getFieldName()));
indexedByDSL.addArgument(indexedBy_leftOperandExtractor);
Collection<String> usedDeclarations = drlxParseResult.getUsedDeclarations();
if (isAlphaIndex(usedDeclarations)) {
indexedByDSL.addArgument(right.getExpression());
} else {
LambdaExpr indexedBy_rightOperandExtractor = new LambdaExpr();
indexedBy_rightOperandExtractor.addParameter(new Parameter(new UnknownType(), usedDeclarations.iterator().next()));
indexedBy_rightOperandExtractor.setBody(new ExpressionStmt(!leftContainsThis ? left.getExpression() : right.getExpression()));
indexedByDSL.addArgument(indexedBy_rightOperandExtractor);
}
return indexedByDSL;
}
Aggregations