use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class RhsParser method processModify.
private ModifyAction processModify(RuleContext context, MethodCallExpr consequenceExpr, MethodCallExpr statement, BlockStmt ruleVariablesBlock) {
String modifiedId = statement.getArgument(0).toString();
Class<?> modifiedClass = context.getDeclarationById(modifiedId).orElseThrow(() -> new RuntimeException("Unknown declaration: " + modifiedId)).getDeclarationClass();
ModifyAction action = new ModifyAction(modifiedClass);
if (statement.getArguments().size() > 1) {
String maskId = statement.getArgument(1).toString();
AssignExpr maskAssignExpr = ruleVariablesBlock.findAll(AssignExpr.class).stream().filter(assign -> ((VariableDeclarationExpr) assign.getTarget()).getVariable(0).toString().equals(maskId)).findFirst().orElseThrow(() -> new RuntimeException("Unknown mask: " + maskId));
MethodCallExpr maskMethod = ((MethodCallExpr) maskAssignExpr.getValue());
List<MethodCallExpr> modifyingExprs = consequenceExpr.findAll(MethodCallExpr.class).stream().filter(m -> m.getScope().map(s -> s.toString().equals(modifiedId) || s.toString().equals("(" + modifiedId + ")")).orElse(false)).collect(Collectors.toList());
for (int i = 1; i < maskMethod.getArguments().size(); i++) {
String property = maskMethod.getArgument(i).asStringLiteralExpr().asString();
String setter = "set" + ucFirst(property);
MethodCallExpr setterExpr = modifyingExprs.stream().filter(m -> m.getNameAsString().equals(setter)).filter(m -> m.getArguments().size() == 1).findFirst().orElse(null);
Object value = null;
if (setterExpr != null) {
Expression arg = setterExpr.getArgument(0);
arg = stripEnclosedAndCast(arg);
if (arg.isLiteralExpr()) {
value = literalToValue(arg.asLiteralExpr());
} else if (arg.isNameExpr()) {
value = ((ImpactAnalysisRuleContext) context).getBindVariableLiteralMap().get(arg.asNameExpr().getName().asString());
} else if (arg.isObjectCreationExpr()) {
value = objectCreationExprToValue((ObjectCreationExpr) arg, context);
}
}
Method accessor = ClassUtils.getAccessor(modifiedClass, property);
if (accessor != null && Map.class.isAssignableFrom(accessor.getReturnType())) {
String mapName = property;
List<MethodCallExpr> mapPutExprs = consequenceExpr.findAll(MethodCallExpr.class).stream().filter(m -> isMapPutExpr(m, modifiedId, accessor.getName())).collect(Collectors.toList());
mapPutExprs.stream().forEach(expr -> {
String mapKey = getLiteralString(context, expr.getArgument(0));
Object mapValue = getLiteralValue(context, expr.getArgument(1));
action.addModifiedProperty(new ModifiedMapProperty(mapName, mapKey, mapValue));
});
} else {
action.addModifiedProperty(new ModifiedProperty(property, value));
}
}
}
return action;
}
use of com.github.javaparser.ast.expr.Expression 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;
}
use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class LhsParser method parseExpressionInConstraint.
private Constraint parseExpressionInConstraint(RuleContext context, Constraint constraint, TypedExpression expr) {
Expression expression = expr.getExpression();
expression = stripEnclosedAndCast(expression);
if (expression.isMethodCallExpr()) {
if (isThisExpression(expression.asMethodCallExpr().getScope().orElse(null))) {
constraint.setProperty(expr.getFieldName());
} else {
constraint = processMapProperty(context, constraint, expr);
}
} else if (expression.isLiteralExpr()) {
constraint.setValue(literalToValue(expression.asLiteralExpr()));
} else if (expression.isNameExpr() && expression.asNameExpr().getNameAsString().equals("_this")) {
constraint.setProperty("this");
} else if (expression.isObjectCreationExpr()) {
constraint.setValue(objectCreationExprToValue(expression.asObjectCreationExpr(), context));
}
return constraint;
}
use of com.github.javaparser.ast.expr.Expression 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);
}
use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class ConstraintParser method handleSpecialComparisonCases.
private SpecialComparisonResult handleSpecialComparisonCases(ExpressionTyper expressionTyper, BinaryExpr.Operator operator, TypedExpression left, TypedExpression right) {
if (isLogicalOperator(operator)) {
Expression rewrittenLeft = handleSpecialComparisonCases(expressionTyper, left);
Expression rewrittenRight = handleSpecialComparisonCases(expressionTyper, right);
if (rewrittenLeft != null && rewrittenRight != null) {
return new SpecialComparisonResult(new BinaryExpr(rewrittenLeft, rewrittenRight, operator), left, right);
}
}
boolean comparison = isComparisonOperator(operator);
if ((isAnyOperandBigDecimal(left, right) || isAnyOperandBigInteger(left, right)) && comparison) {
return compareBigDecimal(operator, left, right);
}
if (comparison) {
SpecialComparisonCase methodName = specialComparisonFactory(left, right);
return methodName.createCompareMethod(operator);
}
return new SpecialComparisonResult(new BinaryExpr(left.getExpression(), right.getExpression(), operator), left, right);
}
Aggregations