Search in sources :

Example 1 with LambdaExpr

use of org.drools.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) {
    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;
}
Also used : UnknownType(org.drools.javaparser.ast.type.UnknownType) BitMask(org.drools.model.BitMask) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Expression(org.drools.javaparser.ast.expr.Expression) DrlxParseUtil.isNameExprWithName(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isNameExprWithName) DroolsImpl(org.drools.modelcompiler.consequence.DroolsImpl) JavaParser(org.drools.javaparser.JavaParser) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) BREAKING_CALL(org.drools.modelcompiler.builder.generator.visitor.NamedConsequenceVisitor.BREAKING_CALL) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) JavaParser.parseExpression(org.drools.javaparser.JavaParser.parseExpression) Statement(org.drools.javaparser.ast.stmt.Statement) Collectors.toSet(java.util.stream.Collectors.toSet) PackageModel(org.drools.modelcompiler.builder.PackageModel) ObjectCreationExpr(org.drools.javaparser.ast.expr.ObjectCreationExpr) DrlxParseUtil.hasScope(org.drools.modelcompiler.builder.generator.DrlxParseUtil.hasScope) Parameter(org.drools.javaparser.ast.body.Parameter) ClassUtils(org.drools.core.util.ClassUtils) Collection(java.util.Collection) DrlxParseUtil.parseBlock(org.drools.modelcompiler.builder.generator.DrlxParseUtil.parseBlock) Set(java.util.Set) EmptyStmt(org.drools.javaparser.ast.stmt.EmptyStmt) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) Objects(java.util.Objects) VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) UnknownType(org.drools.javaparser.ast.type.UnknownType) DrlxParseUtil.findAllChildrenRecursive(org.drools.modelcompiler.builder.generator.DrlxParseUtil.findAllChildrenRecursive) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Modifier(org.drools.javaparser.ast.Modifier) SimpleName(org.drools.javaparser.ast.expr.SimpleName) StringUtils(org.drools.core.util.StringUtils) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) Parameter(org.drools.javaparser.ast.body.Parameter) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 2 with LambdaExpr

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

the class AccumulateVisitor method buildConstraintExpression.

protected Expression buildConstraintExpression(Expression expr, Collection<String> usedDeclarations) {
    LambdaExpr lambdaExpr = new LambdaExpr();
    lambdaExpr.setEnclosingParameters(true);
    usedDeclarations.stream().map(s -> new Parameter(new UnknownType(), s)).forEach(lambdaExpr::addParameter);
    lambdaExpr.setBody(new ExpressionStmt(expr));
    return lambdaExpr;
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) JavaParser(org.drools.javaparser.JavaParser) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) DrlxParseUtil.toType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toType) AccumulateUtil(org.drools.compiler.rule.builder.util.AccumulateUtil) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) DrlxParseUtil.forceCastForName(org.drools.modelcompiler.builder.generator.DrlxParseUtil.forceCastForName) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt) DrlxParseUtil.toVar(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toVar) ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) Statement(org.drools.javaparser.ast.stmt.Statement) PackageModel(org.drools.modelcompiler.builder.PackageModel) Parameter(org.drools.javaparser.ast.body.Parameter) Collection(java.util.Collection) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) UnknownType(org.drools.javaparser.ast.type.UnknownType) List(java.util.List) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) Optional(java.util.Optional) DrlxParseFail(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseFail) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) AbstractExpressionBuilder(org.drools.modelcompiler.builder.generator.expression.AbstractExpressionBuilder) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) DrlxParseUtil.rescopeNamesToNewScope(org.drools.modelcompiler.builder.generator.DrlxParseUtil.rescopeNamesToNewScope) ReturnStmt(org.drools.javaparser.ast.stmt.ReturnStmt) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) ArrayList(java.util.ArrayList) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) IoUtils(org.drools.core.util.IoUtils) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) StringUtil(org.drools.modelcompiler.util.StringUtil) MethodDeclaration(org.drools.javaparser.ast.body.MethodDeclaration) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) ClassOrInterfaceDeclaration(org.drools.javaparser.ast.body.ClassOrInterfaceDeclaration) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) VariableDeclarator(org.drools.javaparser.ast.body.VariableDeclarator) IOException(java.io.IOException) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) CompilationUnit(org.drools.javaparser.ast.CompilationUnit) Type(org.drools.javaparser.ast.type.Type) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) Collectors.toList(java.util.stream.Collectors.toList) ParseResultVisitor(org.drools.modelcompiler.builder.generator.drlxparse.ParseResultVisitor) Modifier(org.drools.javaparser.ast.Modifier) UnknownType(org.drools.javaparser.ast.type.UnknownType) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) Parameter(org.drools.javaparser.ast.body.Parameter) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt)

Example 3 with LambdaExpr

use of org.drools.javaparser.ast.expr.LambdaExpr 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);
    });
}
Also used : PackageModel(org.drools.modelcompiler.builder.PackageModel) Parameter(org.drools.javaparser.ast.body.Parameter) Expression(org.drools.javaparser.ast.expr.Expression) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) BIND_AS_CALL(org.drools.modelcompiler.builder.generator.ModelGenerator.BIND_AS_CALL) FlowExpressionBuilder(org.drools.modelcompiler.builder.generator.expression.FlowExpressionBuilder) Collection(java.util.Collection) BIND_CALL(org.drools.modelcompiler.builder.generator.expression.FlowExpressionBuilder.BIND_CALL) Collectors(java.util.stream.Collectors) NameExpr(org.drools.javaparser.ast.expr.NameExpr) DrlxParseUtil.fromVar(org.drools.modelcompiler.builder.generator.DrlxParseUtil.fromVar) ArrayList(java.util.ArrayList) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) List(java.util.List) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) DrlxParseUtil.toVar(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toVar) Optional(java.util.Optional) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) Expression(org.drools.javaparser.ast.expr.Expression) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) Parameter(org.drools.javaparser.ast.body.Parameter)

Example 4 with LambdaExpr

use of org.drools.javaparser.ast.expr.LambdaExpr 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;
}
Also used : PackageModel(org.drools.modelcompiler.builder.PackageModel) Parameter(org.drools.javaparser.ast.body.Parameter) Expression(org.drools.javaparser.ast.expr.Expression) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) Collection(java.util.Collection) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) BIND_CALL(org.drools.modelcompiler.builder.generator.expression.FlowExpressionBuilder.BIND_CALL) NameExpr(org.drools.javaparser.ast.expr.NameExpr) DrlxParseUtil.fromVar(org.drools.modelcompiler.builder.generator.DrlxParseUtil.fromVar) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) List(java.util.List) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) DrlxParseUtil.toVar(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toVar) Optional(java.util.Optional) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) PatternExpressionBuilder(org.drools.modelcompiler.builder.generator.expression.PatternExpressionBuilder) Node(org.drools.javaparser.ast.Node) Expression(org.drools.javaparser.ast.expr.Expression) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) Parameter(org.drools.javaparser.ast.body.Parameter) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 5 with LambdaExpr

use of org.drools.javaparser.ast.expr.LambdaExpr 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;
}
Also used : UnknownType(org.drools.javaparser.ast.type.UnknownType) Expression(org.drools.javaparser.ast.expr.Expression) LiteralExpr(org.drools.javaparser.ast.expr.LiteralExpr) Optional.of(java.util.Optional.of) JavaParser(org.drools.javaparser.JavaParser) Operator(org.drools.javaparser.ast.expr.BinaryExpr.Operator) ExpressionTyper.findLeftLeafOfNameExpr(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper.findLeftLeafOfNameExpr) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) ConstraintType(org.drools.core.util.index.IndexUtil.ConstraintType) ClassUtil(org.drools.modelcompiler.util.ClassUtil) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt) PrimitiveType(org.drools.javaparser.ast.type.PrimitiveType) Method(java.lang.reflect.Method) LongLiteralExpr(org.drools.javaparser.ast.expr.LongLiteralExpr) ArrayCreationExpr(org.drools.javaparser.ast.expr.ArrayCreationExpr) Parameter(org.drools.javaparser.ast.body.Parameter) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) NameExpr(org.drools.javaparser.ast.expr.NameExpr) UnknownType(org.drools.javaparser.ast.type.UnknownType) IndexUtil(org.drools.core.util.index.IndexUtil) UnaryExpr(org.drools.javaparser.ast.expr.UnaryExpr) List(java.util.List) Stream(java.util.stream.Stream) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) NodeWithSimpleName(org.drools.javaparser.ast.nodeTypes.NodeWithSimpleName) ClassUtil.findMethod(org.drools.modelcompiler.util.ClassUtil.findMethod) StringUtils(org.drools.core.util.StringUtils) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) ArrayAccessExpr(org.drools.javaparser.ast.expr.ArrayAccessExpr) NodeWithTraversableScope(org.drools.javaparser.ast.nodeTypes.NodeWithTraversableScope) CastExpr(org.drools.javaparser.ast.expr.CastExpr) CharLiteralExpr(org.drools.javaparser.ast.expr.CharLiteralExpr) Deque(java.util.Deque) NullLiteralExpr(org.drools.javaparser.ast.expr.NullLiteralExpr) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) ArrayList(java.util.ArrayList) DrlxParser(org.drools.drlx.DrlxParser) HalfBinaryExpr(org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr) BooleanLiteralExpr(org.drools.javaparser.ast.expr.BooleanLiteralExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) LiteralStringValueExpr(org.drools.javaparser.ast.expr.LiteralStringValueExpr) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) LinkedList(java.util.LinkedList) ObjectCreationExpr(org.drools.javaparser.ast.expr.ObjectCreationExpr) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) IntegerLiteralExpr(org.drools.javaparser.ast.expr.IntegerLiteralExpr) ClassUtils(org.drools.core.util.ClassUtils) DrlxExpression(org.drools.javaparser.ast.drlx.expr.DrlxExpression) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) Field(java.lang.reflect.Field) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) Type(org.drools.javaparser.ast.type.Type) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) DoubleLiteralExpr(org.drools.javaparser.ast.expr.DoubleLiteralExpr) NodeWithOptionalScope(org.drools.javaparser.ast.nodeTypes.NodeWithOptionalScope) Collections(java.util.Collections) Node(org.drools.javaparser.ast.Node) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) Parameter(org.drools.javaparser.ast.body.Parameter) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt)

Aggregations

Parameter (org.drools.javaparser.ast.body.Parameter)8 LambdaExpr (org.drools.javaparser.ast.expr.LambdaExpr)8 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)8 NameExpr (org.drools.javaparser.ast.expr.NameExpr)8 Collection (java.util.Collection)6 List (java.util.List)6 Expression (org.drools.javaparser.ast.expr.Expression)6 UnknownType (org.drools.javaparser.ast.type.UnknownType)6 ArrayList (java.util.ArrayList)5 Optional (java.util.Optional)5 ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)5 Objects (java.util.Objects)4 JavaParser (org.drools.javaparser.JavaParser)4 FieldAccessExpr (org.drools.javaparser.ast.expr.FieldAccessExpr)4 ExpressionStmt (org.drools.javaparser.ast.stmt.ExpressionStmt)4 PackageModel (org.drools.modelcompiler.builder.PackageModel)4 IndexUtil (org.drools.core.util.index.IndexUtil)3 AssignExpr (org.drools.javaparser.ast.expr.AssignExpr)3 DrlxParseUtil.toVar (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toVar)3 RuleContext (org.drools.modelcompiler.builder.generator.RuleContext)3