Search in sources :

Example 41 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project drools by kiegroup.

the class Query method createFromExpr.

private MethodCallExpr createFromExpr(String variableName, Expression expr) {
    MethodCallExpr dslExpr = createDslTopLevelMethod(PATTERN_CALL);
    dslExpr.addArgument(context.getVarExpr(variableName));
    context.addExpression(dslExpr);
    MethodCallExpr fromExpr = createDslTopLevelMethod(FROM_CALL);
    LambdaExpr lambdaExpr = new LambdaExpr();
    lambdaExpr.setEnclosingParameters(true);
    TypedExpressionResult result = new ExpressionTyper(context).toTypedExpression(expr);
    for (String usedDeclration : result.getExpressionTyperContext().getUsedDeclarations()) {
        fromExpr.addArgument(context.getVarExpr(usedDeclration));
        lambdaExpr.addParameter(new Parameter(context.getDelarationType(usedDeclration), usedDeclration));
    }
    fromExpr.addArgument(lambdaExpr);
    if (result.getTypedExpression().isPresent()) {
        lambdaExpr.setBody(new ExpressionStmt(result.getTypedExpression().get().getExpression()));
    }
    return fromExpr;
}
Also used : LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) Parameter(com.github.javaparser.ast.body.Parameter) QueryParameter(org.drools.modelcompiler.builder.generator.QueryParameter) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 42 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project drools by kiegroup.

the class Consequence method executeCall.

private MethodCallExpr executeCall(BlockStmt ruleVariablesBlock, BlockStmt ruleConsequence, Collection<String> verifiedDeclUsedInRHS, MethodCallExpr onCall, Set<String> modifyProperties) {
    for (String modifiedProperty : modifyProperties) {
        NodeList<Expression> arguments = nodeList(new NameExpr(modifiedProperty));
        MethodCallExpr update = new MethodCallExpr(new NameExpr("drools"), "update", arguments);
        ruleConsequence.getStatements().add(new ExpressionStmt(update));
    }
    boolean requireDrools = rewriteRHS(ruleVariablesBlock, ruleConsequence);
    MethodCallExpr executeCall = new MethodCallExpr(onCall != null ? onCall : new NameExpr("D"), EXECUTE_CALL);
    LambdaExpr executeLambda = new LambdaExpr();
    executeCall.addArgument(executeLambda);
    executeLambda.setEnclosingParameters(true);
    if (requireDrools) {
        executeLambda.addParameter(new Parameter(toClassOrInterfaceType(org.drools.model.Drools.class), "drools"));
    }
    NodeList<Parameter> parameters = new BoxedParameters(context).getBoxedParametersWithUnboxedAssignment(verifiedDeclUsedInRHS, ruleConsequence);
    parameters.forEach(executeLambda::addParameter);
    executeLambda.setBody(ruleConsequence);
    return executeCall;
}
Also used : LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) Expression(com.github.javaparser.ast.expr.Expression) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) Parameter(com.github.javaparser.ast.body.Parameter) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 43 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project drools by kiegroup.

the class Expressions method namedUnaryLambda.

public static NamedLambda namedUnaryLambda(Expression expr, String text) {
    LambdaExpr lambda = Expressions.unaryLambda(expr);
    String name = Constants.unaryTestName(text);
    FieldDeclaration field = Constants.unaryTest(name, lambda);
    return new NamedLambda(new NameExpr(name), lambda, field);
}
Also used : LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration)

Example 44 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project drools by kiegroup.

the class Expressions method namedLambda.

public static NamedLambda namedLambda(Expression expr, String text) {
    LambdaExpr lambda = Expressions.lambda(expr);
    String name = Constants.functionName(text);
    FieldDeclaration field = Constants.function(name, lambda);
    return new NamedLambda(new NameExpr(name), lambda, field);
}
Also used : LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration)

Example 45 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project drools by kiegroup.

the class Functions method declaration.

public static DirectCompilerResult declaration(FunctionDefNode n, MethodCallExpr list, Expression fnBody) {
    LambdaExpr lambda = Expressions.lambda(fnBody);
    String fnName = Constants.functionName(n.getBody().getText());
    DirectCompilerResult r = DirectCompilerResult.of(Functions.internal(list, new NameExpr(fnName)), BuiltInType.FUNCTION);
    r.addFieldDeclaration(Constants.function(fnName, lambda));
    return r;
}
Also used : LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr)

Aggregations

LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)70 Parameter (com.github.javaparser.ast.body.Parameter)37 NameExpr (com.github.javaparser.ast.expr.NameExpr)30 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)29 Expression (com.github.javaparser.ast.expr.Expression)27 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)25 UnknownType (com.github.javaparser.ast.type.UnknownType)24 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)19 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)16 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)15 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)12 CompilationUnit (com.github.javaparser.ast.CompilationUnit)10 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)10 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)10 Test (org.junit.jupiter.api.Test)10 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)9 LongLiteralExpr (com.github.javaparser.ast.expr.LongLiteralExpr)9 CastExpr (com.github.javaparser.ast.expr.CastExpr)8 ArrayList (java.util.ArrayList)8 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)8