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