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();
}
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);
}
Aggregations