use of com.github.javaparser.ast.expr.Expression 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!");
}
}
use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class ConstraintParser method combineExpressions.
private Expression combineExpressions(List<Expression> leftPrefixExpresssions, List<Expression> rightPrefixExpresssions, Expression combo) {
Expression inner = combo;
if (combo.isEnclosedExpr()) {
EnclosedExpr enclosedExpr = combo.asEnclosedExpr();
inner = stripEnclosedExpr(enclosedExpr);
}
BinaryExpr binaryExpr;
if (inner.isBinaryExpr()) {
binaryExpr = inner.asBinaryExpr();
} else {
throw new RuntimeException(combo + " is not nor contains BinaryExpr");
}
Expression left = binaryExpr.getLeft();
for (Expression prefixExpression : leftPrefixExpresssions) {
left = new BinaryExpr(prefixExpression, left, BinaryExpr.Operator.AND);
}
binaryExpr.setLeft(left);
Expression right = binaryExpr.getRight();
for (Expression prefixExpression : rightPrefixExpresssions) {
right = new BinaryExpr(prefixExpression, right, BinaryExpr.Operator.AND);
}
binaryExpr.setRight(right);
return combo;
}
use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class ConstraintParser method getEqualityExpression.
private SpecialComparisonResult getEqualityExpression(TypedExpression left, TypedExpression right, BinaryExpr.Operator operator) {
if ((isAnyOperandBigDecimal(left, right) || isAnyOperandBigInteger(left, right)) && !isAnyOperandNullLiteral(left, right)) {
return compareBigDecimal(operator, left, right);
}
boolean isLeftNumber = isNumber(left);
boolean isRightNumber = isNumber(right);
String equalsMethod = isLeftNumber && isRightNumber ? "org.drools.modelcompiler.util.EvaluationUtil.areNumbersNullSafeEquals" : "org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals";
Expression leftExpr = left.uncastExpression();
Expression rightExpr = right.uncastExpression();
if (isLeftNumber) {
if (isString(right)) {
leftExpr = new BinaryExpr(new StringLiteralExpr(""), leftExpr, PLUS);
}
} else if (isRightNumber && isString(left)) {
rightExpr = new BinaryExpr(new StringLiteralExpr(""), rightExpr, PLUS);
}
MethodCallExpr methodCallExpr = new MethodCallExpr(null, equalsMethod);
// Avoid casts, by using an helper method we leverage autoboxing and equals
methodCallExpr.addArgument(leftExpr);
methodCallExpr.addArgument(rightExpr);
Expression expression = operator == BinaryExpr.Operator.EQUALS ? methodCallExpr : new UnaryExpr(methodCallExpr, UnaryExpr.Operator.LOGICAL_COMPLEMENT);
return new SpecialComparisonResult(expression, left, right);
}
use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class ConstraintParser method compileToJavaRecursive.
private DrlxParseResult compileToJavaRecursive(Class<?> patternType, String bindingId, ConstraintExpression constraint, Expression drlxExpr, boolean hasBind, boolean isPositional) {
boolean isEnclosed = false;
SimpleName bind = null;
if (drlxExpr instanceof FullyQualifiedInlineCastExpr) {
drlxExpr = transformFullyQualifiedInlineCastExpr(context.getTypeResolver(), (FullyQualifiedInlineCastExpr) drlxExpr);
}
while (drlxExpr instanceof EnclosedExpr) {
drlxExpr = ((EnclosedExpr) drlxExpr).getInner();
isEnclosed = true;
}
if (drlxExpr instanceof DrlxExpression) {
bind = ((DrlxExpression) drlxExpr).getBind();
drlxExpr = ((DrlxExpression) drlxExpr).getExpr();
}
if (drlxExpr instanceof MethodCallExpr && !((MethodCallExpr) drlxExpr).getScope().isPresent() && ((MethodCallExpr) drlxExpr).getNameAsString().equals("eval")) {
drlxExpr = ((MethodCallExpr) drlxExpr).getArgument(0);
}
if (drlxExpr instanceof BinaryExpr) {
DrlxParseResult result = parseBinaryExpr((BinaryExpr) drlxExpr, patternType, bindingId, constraint, drlxExpr, hasBind, isPositional, isEnclosed);
if (result instanceof SingleDrlxParseSuccess && bind != null) {
((SingleDrlxParseSuccess) result).setExprBinding(bind.asString());
}
return result;
}
if (drlxExpr instanceof UnaryExpr) {
return parseUnaryExpr((UnaryExpr) drlxExpr, patternType, bindingId, constraint, drlxExpr, hasBind, isPositional);
}
if (drlxExpr instanceof PointFreeExpr) {
return parsePointFreeExpr((PointFreeExpr) drlxExpr, patternType, bindingId, isPositional);
}
if (patternType == null && drlxExpr instanceof MethodCallExpr) {
MethodCallExpr methodCallExpr = (MethodCallExpr) drlxExpr;
Optional<MethodDeclaration> functionCall = packageModel.getFunctions().stream().filter(m -> m.getName().equals(methodCallExpr.getName())).findFirst();
if (functionCall.isPresent()) {
return parseFunctionInEval(methodCallExpr, patternType, bindingId, isPositional, functionCall);
}
}
if (drlxExpr instanceof FieldAccessExpr) {
return parseFieldAccessExpr((FieldAccessExpr) drlxExpr, patternType, bindingId);
}
String expression = constraint.getExpression();
if (drlxExpr instanceof DrlNameExpr) {
return parseNameExpr((DrlNameExpr) drlxExpr, patternType, bindingId, drlxExpr, hasBind, expression);
}
if (drlxExpr instanceof OOPathExpr) {
return parseOOPathExpr((OOPathExpr) drlxExpr, patternType, bindingId, drlxExpr, hasBind, expression);
}
if (drlxExpr instanceof LiteralExpr) {
Class<?> literalExpressionType = getLiteralExpressionType(((LiteralExpr) drlxExpr));
return new SingleDrlxParseSuccess(patternType, bindingId, drlxExpr, literalExpressionType).setIsPredicate(isBooleanBoxedUnboxed(literalExpressionType));
}
if (patternType != null) {
ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
ExpressionTyper expressionTyper = new ExpressionTyper(context, patternType, bindingId, isPositional, expressionTyperContext);
TypedExpressionResult leftTypedExpressionResult = expressionTyper.toTypedExpression(drlxExpr);
Optional<TypedExpression> optLeft = leftTypedExpressionResult.getTypedExpression();
if (!optLeft.isPresent()) {
return new DrlxParseFail();
}
TypedExpression left = optLeft.get();
Expression combo = left.getExpression();
Type exprType = left.getType();
boolean isPredicate = isBooleanBoxedUnboxed(exprType);
if (isPredicate) {
combo = combineExpressions(leftTypedExpressionResult, combo);
}
return new SingleDrlxParseSuccess(patternType, bindingId, combo, exprType).setReactOnProperties(expressionTyperContext.getReactOnProperties()).setUsedDeclarations(expressionTyperContext.getUsedDeclarations()).setImplicitCastExpression(expressionTyperContext.getInlineCastExpression()).setNullSafeExpressions(expressionTyperContext.getNullSafeExpressions()).setIsPredicate(isPredicate);
} else {
final ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
final ExpressionTyper expressionTyper = new ExpressionTyper(context, null, bindingId, isPositional, expressionTyperContext);
TypedExpressionResult leftTypedExpressionResult = expressionTyper.toTypedExpression(drlxExpr);
Optional<TypedExpression> optLeft = leftTypedExpressionResult.getTypedExpression();
if (!optLeft.isPresent()) {
return new DrlxParseFail();
}
TypedExpression left = optLeft.get();
return new SingleDrlxParseSuccess(null, bindingId, drlxExpr, left.getType()).setUsedDeclarations(expressionTyperContext.getUsedDeclarations()).setIsPredicate(true);
}
}
use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class Consequence method findModifiedPropertiesFromAssignment.
private Set<String> findModifiedPropertiesFromAssignment(List<AssignExpr> assignExprs, MethodCallExpr updateExpr, String updatedVar, Class<?> updatedClass) {
Set<String> modifiedProps = new HashSet<>();
for (AssignExpr assignExpr : assignExprs) {
Expression target = assignExpr.getTarget();
if (target instanceof FieldAccessExpr) {
FieldAccessExpr fieldAccessExpr = (FieldAccessExpr) target;
Expression scope = fieldAccessExpr.getScope();
if (scope instanceof NameExpr && ((NameExpr) scope).getNameAsString().equals(updatedVar)) {
modifiedProps.add(fieldAccessExpr.getNameAsString());
}
}
}
return modifiedProps;
}
Aggregations