Search in sources :

Example 61 with Expression

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

the class AbstractExpressionBuilder method generateLambdaForTemporalConstraint.

protected Expression generateLambdaForTemporalConstraint(TypedExpression typedExpression, Class<?> patternType) {
    Expression expr = typedExpression.getExpression();
    Collection<String> usedDeclarations = DrlxParseUtil.collectUsedDeclarationsInExpression(expr);
    boolean containsThis = usedDeclarations.contains(THIS_PLACEHOLDER);
    if (containsThis) {
        usedDeclarations.remove(THIS_PLACEHOLDER);
    }
    Expression generatedExpr = generateLambdaWithoutParameters(usedDeclarations, expr, !containsThis, Optional.ofNullable(patternType), context);
    if (generatedExpr instanceof LambdaExpr) {
        context.getPackageModel().registerLambdaReturnType((LambdaExpr) generatedExpr, typedExpression.getType());
    }
    return generatedExpr;
}
Also used : DrlxParseUtil.isThisExpression(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isThisExpression) Expression(com.github.javaparser.ast.expr.Expression) CoercedExpression(org.drools.modelcompiler.builder.generator.drlxparse.CoercedExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr)

Example 62 with Expression

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

the class PatternExpressionBuilder method buildExpressionWithIndexing.

@Override
public MethodCallExpr buildExpressionWithIndexing(DrlxParseSuccess drlxParseResult) {
    if (drlxParseResult instanceof MultipleDrlxParseSuccess) {
        MultipleDrlxParseSuccess multi = (MultipleDrlxParseSuccess) drlxParseResult;
        MethodCallExpr exprDSL = new MethodCallExpr(null, multi.getOperator() == BinaryExpr.Operator.OR ? EXPR_OR_CALL : EXPR_AND_CALL);
        for (DrlxParseSuccess child : multi.getResults()) {
            MethodCallExpr childExpr = buildExpressionWithIndexing(child);
            childExpr.setScope(exprDSL);
            exprDSL = childExpr;
            if (child instanceof SingleDrlxParseSuccess && child.getExprBinding() != null) {
                SingleDrlxParseSuccess singleDrlxChild = (SingleDrlxParseSuccess) child;
                context.addDeclaration(child.getExprBinding(), singleDrlxChild.getLeftExprRawClass());
                Expression dslExpr = buildBinding(singleDrlxChild);
                context.addExpression(dslExpr);
            }
        }
        return new MethodCallExpr(exprDSL, multi.getOperator() == BinaryExpr.Operator.OR ? EXPR_END_OR_CALL : EXPR_END_AND_CALL);
    }
    return buildSingleExpressionWithIndexing((SingleDrlxParseSuccess) drlxParseResult);
}
Also used : SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) MultipleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.MultipleDrlxParseSuccess) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) MultipleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.MultipleDrlxParseSuccess) SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 63 with Expression

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

the class PatternExpressionBuilder method buildBinding.

@Override
public MethodCallExpr buildBinding(SingleDrlxParseSuccess drlxParseResult) {
    sortUsedDeclarations(drlxParseResult);
    MethodCallExpr bindDSL = new MethodCallExpr(null, BIND_CALL);
    String boundVar = drlxParseResult.hasUnificationVariable() ? drlxParseResult.getUnificationVariable() : drlxParseResult.getExprBinding();
    bindDSL.addArgument(context.getVarExpr(boundVar));
    final Expression constraintExpression = getBindingExpression(drlxParseResult);
    drlxParseResult.getUsedDeclarationsOnLeft().forEach(d -> bindDSL.addArgument(context.getVar(d)));
    bindDSL.addArgument(constraintExpression);
    final Optional<MethodCallExpr> methodCallExpr = buildReactOn(drlxParseResult);
    methodCallExpr.ifPresent(bindDSL::addArgument);
    context.registerBindingExpression(boundVar, bindDSL);
    return bindDSL;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 64 with Expression

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

the class PatternExpressionBuilder method processExpression.

@Override
public void processExpression(SingleDrlxParseSuccess drlxParseResult) {
    if (drlxParseResult.hasUnificationVariable()) {
        Expression dslExpr = buildUnificationExpression(drlxParseResult);
        context.addExpression(dslExpr);
    } else if (drlxParseResult.isPredicate()) {
        Expression dslExpr = buildExpressionWithIndexing(drlxParseResult);
        context.addExpression(dslExpr);
    }
    if (drlxParseResult.getExprBinding() != null) {
        Expression dslExpr = buildBinding(drlxParseResult);
        context.addExpression(dslExpr);
    }
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression)

Example 65 with Expression

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

the class FlattenScope method transformFullyQualifiedInlineCastExpr.

public static Expression transformFullyQualifiedInlineCastExpr(TypeResolver typeResolver, FullyQualifiedInlineCastExpr fqInlineCastExpr) {
    final Deque<String> expressionNamesWithoutType = new ArrayDeque<>();
    String className = findClassName(fqInlineCastExpr.getName(), typeResolver, expressionNamesWithoutType);
    Expression scope = fqInlineCastExpr.getScope();
    if (scope instanceof FullyQualifiedInlineCastExpr) {
        scope = transformFullyQualifiedInlineCastExpr(typeResolver, (FullyQualifiedInlineCastExpr) scope);
    }
    Expression expr = new InlineCastExpr(toClassOrInterfaceType(className), scope);
    // last element is the one with the actual arguments and need a special case
    if (!expressionNamesWithoutType.isEmpty()) {
        String lastElement = expressionNamesWithoutType.removeLast();
        // the others will be considered FieldAccessExpr
        for (String s : expressionNamesWithoutType) {
            expr = new FieldAccessExpr(expr, s);
        }
        if (fqInlineCastExpr.hasArguments()) {
            expr = new MethodCallExpr(expr, lastElement, fqInlineCastExpr.getArguments());
        } else {
            expr = new FieldAccessExpr(expr, lastElement);
        }
    }
    return expr;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) FullyQualifiedInlineCastExpr(org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr) InlineCastExpr(org.drools.mvel.parser.ast.expr.InlineCastExpr) NullSafeFieldAccessExpr(org.drools.mvel.parser.ast.expr.NullSafeFieldAccessExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ArrayDeque(java.util.ArrayDeque) FullyQualifiedInlineCastExpr(org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) NullSafeMethodCallExpr(org.drools.mvel.parser.ast.expr.NullSafeMethodCallExpr)

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