use of org.drools.mvel.parser.ast.expr.OOPathChunk in project drools by kiegroup.
the class ExpressionTyper method toTypedExpressionRec.
private Optional<TypedExpression> toTypedExpressionRec(Expression drlxExpr) {
Class<?> typeCursor = patternType;
if (drlxExpr instanceof FullyQualifiedInlineCastExpr) {
return toTypedExpressionRec(transformFullyQualifiedInlineCastExpr(ruleContext.getTypeResolver(), (FullyQualifiedInlineCastExpr) drlxExpr));
}
if (drlxExpr instanceof EnclosedExpr) {
Expression inner = ((EnclosedExpr) drlxExpr).getInner();
Optional<TypedExpression> typedExpression = toTypedExpressionRec(inner);
return typedExpression.map(t -> t.cloneWithNewExpression(new EnclosedExpr(t.getExpression())));
}
if (drlxExpr instanceof MethodCallExpr) {
MethodCallExpr methodExpr = (MethodCallExpr) drlxExpr;
Expression expr = methodExpr;
if (isEval(methodExpr.getNameAsString(), methodExpr.getScope(), methodExpr.getArguments())) {
expr = methodExpr.getArgument(0);
}
drlxExpr = expr;
}
if (drlxExpr instanceof NullSafeMethodCallExpr) {
NullSafeMethodCallExpr methodExpr = (NullSafeMethodCallExpr) drlxExpr;
Expression expr = methodExpr;
if (isEval(methodExpr.getNameAsString(), methodExpr.getScope(), methodExpr.getArguments())) {
expr = methodExpr.getArgument(0);
}
drlxExpr = expr;
}
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()));
}
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()));
}));
}
if (drlxExpr instanceof HalfBinaryExpr) {
final Expression binaryExpr = trasformHalfBinaryToBinary(drlxExpr);
if (binaryExpr instanceof BinaryExpr && ((BinaryExpr) binaryExpr).getLeft() == drlxExpr) {
throw new CannotTypeExpressionException("left leaf is the same : drlxExpr = " + drlxExpr + ", originalExpression = " + context.getOriginalExpression());
}
return toTypedExpressionRec(binaryExpr);
}
if (drlxExpr instanceof LiteralExpr) {
drlxExpr = normalizeDigit(drlxExpr);
return of(new TypedExpression(drlxExpr, getLiteralExpressionType((LiteralExpr) drlxExpr)));
}
if (drlxExpr instanceof ThisExpr || (drlxExpr instanceof NameExpr && THIS_PLACEHOLDER.equals(printNode(drlxExpr)))) {
return of(new TypedExpression(new NameExpr(THIS_PLACEHOLDER), patternType));
}
if (drlxExpr instanceof CastExpr) {
CastExpr castExpr = (CastExpr) drlxExpr;
Optional<TypedExpression> optTypedExpr = toTypedExpressionRec(castExpr.getExpression());
return optTypedExpr.map(typedExpr -> new TypedExpression(new CastExpr(castExpr.getType(), typedExpr.getExpression()), getClassFromContext(ruleContext.getTypeResolver(), castExpr.getType().asString())));
}
if (drlxExpr instanceof NameExpr) {
return nameExpr(((NameExpr) drlxExpr).getNameAsString(), typeCursor);
}
if (drlxExpr instanceof FieldAccessExpr || drlxExpr instanceof MethodCallExpr || drlxExpr instanceof ObjectCreationExpr || drlxExpr instanceof NullSafeFieldAccessExpr || drlxExpr instanceof NullSafeMethodCallExpr || drlxExpr instanceof MapCreationLiteralExpression || drlxExpr instanceof ListCreationLiteralExpression) {
return toTypedExpressionFromMethodCallOrField(drlxExpr).getTypedExpression();
}
if (drlxExpr instanceof PointFreeExpr) {
final PointFreeExpr pointFreeExpr = (PointFreeExpr) drlxExpr;
Optional<TypedExpression> optLeft = toTypedExpressionRec(pointFreeExpr.getLeft());
Optional<TypedExpression> optRight = pointFreeExpr.getRight().size() == 1 ? toTypedExpressionRec(pointFreeExpr.getRight().get(0)) : Optional.empty();
OperatorSpec opSpec = getOperatorSpec(pointFreeExpr.getRight(), pointFreeExpr.getOperator());
return optLeft.map(left -> new TypedExpression(opSpec.getExpression(ruleContext, pointFreeExpr, left, this), left.getType()).setStatic(opSpec.isStatic()).setLeft(left).setRight(optRight.orElse(null)));
}
if (drlxExpr instanceof HalfPointFreeExpr) {
final HalfPointFreeExpr halfPointFreeExpr = (HalfPointFreeExpr) drlxExpr;
Expression parentLeft = findLeftLeafOfNameExprTraversingParent(halfPointFreeExpr);
if (parentLeft == halfPointFreeExpr) {
throw new CannotTypeExpressionException("left leaf is the same : halfPointFreeExpr = " + halfPointFreeExpr + ", originalExpression = " + context.getOriginalExpression());
}
Optional<TypedExpression> optLeft = toTypedExpressionRec(parentLeft);
OperatorSpec opSpec = getOperatorSpec(halfPointFreeExpr.getRight(), halfPointFreeExpr.getOperator());
final PointFreeExpr transformedToPointFree = new PointFreeExpr(halfPointFreeExpr.getTokenRange().orElseThrow(() -> new IllegalStateException("Token range is not present!")), 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, this), left.getType()).setStatic(opSpec.isStatic()).setLeft(left));
}
if (drlxExpr instanceof ArrayAccessExpr) {
final ArrayAccessExpr arrayAccessExpr = (ArrayAccessExpr) drlxExpr;
if (Map.class.isAssignableFrom(typeCursor)) {
return createMapAccessExpression(arrayAccessExpr.getIndex(), arrayAccessExpr.getName() instanceof ThisExpr ? new NameExpr(THIS_PLACEHOLDER) : arrayAccessExpr.getName(), Map.class);
} else if (arrayAccessExpr.getName() instanceof FieldAccessExpr) {
Optional<TypedExpression> typedExpression = toTypedExpressionFromMethodCallOrField(drlxExpr).getTypedExpression();
typedExpression.ifPresent(te -> {
final Expression originalExpression = te.getExpression();
DrlxParseUtil.removeRootNode(originalExpression);
});
return typedExpression;
} else {
String name = printNode(drlxExpr.asArrayAccessExpr().getName());
final Optional<TypedExpression> nameExpr = nameExpr(name, typeCursor);
Expression indexExpr = toTypedExpressionFromMethodCallOrField(arrayAccessExpr.getIndex()).getTypedExpression().orElseThrow(() -> new NoSuchElementException("TypedExpressionResult doesn't contain TypedExpression!")).getExpression();
return nameExpr.flatMap(te -> transformToArrayOrMapExpressionWithType(indexExpr, te));
}
}
if (drlxExpr instanceof InstanceOfExpr) {
InstanceOfExpr instanceOfExpr = (InstanceOfExpr) drlxExpr;
ruleContext.addInlineCastType(printNode(instanceOfExpr.getExpression()), instanceOfExpr.getType());
return toTypedExpressionRec(instanceOfExpr.getExpression()).map(e -> new TypedExpression(new InstanceOfExpr(e.getExpression(), instanceOfExpr.getType()), boolean.class));
}
if (drlxExpr instanceof ClassExpr) {
return of(new TypedExpression(drlxExpr, Class.class));
}
if (drlxExpr instanceof InlineCastExpr) {
return toTypedExpressionFromMethodCallOrField(drlxExpr).getTypedExpression();
}
if (drlxExpr instanceof OOPathExpr) {
Class<?> type = patternType;
for (OOPathChunk chunk : ((OOPathExpr) drlxExpr).getChunks()) {
final String fieldName = chunk.getField().toString();
final TypedExpression callExpr = DrlxParseUtil.nameExprToMethodCallExpr(fieldName, type, null, ruleContext);
if (callExpr == null) {
return empty();
}
Class<?> fieldType = (chunk.getInlineCast() != null) ? DrlxParseUtil.getClassFromContext(ruleContext.getTypeResolver(), chunk.getInlineCast().toString()) : callExpr.getRawClass();
if (!chunk.isSingleValue() && Iterable.class.isAssignableFrom(fieldType) || isDataSource(fieldType)) {
type = extractGenericType(type, ((MethodCallExpr) callExpr.getExpression()).getName().toString());
} else {
type = fieldType;
}
}
return of(new TypedExpression(drlxExpr, type));
}
if (drlxExpr.isAssignExpr()) {
AssignExpr assignExpr = drlxExpr.asAssignExpr();
final Expression rightSide = assignExpr.getValue();
return toTypedExpressionRec(rightSide).map(e -> {
final AssignExpr newExpression = new AssignExpr(assignExpr.getTarget(), e.getExpression(), assignExpr.getOperator());
return new TypedExpression(newExpression, e.getType());
});
}
throw new UnsupportedOperationException();
}
use of org.drools.mvel.parser.ast.expr.OOPathChunk in project drools by kiegroup.
the class OOPathExprGenerator method visit.
public void visit(Class<?> originalClass, String originalBind, DrlxParseSuccess patternParseResult) {
final OOPathExpr ooPathExpr = (OOPathExpr) patternParseResult.getExpr();
Class<?> previousClass = originalClass;
String previousBind = originalBind;
NodeList<OOPathChunk> chunks = ooPathExpr.getChunks();
boolean passive = false;
for (int i = 0; i < chunks.size(); i++) {
OOPathChunk chunk = chunks.get(i);
if (chunk.isPassive()) {
if (passive) {
context.addCompilationError(new InvalidExpressionErrorResult("Invalid oopath expression '" + PrintUtil.printNode(ooPathExpr) + "': It is not possible to have 2 non-reactive parts in the same oopath"));
break;
}
passive = true;
}
final String fieldName = chunk.getField().toString();
final TypedExpression callExpr = DrlxParseUtil.nameExprToMethodCallExpr(fieldName, previousClass, null, context);
if (callExpr == null) {
context.addCompilationError(new InvalidExpressionErrorResult("Unknown field " + fieldName + " on " + previousClass));
break;
}
Class<?> fieldType = (chunk.getInlineCast() != null) ? DrlxParseUtil.getClassFromContext(context.getTypeResolver(), chunk.getInlineCast().toString()) : callExpr.getRawClass();
Type exprType = callExpr.getType();
Expression ooPathChunkExpr = prepend(new NameExpr(THIS_PLACEHOLDER), callExpr.getExpression());
if (Iterable.class.isAssignableFrom(fieldType) || isDataSource(fieldType)) {
if (chunk.isSingleValue()) {
ooPathChunkExpr = new MethodCallExpr(null, "java.util.Collections.singletonList", NodeList.nodeList(ooPathChunkExpr));
exprType = creteListParameterizedType(exprType);
} else {
fieldType = extractGenericType(previousClass, ((MethodCallExpr) callExpr.getExpression()).getName().toString());
}
}
final Expression accessorLambda = createLambdaAccessor(previousClass, exprType, ooPathChunkExpr);
final MethodCallExpr reactiveFrom = createFromExpr(previousBind, accessorLambda, passive);
previousBind = bindOOPathChunk(originalBind, patternParseResult, i, i == chunks.size() - 1, chunk, fieldName, fieldType, accessorLambda, reactiveFrom);
previousClass = fieldType;
}
}
use of org.drools.mvel.parser.ast.expr.OOPathChunk in project drools by kiegroup.
the class DroolsMvelParserTest method testOOPathExprWithBackReference.
@Test
public void testOOPathExprWithBackReference() {
String expr = "$toy : /wife/children/toys[name.length == ../../name.length]";
DrlxExpression drlx = parseExpression(parser, expr);
assertEquals("$toy", drlx.getBind().asString());
Expression expression = drlx.getExpr();
assertTrue(expression instanceof OOPathExpr);
final OOPathChunk secondChunk = ((OOPathExpr) expression).getChunks().get(2);
final BinaryExpr secondChunkFirstCondition = (BinaryExpr) secondChunk.getConditions().get(0).getExpr();
final DrlNameExpr rightName = (DrlNameExpr) ((FieldAccessExpr) secondChunkFirstCondition.getRight()).getScope();
assertEquals(2, rightName.getBackReferencesCount());
assertEquals(expr, printNode(drlx));
}
use of org.drools.mvel.parser.ast.expr.OOPathChunk in project drools by kiegroup.
the class ConstraintPrintVisitor method visit.
@Override
public void visit(OOPathExpr oopathExpr, Void arg) {
printComment(oopathExpr.getComment(), arg);
NodeList<OOPathChunk> chunks = oopathExpr.getChunks();
for (int i = 0; i < chunks.size(); i++) {
final OOPathChunk chunk = chunks.get(i);
printer.print(chunk.isSingleValue() ? "." : "/");
chunk.accept(this, arg);
printer.print(chunk.getField().toString());
if (chunk.getInlineCast() != null) {
printer.print("#");
chunk.getInlineCast().accept(this, arg);
}
List<DrlxExpression> condition = chunk.getConditions();
final Iterator<DrlxExpression> iterator = condition.iterator();
if (!condition.isEmpty()) {
printer.print("[");
DrlxExpression first = iterator.next();
first.accept(this, arg);
while (iterator.hasNext()) {
printer.print(",");
iterator.next().accept(this, arg);
}
printer.print("]");
}
}
}
Aggregations