Search in sources :

Example 1 with UnificationTypedExpression

use of org.drools.modelcompiler.builder.generator.UnificationTypedExpression in project drools by kiegroup.

the class ExpressionTyper method nameExpr.

private Optional<TypedExpression> nameExpr(String name, Class<?> typeCursor) {
    TypedExpression expression = nameExprToMethodCallExpr(name, typeCursor, null, ruleContext);
    if (expression != null) {
        context.addReactOnProperties(name);
        Expression plusThis = prepend(new NameExpr(THIS_PLACEHOLDER), expression.getExpression());
        return of(new TypedExpression(plusThis, expression.getType(), name));
    }
    Optional<DeclarationSpec> decl = ruleContext.getDeclarationById(name);
    if (decl.isPresent()) {
        // then drlxExpr is a single NameExpr referring to a binding, e.g.: "$p1".
        context.addUsedDeclarations(name);
        decl.get().getBoundVariable().ifPresent(context::addReactOnProperties);
        return of(new TypedExpression(new NameExpr(name), decl.get().getDeclarationClass()));
    }
    if (ruleContext.getQueryParameters().stream().anyMatch(qp -> qp.getName().equals(name))) {
        // then drlxExpr is a single NameExpr referring to a query parameter, e.g.: "$p1".
        context.addUsedDeclarations(name);
        return of(new TypedExpression(new NameExpr(name)));
    } else if (packageModel.getGlobals().containsKey(name)) {
        Expression plusThis = new NameExpr(name);
        context.addUsedDeclarations(name);
        return of(new TypedExpression(plusThis, packageModel.getGlobals().get(name)));
    } else if (isPositional || ruleContext.isQuery()) {
        String unificationVariable = ruleContext.getOrCreateUnificationId(name);
        expression = new UnificationTypedExpression(unificationVariable, typeCursor, name);
        return of(expression);
    }
    return empty();
}
Also used : DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) Expression(com.github.javaparser.ast.expr.Expression) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) MapCreationLiteralExpression(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression) DrlxParseUtil.isThisExpression(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isThisExpression) ListCreationLiteralExpression(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxParseUtil.transformDrlNameExprToNameExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.transformDrlNameExprToNameExpr) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression)

Example 2 with UnificationTypedExpression

use of org.drools.modelcompiler.builder.generator.UnificationTypedExpression in project drools by kiegroup.

the class CoercedExpression method coerce.

public CoercedExpressionResult coerce() {
    final Class<?> leftClass = left.getRawClass();
    final Class<?> nonPrimitiveLeftClass = toNonPrimitiveType(leftClass);
    final Class<?> rightClass = right.getRawClass();
    final Class<?> nonPrimitiveRightClass = toNonPrimitiveType(rightClass);
    boolean sameClass = leftClass == rightClass;
    boolean isUnificationExpression = left instanceof UnificationTypedExpression || right instanceof UnificationTypedExpression;
    if (sameClass || isUnificationExpression) {
        return new CoercedExpressionResult(left, right);
    }
    if (!canCoerce()) {
        throw new CoercedExpressionException(new InvalidExpressionErrorResult("Comparison operation requires compatible types. Found " + leftClass + " and " + rightClass));
    }
    if ((nonPrimitiveLeftClass == Integer.class || nonPrimitiveLeftClass == Long.class) && nonPrimitiveRightClass == Double.class) {
        CastExpr castExpression = new CastExpr(PrimitiveType.doubleType(), this.left.getExpression());
        return new CoercedExpressionResult(new TypedExpression(castExpression, double.class, left.getType()), right, false);
    }
    final boolean leftIsPrimitive = leftClass.isPrimitive() || Number.class.isAssignableFrom(leftClass);
    final boolean canCoerceLiteralNumberExpr = canCoerceLiteralNumberExpr(leftClass);
    boolean rightAsStaticField = false;
    final Expression rightExpression = right.getExpression();
    final TypedExpression coercedRight;
    if (leftIsPrimitive && canCoerceLiteralNumberExpr && rightExpression instanceof LiteralStringValueExpr) {
        final Expression coercedLiteralNumberExprToType = coerceLiteralNumberExprToType((LiteralStringValueExpr) right.getExpression(), leftClass);
        coercedRight = right.cloneWithNewExpression(coercedLiteralNumberExprToType);
        coercedRight.setType(leftClass);
    } else if (shouldCoerceBToString(left, right)) {
        coercedRight = coerceToString(right);
    } else if (isNotBinaryExpression(right) && canBeNarrowed(leftClass, rightClass) && right.isNumberLiteral()) {
        coercedRight = castToClass(leftClass);
    } else if (leftClass == long.class && rightClass == int.class) {
        coercedRight = right.cloneWithNewExpression(new CastExpr(PrimitiveType.longType(), right.getExpression()));
    } else if (leftClass == Date.class && rightClass == String.class) {
        coercedRight = coerceToDate(right);
        rightAsStaticField = true;
    } else if (leftClass == LocalDate.class && rightClass == String.class) {
        coercedRight = coerceToLocalDate(right);
        rightAsStaticField = true;
    } else if (leftClass == LocalDateTime.class && rightClass == String.class) {
        coercedRight = coerceToLocalDateTime(right);
        rightAsStaticField = true;
    } else if (shouldCoerceBToMap()) {
        coercedRight = castToClass(toNonPrimitiveType(leftClass));
    } else if (isBoolean(leftClass) && !isBoolean(rightClass)) {
        coercedRight = coerceBoolean(right);
    } else {
        coercedRight = right;
    }
    final TypedExpression coercedLeft;
    if (nonPrimitiveLeftClass == Character.class && shouldCoerceBToString(right, left)) {
        coercedLeft = coerceToString(left);
    } else {
        coercedLeft = left;
    }
    return new CoercedExpressionResult(coercedLeft, coercedRight, rightAsStaticField);
}
Also used : LocalDateTime(java.time.LocalDateTime) LiteralStringValueExpr(com.github.javaparser.ast.expr.LiteralStringValueExpr) Date(java.util.Date) LocalDate(java.time.LocalDate) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) Expression(com.github.javaparser.ast.expr.Expression) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) CastExpr(com.github.javaparser.ast.expr.CastExpr) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression)

Aggregations

Expression (com.github.javaparser.ast.expr.Expression)2 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)2 UnificationTypedExpression (org.drools.modelcompiler.builder.generator.UnificationTypedExpression)2 CastExpr (com.github.javaparser.ast.expr.CastExpr)1 LiteralStringValueExpr (com.github.javaparser.ast.expr.LiteralStringValueExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 Date (java.util.Date)1 InvalidExpressionErrorResult (org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult)1 DeclarationSpec (org.drools.modelcompiler.builder.generator.DeclarationSpec)1 DrlxParseUtil.isThisExpression (org.drools.modelcompiler.builder.generator.DrlxParseUtil.isThisExpression)1 DrlxParseUtil.transformDrlNameExprToNameExpr (org.drools.modelcompiler.builder.generator.DrlxParseUtil.transformDrlNameExprToNameExpr)1 DrlNameExpr (org.drools.mvel.parser.ast.expr.DrlNameExpr)1 ListCreationLiteralExpression (org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression)1 MapCreationLiteralExpression (org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression)1