Search in sources :

Example 36 with LambdaExpr

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

the class LambdaUtilTest method appendTwoMethodsToLambda.

@Test
public void appendTwoMethodsToLambda() {
    LambdaExpr l1 = parseExpression("(_this) -> _this.getDueDate()");
    LambdaExpr l2 = parseExpression("(_this) -> _this.getTime().getTime()");
    Expression expected = parseExpression("(_this) -> _this.getDueDate().getTime().getTime()");
    Expression actual = LambdaUtil.appendNewLambdaToOld(l1, l2);
    assertEquals(expected.toString(), actual.toString());
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) Test(org.junit.Test)

Example 37 with LambdaExpr

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

the class LambdaUtilTest method appendaLambdaToOld.

@Test
public void appendaLambdaToOld() {
    LambdaExpr l1 = parseExpression("(_this) -> _this.getTimeFieldAsDate()");
    LambdaExpr l2 = parseExpression("(_this) -> _this.getTime()");
    Expression expected = parseExpression("(_this) -> _this.getTimeFieldAsDate().getTime()");
    Expression actual = LambdaUtil.appendNewLambdaToOld(l1, l2);
    assertEquals(expected.toString(), actual.toString());
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) Test(org.junit.Test)

Example 38 with LambdaExpr

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

the class FromVisitor method fromConstructorExpr.

private Optional<Expression> fromConstructorExpr(String expression, ObjectCreationExpr parsedExpression) {
    MethodCallExpr fromCall = createDslTopLevelMethod(FROM_CALL);
    List<String> bindingIds = new ArrayList<>();
    for (Expression argument : parsedExpression.getArguments()) {
        final String argumentName = PrintUtil.printNode(argument);
        if (contextHasDeclaration(argumentName)) {
            bindingIds.add(argumentName);
            fromCall.addArgument(context.getVarExpr(argumentName));
        }
    }
    Expression newExpr = generateLambdaWithoutParameters(bindingIds, parsedExpression, true, Optional.empty(), context);
    if (newExpr instanceof LambdaExpr) {
        context.getPackageModel().registerLambdaReturnType((LambdaExpr) newExpr, DrlxParseUtil.getClassFromType(context.getTypeResolver(), parsedExpression.getType()));
    }
    fromCall.addArgument(newExpr);
    return of(fromCall);
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) ArrayList(java.util.ArrayList) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 39 with LambdaExpr

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

the class AccumulateVisitor method composeTwoBindings.

private void composeTwoBindings(String binding, MethodCallExpr newBindingExpression) {
    context.findBindingExpression(binding).ifPresent(oldBind -> {
        // compose newComposedBinding using oldBind and newBindingExpression. But still keep oldBind.
        LambdaExpr oldBindLambda = oldBind.findFirst(LambdaExpr.class).orElseThrow(RuntimeException::new);
        LambdaExpr newBindLambda = newBindingExpression.findFirst(LambdaExpr.class).orElseThrow(RuntimeException::new);
        LambdaExpr tmpOldBindLambda = oldBindLambda.clone();
        Expression newComposedLambda = LambdaUtil.appendNewLambdaToOld(tmpOldBindLambda, newBindLambda);
        MethodCallExpr newComposedBinding = new MethodCallExpr(BIND_CALL, newBindingExpression.getArgument(0), newComposedLambda);
        newComposedBinding.setScope(oldBind.getScope().orElseThrow(RuntimeException::new));
        Optional<MethodCallExpr> optReactOn = oldBind.getArguments().stream().filter(MethodCallExpr.class::isInstance).map(MethodCallExpr.class::cast).filter(exp -> exp.getName().asString().equals(REACT_ON_CALL)).findFirst();
        if (optReactOn.isPresent()) {
            newComposedBinding.addArgument(optReactOn.get().clone());
        }
        // insert newComposedBinding at the first in the chain
        oldBind.setScope(newComposedBinding);
    });
}
Also used : REACT_ON_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.REACT_ON_CALL) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) ACCUMULATE_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.ACCUMULATE_CALL) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) PatternDescr(org.drools.drl.ast.descr.PatternDescr) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) ExpressionTyperContext(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyperContext) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) AccumulateUtil(org.drools.compiler.rule.builder.util.AccumulateUtil) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) Expression(com.github.javaparser.ast.expr.Expression) ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) PatternExpressionBuilder(org.drools.modelcompiler.builder.generator.expression.PatternExpressionBuilder) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) Node(com.github.javaparser.ast.Node) PackageModel(org.drools.modelcompiler.builder.PackageModel) NodeList(com.github.javaparser.ast.NodeList) AND_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.AND_CALL) Collection(java.util.Collection) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Set(java.util.Set) LambdaUtil(org.drools.modelcompiler.util.LambdaUtil) BaseDescr(org.drools.drl.ast.descr.BaseDescr) VALUE_OF_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.VALUE_OF_CALL) Objects(java.util.Objects) List(java.util.List) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) Optional(java.util.Optional) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) DrlxParseFail(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseFail) DrlxParseUtil.validateDuplicateBindings(org.drools.modelcompiler.builder.generator.DrlxParseUtil.validateDuplicateBindings) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) AbstractExpressionBuilder(org.drools.modelcompiler.builder.generator.expression.AbstractExpressionBuilder) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) PrintUtil.printNode(org.drools.mvel.parser.printer.PrintUtil.printNode) Parameter(com.github.javaparser.ast.body.Parameter) Pattern(org.drools.core.rule.Pattern) ArrayList(java.util.ArrayList) ACC_FUNCTION_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.ACC_FUNCTION_CALL) HashSet(java.util.HashSet) CollectListAccumulateFunction(org.drools.core.base.accumulators.CollectListAccumulateFunction) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) DrlxParseUtil.getLiteralExpressionType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getLiteralExpressionType) DslMethodNames.createDslTopLevelMethod(org.drools.modelcompiler.builder.generator.DslMethodNames.createDslTopLevelMethod) AndDescr(org.drools.drl.ast.descr.AndDescr) ReplaceTypeInLambda.replaceTypeInExprLambda(org.drools.modelcompiler.util.lambdareplace.ReplaceTypeInLambda.replaceTypeInExprLambda) BIND_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.BIND_CALL) Optional.ofNullable(java.util.Optional.ofNullable) BIND_AS_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.BIND_AS_CALL) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) Collectors.toList(java.util.stream.Collectors.toList) CollectAccumulator(org.drools.core.base.accumulators.CollectAccumulator) CollectSetAccumulateFunction(org.drools.core.base.accumulators.CollectSetAccumulateFunction) ParseResultVisitor(org.drools.modelcompiler.builder.generator.drlxparse.ParseResultVisitor) Collections(java.util.Collections) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 40 with LambdaExpr

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

the class FromVisitor method createSupplier.

private Expression createSupplier(Expression parsedExpression) {
    final LambdaExpr lambdaExpr = new LambdaExpr(NodeList.nodeList(), new ExpressionStmt(parsedExpression), true);
    MethodCallExpr fromCall = createDslTopLevelMethod(FROM_CALL);
    fromCall.addArgument(lambdaExpr);
    return fromCall;
}
Also used : LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

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