use of com.github.javaparser.ast.expr.EnclosedExpr in project drools by kiegroup.
the class AccumulateInline method parseResultMethod.
private void parseResultMethod() {
// <result expression>: this is a semantic expression in the selected dialect that is executed after all source objects are iterated.
MethodDeclaration resultMethod = getMethodFromTemplateClass("getResult");
Type returnExpressionType = toClassOrInterfaceType(java.lang.Object.class);
Expression returnExpression = StaticJavaParser.parseExpression(accumulateDescr.getResultCode());
if (returnExpression instanceof NameExpr) {
returnExpression = new EnclosedExpr(returnExpression);
}
rescopeNamesToNewScope(getDataNameExpr(), contextFieldNames, returnExpression);
resultMethod.getBody().orElseThrow(InvalidInlineTemplateException::new).addStatement(new ReturnStmt(returnExpression));
MethodDeclaration getResultTypeMethod = getMethodFromTemplateClass("getResultType");
getResultTypeMethod.getBody().orElseThrow(InvalidInlineTemplateException::new).addStatement(new ReturnStmt(new ClassExpr(returnExpressionType)));
}
use of com.github.javaparser.ast.expr.EnclosedExpr in project drools by kiegroup.
the class FlattenScope method flattenScope.
public static List<Node> flattenScope(TypeResolver typeResolver, Expression expressionWithScope) {
List<Node> res = new ArrayList<>();
if (expressionWithScope instanceof FullyQualifiedInlineCastExpr) {
res.addAll(flattenScope(typeResolver, transformFullyQualifiedInlineCastExpr(typeResolver, (FullyQualifiedInlineCastExpr) expressionWithScope)));
} else if (expressionWithScope instanceof FieldAccessExpr) {
FieldAccessExpr fieldAccessExpr = (FieldAccessExpr) expressionWithScope;
res.addAll(flattenScope(typeResolver, fieldAccessExpr.getScope()));
res.add(fieldAccessExpr.getName());
} else if (expressionWithScope instanceof NullSafeFieldAccessExpr) {
NullSafeFieldAccessExpr fieldAccessExpr = (NullSafeFieldAccessExpr) expressionWithScope;
res.addAll(flattenScope(typeResolver, fieldAccessExpr.getScope()));
res.add(fieldAccessExpr.getName());
} else if (expressionWithScope instanceof MethodCallExpr) {
MethodCallExpr methodCallExpr = (MethodCallExpr) expressionWithScope;
if (methodCallExpr.getScope().isPresent()) {
Expression scope = methodCallExpr.getScope().get();
if (isFullyQualifiedClassName(typeResolver, scope)) {
res.add(scope);
} else {
res.addAll(flattenScope(typeResolver, scope));
}
}
res.add(methodCallExpr);
} else if (expressionWithScope instanceof NullSafeMethodCallExpr) {
NullSafeMethodCallExpr methodCallExpr = (NullSafeMethodCallExpr) expressionWithScope;
if (methodCallExpr.getScope().isPresent()) {
res.addAll(flattenScope(typeResolver, methodCallExpr.getScope().orElseThrow(() -> new IllegalStateException("Scope expression is not present!"))));
}
res.add(methodCallExpr);
} else if (expressionWithScope instanceof InlineCastExpr && ((InlineCastExpr) expressionWithScope).getExpression() instanceof FieldAccessExpr) {
InlineCastExpr inlineCastExpr = (InlineCastExpr) expressionWithScope;
Expression internalScope = ((FieldAccessExpr) inlineCastExpr.getExpression()).getScope();
res.addAll(flattenScope(typeResolver, internalScope));
res.add(expressionWithScope);
} else if (expressionWithScope instanceof ArrayAccessExpr) {
ArrayAccessExpr arrayAccessExpr = (ArrayAccessExpr) expressionWithScope;
res.addAll(flattenScope(typeResolver, arrayAccessExpr.getName()));
res.add(arrayAccessExpr);
} else if (expressionWithScope instanceof EnclosedExpr) {
res.addAll(flattenScope(typeResolver, ((EnclosedExpr) expressionWithScope).getInner()));
} else {
res.add(expressionWithScope);
}
return res;
}
use of com.github.javaparser.ast.expr.EnclosedExpr in project drools by kiegroup.
the class AstUtils method transformHalfBinaryArg.
private static Expression transformHalfBinaryArg(TokenRange tokenRange, Expression scope, Expression name, Expression expr, boolean nullSafe) {
if (expr instanceof HalfBinaryExpr) {
Expression left = scope == null ? name : (nullSafe ? new NullSafeFieldAccessExpr(scope, null, name.asNameExpr().getName()) : new FieldAccessExpr(scope, null, name.asNameExpr().getName()));
return new BinaryExpr(tokenRange, left, ((HalfBinaryExpr) expr).getRight(), ((HalfBinaryExpr) expr).getOperator().toBinaryExprOperator());
}
if (expr instanceof EnclosedExpr) {
return transformHalfBinaryArg(tokenRange, scope, name, ((EnclosedExpr) expr).getInner(), nullSafe);
}
if (expr instanceof BinaryExpr) {
BinaryExpr binary = (BinaryExpr) expr;
Expression rewrittenLeft = transformHalfBinaryArg(tokenRange, scope, name, binary.getLeft(), nullSafe);
Expression rewrittenRight = binary.getRight() instanceof HalfBinaryExpr && !(binary.getLeft() instanceof EnclosedExpr) ? binary.getRight() : transformHalfBinaryArg(tokenRange, scope, name, binary.getRight(), nullSafe);
rewrittenRight.setParentNode(rewrittenLeft);
return new BinaryExpr(tokenRange, rewrittenLeft, rewrittenRight, binary.getOperator());
}
throw new IllegalStateException();
}
use of com.github.javaparser.ast.expr.EnclosedExpr in project drools by kiegroup.
the class DrlxParseUtil method findRootNodeViaScopeRec.
private static RemoveRootNodeResult findRootNodeViaScopeRec(Expression expr, LinkedList<Expression> acc) {
if (expr.isArrayAccessExpr()) {
throw new RuntimeException("This doesn't work on arrayAccessExpr convert them to a method call");
}
if (expr instanceof EnclosedExpr) {
return findRootNodeViaScopeRec(expr.asEnclosedExpr().getInner(), acc);
} else if (expr instanceof CastExpr) {
return findRootNodeViaScopeRec(expr.asCastExpr().getExpression(), acc);
} else if (expr instanceof ThisExpr) {
return new RemoveRootNodeResult(Optional.of(expr), expr, expr);
} else if (expr instanceof NodeWithTraversableScope) {
final NodeWithTraversableScope exprWithScope = (NodeWithTraversableScope) expr;
return exprWithScope.traverseScope().flatMap((Expression scope) -> {
if (isDslTopLevelNamespace(scope)) {
return empty();
}
Expression sanitizedExpr = DrlxParseUtil.transformDrlNameExprToNameExpr(expr);
acc.addLast(sanitizedExpr.clone());
return of(findRootNodeViaScopeRec(scope, acc));
}).orElse(new RemoveRootNodeResult(Optional.of(expr), expr, acc.isEmpty() ? expr : acc.getLast()));
} else if (expr instanceof NameExpr) {
if (!acc.isEmpty() && acc.getLast() instanceof NodeWithOptionalScope<?>) {
((NodeWithOptionalScope<?>) acc.getLast()).setScope(null);
for (ListIterator<Expression> iterator = acc.listIterator(); iterator.hasNext(); ) {
Expression e = iterator.next();
if (e instanceof NodeWithOptionalScope) {
NodeWithOptionalScope<?> node = (NodeWithOptionalScope<?>) e;
if (iterator.hasNext()) {
node.setScope(acc.get(iterator.nextIndex()));
}
}
}
return new RemoveRootNodeResult(Optional.of(expr), acc.getFirst(), acc.getLast());
} else {
return new RemoveRootNodeResult(Optional.of(expr), expr, expr);
}
}
return new RemoveRootNodeResult(empty(), expr, expr);
}
use of com.github.javaparser.ast.expr.EnclosedExpr in project drools by kiegroup.
the class ConstraintUtil method normalizeConstraint.
/**
* Swap left and right operands in a constraint when a fact property is located on the right side.
*
* e.g. Person(20 < age) should be normalized to Person(age > 20)
*
* @param drlxParseResult
* @return Normalized <code>DrlxParseResult</code>
*/
public static DrlxParseResult normalizeConstraint(DrlxParseResult drlxParseResult) {
if (!ENABLE_NORMALIZE) {
return drlxParseResult;
}
if (drlxParseResult instanceof SingleDrlxParseSuccess) {
// Create a copy
SingleDrlxParseSuccess drlx = new SingleDrlxParseSuccess((SingleDrlxParseSuccess) drlxParseResult);
Expression expr = drlx.getExpr();
if (expr == null) {
return drlxParseResult;
}
if (expr instanceof MethodCallExpr) {
processTopLevelExpression(drlx, (MethodCallExpr) expr);
} else if (expr instanceof EnclosedExpr) {
Expression inner = stripEnclosedExpr((EnclosedExpr) expr);
if (inner instanceof MethodCallExpr) {
processTopLevelExpression(drlx, (MethodCallExpr) inner);
} else {
processExpression(expr);
}
} else {
processExpression(expr);
}
return drlx;
}
return drlxParseResult;
}
Aggregations