use of org.drools.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.
the class FlowExpressionBuilder method buildExpression.
private MethodCallExpr buildExpression(DrlxParseSuccess drlxParseResult, MethodCallExpr exprDSL) {
final List<String> usedDeclarationsWithUnification = new ArrayList<>();
if (drlxParseResult.isPatternBindingUnification()) {
usedDeclarationsWithUnification.add(drlxParseResult.getPatternBinding());
} else {
if (drlxParseResult.getPatternBinding() != null && !drlxParseResult.getUsedDeclarations().contains(drlxParseResult.getPatternBinding())) {
exprDSL.addArgument(new NameExpr(toVar(drlxParseResult.getPatternBinding())));
}
}
usedDeclarationsWithUnification.addAll(drlxParseResult.getUsedDeclarations());
usedDeclarationsWithUnification.stream().filter(s -> !(drlxParseResult.isSkipThisAsParam() && s.equals(drlxParseResult.getPatternBinding()))).map(x -> QueryGenerator.substituteBindingWithQueryParameter(context, x)).forEach(exprDSL::addArgument);
exprDSL.addArgument(buildConstraintExpression(drlxParseResult, drlxParseResult.getExpr()));
return exprDSL;
}
use of org.drools.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.
the class FlowExpressionBuilder method buildExpressionWithIndexing.
public Expression buildExpressionWithIndexing(DrlxParseSuccess drlxParseResult) {
String exprId = drlxParseResult.getExprId();
MethodCallExpr exprDSL = new MethodCallExpr(null, EXPR_CALL);
if (exprId != null && !"".equals(exprId)) {
exprDSL.addArgument(new StringLiteralExpr(exprId));
}
exprDSL = buildExpression(drlxParseResult, exprDSL);
exprDSL = buildIndexedBy(drlxParseResult, exprDSL);
exprDSL = buildReactOn(drlxParseResult, exprDSL);
return exprDSL;
}
use of org.drools.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.
the class PatternExpressionBuilder method buildExpressionWithIndexing.
@Override
public Expression buildExpressionWithIndexing(DrlxParseSuccess drlxParseResult) {
String exprId = drlxParseResult.getExprId();
MethodCallExpr exprDSL = new MethodCallExpr(null, EXPR_CALL);
if (exprId != null && !"".equals(exprId)) {
exprDSL.addArgument(new StringLiteralExpr(exprId));
}
exprDSL = buildExpression(drlxParseResult, exprDSL);
MethodCallExpr finalExprDSL = exprDSL;
Optional<MethodCallExpr> indexedByExpr = buildIndexedBy(drlxParseResult);
indexedByExpr.ifPresent(finalExprDSL::addArgument);
final Optional<MethodCallExpr> reactOnDSL = buildReactOn(drlxParseResult);
reactOnDSL.ifPresent(finalExprDSL::addArgument);
return exprDSL;
}
use of org.drools.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.
the class ExpressionTyper method flattenScope.
private static List<Node> flattenScope(Expression expressionWithScope) {
List<Node> res = new ArrayList<>();
if (expressionWithScope instanceof FieldAccessExpr) {
FieldAccessExpr fieldAccessExpr = (FieldAccessExpr) expressionWithScope;
res.addAll(flattenScope(fieldAccessExpr.getScope()));
res.add(fieldAccessExpr.getName());
} else if (expressionWithScope instanceof MethodCallExpr) {
MethodCallExpr methodCallExpr = (MethodCallExpr) expressionWithScope;
if (methodCallExpr.getScope().isPresent()) {
res.addAll(flattenScope(methodCallExpr.getScope().get()));
}
res.add(methodCallExpr.setScope(null));
} else if (expressionWithScope instanceof InlineCastExpr && ((InlineCastExpr) expressionWithScope).getExpression() instanceof FieldAccessExpr) {
InlineCastExpr inlineCastExpr = (InlineCastExpr) expressionWithScope;
Expression internalScope = ((FieldAccessExpr) inlineCastExpr.getExpression()).getScope();
res.addAll(flattenScope((internalScope)));
res.add(expressionWithScope);
} else {
res.add(expressionWithScope);
}
return res;
}
use of org.drools.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.
the class ExpressionTyper method toTypedExpressionRec.
private Optional<TypedExpression> toTypedExpressionRec(Expression drlxExpr) {
Class<?> typeCursor = patternType;
if (drlxExpr instanceof EnclosedExpr) {
drlxExpr = ((EnclosedExpr) drlxExpr).getInner();
}
if (drlxExpr instanceof UnaryExpr) {
UnaryExpr unaryExpr = (UnaryExpr) drlxExpr;
Optional<TypedExpression> optTypedExpr = toTypedExpressionRec(unaryExpr.getExpression());
return optTypedExpr.map(typedExpr -> new TypedExpression(new UnaryExpr(typedExpr.getExpression(), unaryExpr.getOperator()), typedExpr.getType()));
} else if (drlxExpr instanceof BinaryExpr) {
BinaryExpr binaryExpr = (BinaryExpr) drlxExpr;
BinaryExpr.Operator operator = binaryExpr.getOperator();
Optional<TypedExpression> optLeft = toTypedExpressionRec(binaryExpr.getLeft());
Optional<TypedExpression> optRight = toTypedExpressionRec(binaryExpr.getRight());
return optLeft.flatMap(left -> optRight.flatMap(right -> {
final BinaryExpr combo = new BinaryExpr(left.getExpression(), right.getExpression(), operator);
return of(new TypedExpression(combo, left.getType()));
}));
} else if (drlxExpr instanceof HalfBinaryExpr) {
final Expression binaryExpr = trasformHalfBinaryToBinary(drlxExpr);
return toTypedExpressionRec(binaryExpr);
} else if (drlxExpr instanceof LiteralExpr) {
return of(new TypedExpression(drlxExpr, getLiteralExpressionType((LiteralExpr) drlxExpr)));
} else if (drlxExpr instanceof ThisExpr) {
return of(new TypedExpression(new NameExpr("_this"), patternType));
} else if (drlxExpr instanceof CastExpr) {
CastExpr castExpr = (CastExpr) drlxExpr;
toTypedExpressionRec(castExpr.getExpression());
return of(new TypedExpression(castExpr, getClassFromContext(ruleContext.getTypeResolver(), castExpr.getType().asString())));
} else if (drlxExpr instanceof NameExpr) {
String name = drlxExpr.toString();
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);
return of(new TypedExpression(drlxExpr, 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(drlxExpr));
} else if (packageModel.getGlobals().containsKey(name)) {
Expression plusThis = new NameExpr(name);
context.addUsedDeclarations(name);
return of(new TypedExpression(plusThis, packageModel.getGlobals().get(name)));
} else {
TypedExpression expression;
try {
expression = nameExprToMethodCallExpr(name, typeCursor, null);
} catch (IllegalArgumentException e) {
if (isPositional || ruleContext.getQueryName().isPresent()) {
String unificationVariable = ruleContext.getOrCreateUnificationId(name);
expression = new TypedExpression(unificationVariable, typeCursor, name);
return of(expression);
}
return Optional.empty();
}
context.addReactOnProperties(name);
Expression plusThis = prepend(new NameExpr("_this"), expression.getExpression());
return of(new TypedExpression(plusThis, expression.getType(), name));
}
} else if (drlxExpr instanceof FieldAccessExpr || drlxExpr instanceof MethodCallExpr) {
return toTypedExpressionFromMethodCallOrField(drlxExpr).getTypedExpression();
} else if (drlxExpr instanceof PointFreeExpr) {
final PointFreeExpr pointFreeExpr = (PointFreeExpr) drlxExpr;
Optional<TypedExpression> optLeft = toTypedExpressionRec(pointFreeExpr.getLeft());
OperatorSpec opSpec = getOperatorSpec(drlxExpr, pointFreeExpr.getRight(), pointFreeExpr.getOperator());
return optLeft.map(left -> new TypedExpression(opSpec.getExpression(ruleContext, pointFreeExpr, left), left.getType()).setStatic(opSpec.isStatic()).setLeft(left));
} else if (drlxExpr instanceof HalfPointFreeExpr) {
final HalfPointFreeExpr halfPointFreeExpr = (HalfPointFreeExpr) drlxExpr;
Expression parentLeft = findLeftLeafOfNameExpr(halfPointFreeExpr.getParentNode().orElseThrow(UnsupportedOperationException::new));
Optional<TypedExpression> optLeft = toTypedExpressionRec(parentLeft);
OperatorSpec opSpec = getOperatorSpec(drlxExpr, halfPointFreeExpr.getRight(), halfPointFreeExpr.getOperator());
final PointFreeExpr transformedToPointFree = new PointFreeExpr(halfPointFreeExpr.getTokenRange().get(), parentLeft, halfPointFreeExpr.getRight(), halfPointFreeExpr.getOperator(), halfPointFreeExpr.isNegated(), halfPointFreeExpr.getArg1(), halfPointFreeExpr.getArg2(), halfPointFreeExpr.getArg3(), halfPointFreeExpr.getArg4());
return optLeft.map(left -> new TypedExpression(opSpec.getExpression(ruleContext, transformedToPointFree, left), left.getType()).setStatic(opSpec.isStatic()).setLeft(left));
}
throw new UnsupportedOperationException();
}
Aggregations