Search in sources :

Example 1 with NameExpr

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

the class DrlxParseUtil method rescopeNamesToNewScope.

/**
 * Mutates expression
 * such that, if it contains a NameExpr for any of the <code>names</code>,
 * it is replaced with a FieldAccessExpr having <code>newScope</code> as the scope.
 */
public static void rescopeNamesToNewScope(Expression newScope, List<String> names, Expression e) {
    if (e instanceof AssignExpr) {
        AssignExpr assignExpr = (AssignExpr) e;
        rescopeNamesToNewScope(newScope, names, assignExpr.getTarget());
        rescopeNamesToNewScope(newScope, names, assignExpr.getValue());
    } else if (e instanceof BinaryExpr) {
        rescopeNamesToNewScope(newScope, names, ((BinaryExpr) e).getLeft());
        rescopeNamesToNewScope(newScope, names, ((BinaryExpr) e).getRight());
    } else if (e instanceof UnaryExpr) {
        rescopeNamesToNewScope(newScope, names, ((UnaryExpr) e).getExpression());
    } else if (e instanceof EnclosedExpr) {
        rescopeNamesToNewScope(newScope, names, ((EnclosedExpr) e).getInner());
    } else {
        Optional<Expression> rootNode = DrlxParseUtil.findRootNodeViaScope(e);
        if (rootNode.isPresent() && rootNode.get() instanceof NameExpr) {
            NameExpr nameExpr = (NameExpr) rootNode.get();
            if (names.contains(nameExpr.getNameAsString())) {
                Expression prepend = new FieldAccessExpr(newScope, nameExpr.getNameAsString());
                if (e instanceof NameExpr) {
                    // actually `e` was not composite, it was already the NameExpr node I was looking to replace.
                    e.getParentNode().get().replace(nameExpr, prepend);
                } else {
                    e.replace(nameExpr, prepend);
                }
            }
        }
    }
}
Also used : Optional(java.util.Optional) Expression(org.drools.javaparser.ast.expr.Expression) DrlxExpression(org.drools.javaparser.ast.drlx.expr.DrlxExpression) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr) ExpressionTyper.findLeftLeafOfNameExpr(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper.findLeftLeafOfNameExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) UnaryExpr(org.drools.javaparser.ast.expr.UnaryExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr)

Example 2 with NameExpr

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

the class DrlxParseUtil method nameExprToMethodCallExpr.

public static TypedExpression nameExprToMethodCallExpr(String name, Class<?> clazz, Expression scope) {
    Method accessor = ClassUtils.getAccessor(clazz, name);
    if (accessor != null) {
        MethodCallExpr body = new MethodCallExpr(scope, accessor.getName());
        return new TypedExpression(body, accessor.getReturnType());
    }
    if (clazz.isArray() && name.equals("length")) {
        FieldAccessExpr expr = new FieldAccessExpr(scope, name);
        return new TypedExpression(expr, int.class);
    }
    try {
        Field field = clazz.getField(name);
        if (scope == null) {
            if (Modifier.isStatic(field.getModifiers())) {
                scope = new NameExpr(clazz.getCanonicalName());
            } else {
                throw new IllegalArgumentException("Unknown field " + name + " on " + clazz);
            }
        }
        FieldAccessExpr expr = new FieldAccessExpr(scope, name);
        return new TypedExpression(expr, field.getType());
    } catch (NoSuchFieldException e) {
        throw new IllegalArgumentException("Unknown field " + name + " on " + clazz);
    }
}
Also used : Field(java.lang.reflect.Field) ExpressionTyper.findLeftLeafOfNameExpr(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper.findLeftLeafOfNameExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) Method(java.lang.reflect.Method) ClassUtil.findMethod(org.drools.modelcompiler.util.ClassUtil.findMethod) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 3 with NameExpr

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

the class ConstraintParser method getDrlxParseResult.

private DrlxParseResult getDrlxParseResult(Class<?> patternType, String bindingId, String expression, DrlxExpression drlx, boolean isPositional) {
    Expression drlxExpr = drlx.getExpr();
    boolean isEnclosed = false;
    while (drlxExpr instanceof EnclosedExpr) {
        drlxExpr = ((EnclosedExpr) drlxExpr).getInner();
        isEnclosed = true;
    }
    if (drlxExpr instanceof MethodCallExpr && !((MethodCallExpr) drlxExpr).getScope().isPresent() && ((MethodCallExpr) drlxExpr).getNameAsString().equals("eval")) {
        drlxExpr = ((MethodCallExpr) drlxExpr).getArgument(0);
    }
    String exprId;
    if (GENERATE_EXPR_ID) {
        exprId = context.getExprId(patternType, expression);
    }
    if (drlxExpr instanceof BinaryExpr) {
        BinaryExpr binaryExpr = (BinaryExpr) drlxExpr;
        BinaryExpr.Operator operator = binaryExpr.getOperator();
        IndexUtil.ConstraintType decodeConstraintType = DrlxParseUtil.toConstraintType(operator);
        final ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
        final ExpressionTyper expressionTyper = new ExpressionTyper(context, patternType, bindingId, isPositional, expressionTyperContext);
        TypedExpressionResult leftTypedExpressionResult = expressionTyper.toTypedExpression(binaryExpr.getLeft());
        Optional<TypedExpression> optLeft = leftTypedExpressionResult.getTypedExpression();
        if (!optLeft.isPresent()) {
            return new DrlxParseFail();
        }
        List<String> usedDeclarationsOnLeft = drlx.getBind() == null ? null : new ArrayList<>(expressionTyperContext.getUsedDeclarations());
        TypedExpressionResult rightExpressionResult = expressionTyper.toTypedExpression(binaryExpr.getRight());
        Optional<TypedExpression> optRight = rightExpressionResult.getTypedExpression();
        if (!optRight.isPresent()) {
            context.addCompilationError(new ParseExpressionErrorResult(drlxExpr));
            return new DrlxParseFail();
        }
        TypedExpression left = optLeft.get();
        TypedExpression right = optRight.get();
        Expression combo;
        if (left.isPrimitive()) {
            if (!right.getType().isPrimitive() && !Number.class.isAssignableFrom(right.getType()) && !Boolean.class.isAssignableFrom(right.getType()) && !String.class.isAssignableFrom(right.getType())) {
                context.addCompilationError(new InvalidExpressionErrorResult("Comparison operation requires compatible types. Found " + left.getType() + " and " + right.getType()));
                return new DrlxParseFail();
            }
            if (right.getExpression() instanceof StringLiteralExpr) {
                right.setExpression(new IntegerLiteralExpr(((StringLiteralExpr) right.getExpression()).asString()));
            } else if (right.getExpression() instanceof LiteralStringValueExpr) {
                right.setExpression(coerceLiteralExprToType((LiteralStringValueExpr) right.getExpression(), left.getType()));
            }
            combo = new BinaryExpr(left.getExpression(), right.getExpression(), operator);
        } else {
            coerceRightExpression(left, right);
            switch(operator) {
                case EQUALS:
                case NOT_EQUALS:
                    combo = getEqualityExpression(left, right, operator);
                    break;
                default:
                    if (left.getExpression() == null || right.getExpression() == null) {
                        context.addCompilationError(new ParseExpressionErrorResult(drlxExpr));
                        return new DrlxParseFail();
                    }
                    combo = handleSpecialComparisonCases(operator, left, right);
            }
        }
        for (Expression e : leftTypedExpressionResult.getPrefixExpressions()) {
            combo = new BinaryExpr(e, combo, BinaryExpr.Operator.AND);
        }
        boolean isBetaNode = false;
        if (right.getExpression() instanceof BinaryExpr) {
            if (((BinaryExpr) right.getExpression()).getRight() instanceof MethodCallExpr) {
                isBetaNode = true;
            }
        } else if (right.getExpression() instanceof NameExpr) {
            isBetaNode = true;
        }
        if (isEnclosed) {
            combo = new EnclosedExpr(combo);
        }
        return new DrlxParseSuccess(patternType, exprId, bindingId, combo, left.getType()).setDecodeConstraintType(decodeConstraintType).setUsedDeclarations(expressionTyperContext.getUsedDeclarations()).setUsedDeclarationsOnLeft(usedDeclarationsOnLeft).setReactOnProperties(expressionTyperContext.getReactOnProperties()).setLeft(left).setRight(right).setBetaNode(isBetaNode);
    }
    if (drlxExpr instanceof UnaryExpr) {
        UnaryExpr unaryExpr = (UnaryExpr) drlxExpr;
        TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, patternType, bindingId, isPositional).toTypedExpression(unaryExpr);
        return typedExpressionResult.getTypedExpression().<DrlxParseResult>map(left -> {
            return new DrlxParseSuccess(patternType, exprId, bindingId, left.getExpression(), left.getType()).setUsedDeclarations(typedExpressionResult.getUsedDeclarations()).setReactOnProperties(typedExpressionResult.getReactOnProperties()).setLeft(left);
        }).orElse(new DrlxParseFail());
    }
    if (drlxExpr instanceof PointFreeExpr) {
        PointFreeExpr pointFreeExpr = (PointFreeExpr) drlxExpr;
        TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, patternType, bindingId, isPositional).toTypedExpression(pointFreeExpr);
        final Optional<TypedExpression> optTypedExpression = typedExpressionResult.getTypedExpression();
        return optTypedExpression.<DrlxParseResult>map(typedExpression -> {
            final Expression returnExpression = typedExpression.getExpression();
            final Class<?> returnType = typedExpression.getType();
            return new DrlxParseSuccess(patternType, exprId, bindingId, returnExpression, returnType).setUsedDeclarations(typedExpressionResult.getUsedDeclarations()).setReactOnProperties(typedExpressionResult.getReactOnProperties()).setLeft(typedExpression.getLeft()).setStatic(typedExpression.isStatic()).setValidExpression(true);
        }).orElse(new DrlxParseFail());
    }
    if (drlxExpr instanceof MethodCallExpr) {
        MethodCallExpr methodCallExpr = (MethodCallExpr) drlxExpr;
        // when the methodCallExpr will be placed in the model/DSL, any parameter being a "this" need to be implemented as _this by convention.
        List<ThisExpr> rewriteThisExprs = recurseCollectArguments(methodCallExpr).stream().filter(ThisExpr.class::isInstance).map(ThisExpr.class::cast).collect(Collectors.toList());
        for (ThisExpr t : rewriteThisExprs) {
            methodCallExpr.replace(t, new NameExpr("_this"));
        }
        Optional<MethodDeclaration> functionCall = packageModel.getFunctions().stream().filter(m -> m.getName().equals(methodCallExpr.getName())).findFirst();
        if (functionCall.isPresent()) {
            Class<?> returnType = DrlxParseUtil.getClassFromContext(context.getTypeResolver(), functionCall.get().getType().asString());
            NodeList<Expression> arguments = methodCallExpr.getArguments();
            List<String> usedDeclarations = new ArrayList<>();
            for (Expression arg : arguments) {
                if (arg instanceof NameExpr && !arg.toString().equals("_this")) {
                    usedDeclarations.add(arg.toString());
                } else if (arg instanceof MethodCallExpr) {
                    TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, null, bindingId, isPositional).toTypedExpression(arg);
                    usedDeclarations.addAll(typedExpressionResult.getUsedDeclarations());
                }
            }
            return new DrlxParseSuccess(patternType, exprId, bindingId, methodCallExpr, returnType).setUsedDeclarations(usedDeclarations);
        } else if (methodCallExpr.getScope().isPresent() && methodCallExpr.getScope().get() instanceof StringLiteralExpr) {
            TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, String.class, bindingId, isPositional).toTypedExpression(methodCallExpr);
            Optional<TypedExpression> optConverted = typedExpressionResult.getTypedExpression();
            return optConverted.<DrlxParseResult>map(converted -> {
                return new DrlxParseSuccess(String.class, exprId, bindingId, converted.getExpression(), converted.getType()).setLeft(converted).setUsedDeclarations(typedExpressionResult.getUsedDeclarations());
            }).orElse(new DrlxParseFail());
        } else if (patternType != null) {
            NameExpr _this = new NameExpr("_this");
            TypedExpression converted = DrlxParseUtil.toMethodCallWithClassCheck(context, methodCallExpr, bindingId, patternType, context.getTypeResolver());
            Expression withThis = DrlxParseUtil.prepend(_this, converted.getExpression());
            return new DrlxParseSuccess(patternType, exprId, bindingId, withThis, converted.getType()).setLeft(converted);
        } else {
            return new DrlxParseSuccess(patternType, exprId, bindingId, methodCallExpr, null);
        }
    }
    if (drlxExpr instanceof FieldAccessExpr) {
        FieldAccessExpr fieldCallExpr = (FieldAccessExpr) drlxExpr;
        NameExpr _this = new NameExpr("_this");
        TypedExpression converted = DrlxParseUtil.toMethodCallWithClassCheck(context, fieldCallExpr, bindingId, patternType, context.getTypeResolver());
        Expression withThis = DrlxParseUtil.prepend(_this, converted.getExpression());
        return new DrlxParseSuccess(patternType, exprId, bindingId, withThis, converted.getType()).setLeft(converted);
    }
    if (drlxExpr instanceof NameExpr) {
        NameExpr nameExpr = (NameExpr) drlxExpr;
        NameExpr _this = new NameExpr("_this");
        TypedExpression converted = DrlxParseUtil.toMethodCallWithClassCheck(context, nameExpr, bindingId, patternType, context.getTypeResolver());
        Expression withThis = DrlxParseUtil.prepend(_this, converted.getExpression());
        if (drlx.getBind() != null) {
            return new DrlxParseSuccess(patternType, exprId, bindingId, null, converted.getType()).setLeft(new TypedExpression(withThis, converted.getType())).addReactOnProperty(lcFirst(nameExpr.getNameAsString()));
        } else {
            return new DrlxParseSuccess(patternType, exprId, bindingId, withThis, converted.getType()).addReactOnProperty(nameExpr.getNameAsString());
        }
    }
    if (drlxExpr instanceof OOPathExpr) {
        return new DrlxParseSuccess(patternType, exprId, bindingId, drlxExpr, null);
    }
    if (drlxExpr instanceof LiteralExpr) {
        return new DrlxParseSuccess(patternType, exprId, bindingId, drlxExpr, getLiteralExpressionType(((LiteralExpr) drlxExpr)));
    }
    // TODO
    throw new UnsupportedOperationException("Unknown expression: " + toDrlx(drlxExpr));
}
Also used : IntegerLiteralExpr(org.drools.javaparser.ast.expr.IntegerLiteralExpr) Expression(org.drools.javaparser.ast.expr.Expression) ThisExpr(org.drools.javaparser.ast.expr.ThisExpr) ClassUtil.toNonPrimitiveType(org.drools.modelcompiler.util.ClassUtil.toNonPrimitiveType) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) CastExpr(org.drools.javaparser.ast.expr.CastExpr) LiteralExpr(org.drools.javaparser.ast.expr.LiteralExpr) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) ArrayList(java.util.ArrayList) ExpressionTyperContext(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyperContext) BigDecimal(java.math.BigDecimal) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) ParseExpressionErrorResult(org.drools.modelcompiler.builder.errors.ParseExpressionErrorResult) ClassUtil(org.drools.modelcompiler.util.ClassUtil) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) DrlxParseUtil.getLiteralExpressionType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getLiteralExpressionType) LiteralStringValueExpr(org.drools.javaparser.ast.expr.LiteralStringValueExpr) MethodDeclaration(org.drools.javaparser.ast.body.MethodDeclaration) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) PrintUtil.toDrlx(org.drools.javaparser.printer.PrintUtil.toDrlx) GREATER(org.drools.javaparser.ast.expr.BinaryExpr.Operator.GREATER) PackageModel(org.drools.modelcompiler.builder.PackageModel) LESS(org.drools.javaparser.ast.expr.BinaryExpr.Operator.LESS) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) IntegerLiteralExpr(org.drools.javaparser.ast.expr.IntegerLiteralExpr) DrlxExpression(org.drools.javaparser.ast.drlx.expr.DrlxExpression) JavaParserUtil.toJavaParserType(org.drools.modelcompiler.util.JavaParserUtil.toJavaParserType) PointFreeExpr(org.drools.javaparser.ast.drlx.expr.PointFreeExpr) LESS_EQUALS(org.drools.javaparser.ast.expr.BinaryExpr.Operator.LESS_EQUALS) Collectors(java.util.stream.Collectors) NameExpr(org.drools.javaparser.ast.expr.NameExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) IndexUtil(org.drools.core.util.index.IndexUtil) UnaryExpr(org.drools.javaparser.ast.expr.UnaryExpr) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) List(java.util.List) OOPathExpr(org.drools.javaparser.ast.drlx.OOPathExpr) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) NodeList(org.drools.javaparser.ast.NodeList) StringUtils.lcFirst(org.drools.core.util.StringUtils.lcFirst) NodeWithArguments(org.drools.javaparser.ast.nodeTypes.NodeWithArguments) NodeWithOptionalScope(org.drools.javaparser.ast.nodeTypes.NodeWithOptionalScope) Optional(java.util.Optional) DrlxParseUtil.coerceLiteralExprToType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.coerceLiteralExprToType) GREATER_EQUALS(org.drools.javaparser.ast.expr.BinaryExpr.Operator.GREATER_EQUALS) DrlxParseUtil.isPrimitiveExpression(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isPrimitiveExpression) IndexUtil(org.drools.core.util.index.IndexUtil) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) ArrayList(java.util.ArrayList) ParseExpressionErrorResult(org.drools.modelcompiler.builder.errors.ParseExpressionErrorResult) ExpressionTyperContext(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyperContext) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) OOPathExpr(org.drools.javaparser.ast.drlx.OOPathExpr) LiteralExpr(org.drools.javaparser.ast.expr.LiteralExpr) IntegerLiteralExpr(org.drools.javaparser.ast.expr.IntegerLiteralExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) ThisExpr(org.drools.javaparser.ast.expr.ThisExpr) Optional(java.util.Optional) MethodDeclaration(org.drools.javaparser.ast.body.MethodDeclaration) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) PointFreeExpr(org.drools.javaparser.ast.drlx.expr.PointFreeExpr) LiteralStringValueExpr(org.drools.javaparser.ast.expr.LiteralStringValueExpr) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) UnaryExpr(org.drools.javaparser.ast.expr.UnaryExpr) Expression(org.drools.javaparser.ast.expr.Expression) DrlxExpression(org.drools.javaparser.ast.drlx.expr.DrlxExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxParseUtil.isPrimitiveExpression(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isPrimitiveExpression) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 4 with NameExpr

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

the class QueryGenerator method processQuery.

public static void processQuery(KnowledgeBuilderImpl kbuilder, PackageModel packageModel, QueryDescr queryDescr) {
    String queryDefVariableName = toQueryDef(queryDescr.getName());
    RuleContext context = packageModel.getQueryDefWithType().get(queryDefVariableName).getContext();
    new ModelGeneratorVisitor(context, packageModel).visit(queryDescr.getLhs());
    final Type queryType = JavaParser.parseType(Query.class.getCanonicalName());
    MethodDeclaration queryMethod = new MethodDeclaration(EnumSet.of(Modifier.PRIVATE), queryType, "query_" + toId(queryDescr.getName()));
    BlockStmt queryBody = new BlockStmt();
    ModelGenerator.createVariables(kbuilder, queryBody, packageModel, context);
    queryMethod.setBody(queryBody);
    String queryBuildVarName = toId(queryDescr.getName()) + "_build";
    VariableDeclarationExpr queryBuildVar = new VariableDeclarationExpr(queryType, queryBuildVarName);
    MethodCallExpr buildCall = new MethodCallExpr(new NameExpr(queryDefVariableName), BUILD_CALL);
    context.getExpressions().forEach(buildCall::addArgument);
    AssignExpr queryBuildAssign = new AssignExpr(queryBuildVar, buildCall, AssignExpr.Operator.ASSIGN);
    queryBody.addStatement(queryBuildAssign);
    queryBody.addStatement(new ReturnStmt(queryBuildVarName));
    packageModel.putQueryMethod(queryMethod);
}
Also used : VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) Query(org.drools.model.Query) MethodDeclaration(org.drools.javaparser.ast.body.MethodDeclaration) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) NameExpr(org.drools.javaparser.ast.expr.NameExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) Type(org.drools.javaparser.ast.type.Type) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) ReturnStmt(org.drools.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 5 with NameExpr

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

the class FlowExpressionBuilder method buildExpression.

private MethodCallExpr buildExpression(DrlxParseSuccess drlxParseResult, MethodCallExpr exprDSL) {
    final List<String> usedDeclarationsWithUnification = new ArrayList<>();
    if (drlxParseResult.isPatternBindingUnification()) {
        usedDeclarationsWithUnification.add(drlxParseResult.getPatternBinding());
    } else {
        if (drlxParseResult.getPatternBinding() != null && !drlxParseResult.getUsedDeclarations().contains(drlxParseResult.getPatternBinding())) {
            exprDSL.addArgument(new NameExpr(toVar(drlxParseResult.getPatternBinding())));
        }
    }
    usedDeclarationsWithUnification.addAll(drlxParseResult.getUsedDeclarations());
    usedDeclarationsWithUnification.stream().filter(s -> !(drlxParseResult.isSkipThisAsParam() && s.equals(drlxParseResult.getPatternBinding()))).map(x -> QueryGenerator.substituteBindingWithQueryParameter(context, x)).forEach(exprDSL::addArgument);
    exprDSL.addArgument(buildConstraintExpression(drlxParseResult, drlxParseResult.getExpr()));
    return exprDSL;
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) BIND_AS_CALL(org.drools.modelcompiler.builder.generator.ModelGenerator.BIND_AS_CALL) QueryGenerator(org.drools.modelcompiler.builder.generator.QueryGenerator) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) JavaParser(org.drools.javaparser.JavaParser) ArrayList(java.util.ArrayList) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) DrlxParseUtil.generateLambdaWithoutParameters(org.drools.modelcompiler.builder.generator.DrlxParseUtil.generateLambdaWithoutParameters) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt) DrlxParseUtil.toVar(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toVar) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) 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) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) Objects(java.util.Objects) UnknownType(org.drools.javaparser.ast.type.UnknownType) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) List(java.util.List) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) Stream(java.util.stream.Stream) ArrayList(java.util.ArrayList) NameExpr(org.drools.javaparser.ast.expr.NameExpr)

Aggregations

NameExpr (org.drools.javaparser.ast.expr.NameExpr)24 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)20 Expression (org.drools.javaparser.ast.expr.Expression)14 FieldAccessExpr (org.drools.javaparser.ast.expr.FieldAccessExpr)10 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)10 EnclosedExpr (org.drools.javaparser.ast.expr.EnclosedExpr)8 List (java.util.List)6 Optional (java.util.Optional)6 Parameter (org.drools.javaparser.ast.body.Parameter)6 BinaryExpr (org.drools.javaparser.ast.expr.BinaryExpr)6 LambdaExpr (org.drools.javaparser.ast.expr.LambdaExpr)6 RuleContext (org.drools.modelcompiler.builder.generator.RuleContext)6 ArrayList (java.util.ArrayList)5 ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)5 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)5 ExpressionStmt (org.drools.javaparser.ast.stmt.ExpressionStmt)5 PackageModel (org.drools.modelcompiler.builder.PackageModel)5 DeclarationSpec (org.drools.modelcompiler.builder.generator.DeclarationSpec)5 Collection (java.util.Collection)4 UnknownType (org.drools.javaparser.ast.type.UnknownType)4