Search in sources :

Example 86 with MethodCallExpr

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

the class LhsParser method processMapProperty.

private Constraint processMapProperty(RuleContext context, Constraint constraint, TypedExpression expr) {
    MethodCallExpr mce = expr.getExpression().asMethodCallExpr();
    Optional<Expression> scope = mce.getScope();
    if (scope.isPresent() && scope.get().isMethodCallExpr()) {
        MethodCallExpr scopeMce = scope.get().asMethodCallExpr();
        String prop = ClassUtils.getter2property(scopeMce.getName().asString());
        Optional<Class<?>> origType = expr.getOriginalPatternType();
        if (prop != null && origType.isPresent()) {
            Method accessor = ClassUtils.getAccessor(origType.get(), prop);
            if (accessor != null && Map.class.isAssignableFrom(accessor.getReturnType()) && mce.getName().asString().equals("get")) {
                String key = getLiteralString(context, mce.getArgument(0));
                if (key != null) {
                    MapConstraint mapConstraint = new MapConstraint(constraint);
                    // map name
                    mapConstraint.setProperty(prop);
                    mapConstraint.setKey(key);
                    constraint = mapConstraint;
                }
            }
        }
    }
    return constraint;
}
Also used : ConstraintExpression(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintExpression) DrlxParseUtil.isThisExpression(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isThisExpression) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) MapConstraint(org.drools.impact.analysis.model.left.MapConstraint) ParserUtil.getLiteralString(org.drools.impact.analysis.parser.impl.ParserUtil.getLiteralString) Method(java.lang.reflect.Method) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 87 with MethodCallExpr

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

the class LhsParser method parseConstraint.

private void parseConstraint(RuleContext context, PatternDescr patternDescr, Pattern pattern, ConstraintParser constraintParser, BaseDescr constraintDescr) {
    ConstraintExpression constraintExpression = ConstraintExpression.createConstraintExpression(context, pattern.getPatternClass(), constraintDescr, false);
    DrlxParseResult drlxParseResult = constraintParser.drlxParse(pattern.getPatternClass(), patternDescr.getIdentifier(), constraintExpression, false);
    if (drlxParseResult.isSuccess()) {
        SingleDrlxParseSuccess result = (SingleDrlxParseSuccess) drlxParseResult;
        if (!result.getReactOnProperties().isEmpty()) {
            result.getReactOnProperties().forEach(pattern::addReactOn);
        } else {
            pattern.setClassReactive(true);
        }
        if (result.getRight() != null) {
            Constraint constraint = new Constraint();
            constraint = parseExpressionInConstraint(context, constraint, result.getLeft());
            boolean valueOnLeft = constraint.getValue() != null;
            constraint = parseExpressionInConstraint(context, constraint, result.getRight());
            if (constraint.getValue() != null) {
                // the constraint is relevant for impact analysis only if it checks a fixed value
                constraint.setType(decode(result.getDecodeConstraintType(), valueOnLeft));
                pattern.addConstraint(constraint);
            }
        } else if (result.getExprBinding() != null && result.getExpr() != null && result.getExpr().isLiteralExpr()) {
            ((ImpactAnalysisRuleContext) context).getBindVariableLiteralMap().put(result.getExprBinding(), ParserUtil.literalToValue(result.getExpr().asLiteralExpr()));
        } else if (isPredicateMethodCall(result)) {
            MethodCallExpr mce = result.getExpr().asMethodCallExpr();
            addConstraintIfBooleanProperty(pattern, mce, true);
        } else if (isNegatedPredicateMethodCall(result)) {
            MethodCallExpr mce = result.getExpr().asUnaryExpr().getExpression().asMethodCallExpr();
            addConstraintIfBooleanProperty(pattern, mce, false);
        }
    }
}
Also used : SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) ConstraintExpression(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintExpression) MapConstraint(org.drools.impact.analysis.model.left.MapConstraint) Constraint(org.drools.impact.analysis.model.left.Constraint) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 88 with MethodCallExpr

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

the class CoercedExpression method coerceToLocalDate.

private static TypedExpression coerceToLocalDate(TypedExpression typedExpression) {
    MethodCallExpr methodCallExpr = new MethodCallExpr(null, STRING_TO_LOCAL_DATE_METHOD);
    methodCallExpr.addArgument(typedExpression.getExpression());
    return new TypedExpression(methodCallExpr, LocalDate.class);
}
Also used : UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 89 with MethodCallExpr

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

the class CoercedExpression method coerceToDate.

private static TypedExpression coerceToDate(TypedExpression typedExpression) {
    MethodCallExpr methodCallExpr = new MethodCallExpr(null, STRING_TO_DATE_METHOD);
    methodCallExpr.addArgument(typedExpression.getExpression());
    return new TypedExpression(methodCallExpr, Date.class);
}
Also used : UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 90 with MethodCallExpr

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

the class ConstraintParser method parseFunctionInEval.

private DrlxParseResult parseFunctionInEval(MethodCallExpr methodCallExpr, Class<?> patternType, String bindingId, boolean isPositional, Optional<MethodDeclaration> functionCall) {
    // 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_PLACEHOLDER));
    }
    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) {
            String argString = printNode(arg);
            if (arg instanceof DrlNameExpr && !argString.equals(THIS_PLACEHOLDER)) {
                usedDeclarations.add(argString);
            } else if (arg instanceof CastExpr) {
                String s = printNode(((CastExpr) arg).getExpression());
                usedDeclarations.add(s);
            } else if (arg instanceof MethodCallExpr) {
                TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, null, bindingId, isPositional).toTypedExpression(arg);
                usedDeclarations.addAll(typedExpressionResult.getUsedDeclarations());
            }
        }
        return new SingleDrlxParseSuccess(patternType, bindingId, methodCallExpr, returnType).setUsedDeclarations(usedDeclarations).setIsPredicate(isBooleanBoxedUnboxed(returnType));
    } else {
        throw new IllegalArgumentException("Specified function call is not present!");
    }
}
Also used : DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) ArrayList(java.util.ArrayList) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) FlattenScope.transformFullyQualifiedInlineCastExpr(org.drools.modelcompiler.builder.generator.expressiontyper.FlattenScope.transformFullyQualifiedInlineCastExpr) FullyQualifiedInlineCastExpr(org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr) CastExpr(com.github.javaparser.ast.expr.CastExpr) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)299 Expression (com.github.javaparser.ast.expr.Expression)142 NameExpr (com.github.javaparser.ast.expr.NameExpr)106 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)101 Test (org.junit.Test)89 CompilationUnit (com.github.javaparser.ast.CompilationUnit)78 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)77 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)57 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)52 NodeList (com.github.javaparser.ast.NodeList)47 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)47 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)46 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)39 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)37 List (java.util.List)34 MethodUsage (com.github.javaparser.resolution.MethodUsage)33 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)32 ArrayList (java.util.ArrayList)31 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)30 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)29