Search in sources :

Example 81 with Expression

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

the class ExecModelLambdaPostProcessor method convertIndexedByCall.

private void convertIndexedByCall(MethodCallExpr methodCallExpr) {
    NodeList<Expression> arguments = methodCallExpr.getArguments();
    Expression firstArgument = arguments.get(0);
    if (!firstArgument.isClassExpr()) {
        logger.warn("argument is not ClassExpr. argument : {}, methodCallExpr : {}", firstArgument, methodCallExpr);
        return;
    }
    Optional<String> exprId = methodCallExpr.findFirst(StringLiteralExpr.class).map(LiteralStringValueExpr::getValue);
    Expression leftOperandExtractorLambda = arguments.get(3);
    if (leftOperandExtractorLambda.isLambdaExpr()) {
        replaceLambda(leftOperandExtractorLambda.asLambdaExpr(), id -> new MaterializedLambdaExtractor(packageName, ruleClassName, getType(firstArgument).clone()), exprId);
    }
    for (int i = 4; i < arguments.size(); i++) {
        Expression expr = arguments.get(i);
        if (expr.isLambdaExpr()) {
            replaceLambda(expr.asLambdaExpr(), id -> new MaterializedLambdaExtractor(packageName, ruleClassName, toClassOrInterfaceType(Object.class)), exprId);
        }
    }
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) LiteralStringValueExpr(com.github.javaparser.ast.expr.LiteralStringValueExpr)

Example 82 with Expression

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

the class Query method buildPattern.

@Override
public void buildPattern() {
    NameExpr queryCall = new NameExpr(toQueryDef(pattern.getObjectType()));
    MethodCallExpr callCall = new MethodCallExpr(queryCall, QUERY_INVOCATION_CALL);
    callCall.addArgument("" + !pattern.isQuery());
    if (!constraintDescrs.isEmpty()) {
        List<QueryParameter> queryParams = packageModel.queryVariables(queryName);
        if (queryParams.size() != constraintDescrs.size()) {
            context.addCompilationError(new InvalidExpressionErrorResult("Wrong number of argument invoking query '" + queryName + "'"));
            return;
        }
        Expression[] queryArgs = new Expression[queryParams.size()];
        for (int i = 0; i < constraintDescrs.size(); i++) {
            BaseDescr baseDescr = constraintDescrs.get(i);
            String itemText = baseDescr.getText();
            boolean isPositional = baseDescr instanceof ExprConstraintDescr && ((ExprConstraintDescr) baseDescr).getType() == ExprConstraintDescr.Type.POSITIONAL;
            boolean isBinding = baseDescr instanceof BindingDescr || itemText.contains(":");
            if ((!isPositional) && (!isBinding)) {
                // error, can't have non binding slots.
                context.addCompilationError(new InvalidExpressionErrorResult("Query's must use positional or bindings, not field constraints: " + itemText));
            } else if (isPositional && isBinding) {
                // error, can't have positional binding slots.
                context.addCompilationError(new InvalidExpressionErrorResult("Query's can't use positional bindings: " + itemText));
            }
            int colonPos = itemText.indexOf(':');
            if (colonPos > 0) {
                String bindingId = itemText.substring(0, colonPos).trim();
                String paramName = itemText.substring(colonPos + 1).trim();
                for (int j = 0; j < queryParams.size(); j++) {
                    if (queryParams.get(j).getName().equals(paramName)) {
                        addQueryArg(queryParams, queryArgs, bindingId, j);
                        break;
                    } else if (queryParams.get(j).getName().equals(bindingId)) {
                        addQueryArg(queryParams, queryArgs, paramName, j);
                        break;
                    }
                }
            } else {
                addQueryArg(queryParams, queryArgs, itemText, i);
            }
        }
        for (Expression queryArg : queryArgs) {
            callCall.addArgument(queryArg);
        }
    }
    context.addExpression(callCall);
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) QueryParameter(org.drools.modelcompiler.builder.generator.QueryParameter) NameExpr(com.github.javaparser.ast.expr.NameExpr) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) Expression(com.github.javaparser.ast.expr.Expression) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) BaseDescr(org.drools.drl.ast.descr.BaseDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 83 with Expression

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

the class Query method addQueryArg.

private void addQueryArg(List<QueryParameter> queryParams, Expression[] queryArgs, String itemText, int i) {
    if (isLiteral(itemText)) {
        MethodCallExpr valueOfMethod = createDslTopLevelMethod(VALUE_OF_CALL);
        valueOfMethod.addArgument(new NameExpr(itemText));
        queryArgs[i] = valueOfMethod;
    } else {
        Expression expr = parseExpression(itemText);
        if (expr.isNameExpr()) {
            context.addDeclaration(itemText, queryParams.get(i).getType());
            queryArgs[i] = context.getVarExpr(itemText);
        } else {
            String variableName = context.getExprId(queryParams.get(i).getType(), itemText);
            context.addDeclaration(variableName, queryParams.get(i).getType(), createFromExpr(variableName, expr));
            queryArgs[i] = context.getVarExpr(variableName);
        }
    }
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) NameExpr(com.github.javaparser.ast.expr.NameExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 84 with Expression

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

the class DrlxParseUtilTest method prependTestWithCast.

@Test
public void prependTestWithCast() {
    final Expression expr = StaticJavaParser.parseExpression("((InternationalAddress) getAddress()).getState()");
    final NameExpr nameExpr = new NameExpr(THIS_PLACEHOLDER);
    final Expression concatenated = DrlxParseUtil.prepend(nameExpr, expr);
    assertEquals("((InternationalAddress) _this.getAddress()).getState()", concatenated.toString());
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) NameExpr(com.github.javaparser.ast.expr.NameExpr) Test(org.junit.Test)

Example 85 with Expression

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

the class DrlxParseUtilTest method prependTest.

@Test
public void prependTest() {
    final Expression expr = StaticJavaParser.parseExpression("getAddressName().startsWith(\"M\")");
    final NameExpr nameExpr = new NameExpr(THIS_PLACEHOLDER);
    final Expression concatenated = DrlxParseUtil.prepend(nameExpr, expr);
    assertEquals(THIS_PLACEHOLDER + ".getAddressName().startsWith(\"M\")", concatenated.toString());
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) NameExpr(com.github.javaparser.ast.expr.NameExpr) Test(org.junit.Test)

Aggregations

Expression (com.github.javaparser.ast.expr.Expression)429 Test (org.junit.Test)166 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)149 NameExpr (com.github.javaparser.ast.expr.NameExpr)135 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)94 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)90 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)84 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)73 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)71 NodeList (com.github.javaparser.ast.NodeList)66 DrlxParser.parseExpression (org.drools.mvel.parser.DrlxParser.parseExpression)61 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)53 CompilationUnit (com.github.javaparser.ast.CompilationUnit)51 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)50 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)49 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)49 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)46 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)44 List (java.util.List)44 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)43