use of org.drools.mvel.parser.ast.expr.DrlxExpression in project drools by kiegroup.
the class FromVisitor method fromFieldOrName.
private Optional<Expression> fromFieldOrName(String expression) {
Optional<String> optContainsBinding = DrlxParseUtil.findBindingIdFromDotExpression(expression);
final String bindingId = optContainsBinding.orElse(expression);
final DrlxExpression drlxExpression = DrlxParseUtil.parseExpression(expression);
final Expression parsedExpression = drlxExpression.getExpr();
Optional<TypedExpression> staticField = parsedExpression instanceof FieldAccessExpr ? ExpressionTyper.tryParseAsConstantField(context.getTypeResolver(), ((FieldAccessExpr) parsedExpression).getScope(), ((FieldAccessExpr) parsedExpression).getNameAsString()) : Optional.empty();
if (staticField.isPresent()) {
return of(createSupplier(parsedExpression));
}
if (packageModel.hasEntryPoint(bindingId)) {
return of(createEntryPointCall(bindingId));
}
if (contextHasDeclaration(bindingId)) {
return of(createFromCall(expression, bindingId, optContainsBinding.isPresent(), null));
}
return of(createUnitDataCall(bindingId));
}
use of org.drools.mvel.parser.ast.expr.DrlxExpression in project drools by kiegroup.
the class ExpressionTyperTest method transformMethodExpressionToMethodCallWithInlineCast.
@Test
public void transformMethodExpressionToMethodCallWithInlineCast() {
typeResolver.addImport("org.drools.modelcompiler.domain.InternationalAddress");
final DrlxExpression expr = DrlxParseUtil.parseExpression("address#InternationalAddress.state");
final MethodCallExpr expected = StaticJavaParser.parseExpression("((org.drools.modelcompiler.domain.InternationalAddress)_this.getAddress()).getState()");
assertEquals(PrintUtil.printNode(expected), toTypedExpression(PrintUtil.printNode(expr.getExpr()), Person.class).getExpression().toString());
}
use of org.drools.mvel.parser.ast.expr.DrlxExpression in project drools by kiegroup.
the class ConstraintParser method drlxParse.
public DrlxParseResult drlxParse(Class<?> patternType, String bindingId, ConstraintExpression constraint, boolean isPositional) {
String constraintExpressionString = constraint.getExpression();
DrlxExpression drlx = DrlxParseUtil.parseExpression(constraintExpressionString);
boolean hasBind = drlx.getBind() != null;
DrlxParseResult drlxParseResult = compileStart(patternType, bindingId, constraint, drlx.getExpr(), hasBind, isPositional).setOriginalDrlConstraint(constraintExpressionString);
drlxParseResult.accept(result -> {
if (hasBind) {
SingleDrlxParseSuccess singleResult = (SingleDrlxParseSuccess) result;
String bindId = drlx.getBind().asString();
addDeclaration(drlx, singleResult, bindId);
} else if (result instanceof SingleDrlxParseSuccess) {
SingleDrlxParseSuccess singleResult = (SingleDrlxParseSuccess) result;
// a constraint has a binding inside its expression (not in top level DrlxExpression)
String bindId = singleResult.getExprBinding();
if (bindId != null) {
addDeclaration(drlx, singleResult, bindId);
}
}
});
return drlxParseResult;
}
use of org.drools.mvel.parser.ast.expr.DrlxExpression 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 org.drools.mvel.parser.ast.expr.DrlxExpression 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));
}
Aggregations