use of org.drools.javaparser.ast.Node 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.Node in project drools by kiegroup.
the class AccumulateVisitorPatternDSL method addBindAsLastChainCall.
private void addBindAsLastChainCall(MethodCallExpr newBindingExpression, MethodCallExpr pattern) {
final Expression newScope = (Expression) pattern.getParentNode().orElse(pattern);
final Optional<Node> optParent = newScope.getParentNode();
newBindingExpression.setScope(newScope);
optParent.ifPresent(parent -> parent.replace(newScope, newBindingExpression));
}
use of org.drools.javaparser.ast.Node in project drools by kiegroup.
the class DrlxParseUtil method removeRootNode.
public static RemoveRootNodeResult removeRootNode(Expression expr) {
Optional<Expression> rootNode = findRootNodeViaScope(expr);
if (rootNode.isPresent()) {
Expression root = rootNode.get();
Optional<Node> parent = root.getParentNode();
parent.ifPresent(p -> p.remove(root));
return new RemoveRootNodeResult(rootNode, (Expression) parent.orElse(expr));
}
return new RemoveRootNodeResult(rootNode, expr);
}
use of org.drools.javaparser.ast.Node in project drools by kiegroup.
the class ExpressionTyper method nameExpr.
private Optional<TypedExpressionCursor> nameExpr(Expression drlxExpr, NameExpr firstNode, boolean isInLineCast, Class<?> originalTypeCursor) {
Optional<TypedExpressionCursor> teCursor;
String firstName = firstNode.getName().getIdentifier();
Optional<DeclarationSpec> declarationById = ruleContext.getDeclarationById(firstName);
if (declarationById.isPresent()) {
// do NOT append any reactOnProperties.
// because reactOnProperties is referring only to the properties of the type of the pattern, not other declarations properites.
context.addUsedDeclarations(firstName);
final Class<?> typeCursor;
if (!isInLineCast) {
typeCursor = declarationById.get().getDeclarationClass();
} else {
typeCursor = originalTypeCursor;
}
teCursor = of(new TypedExpressionCursor(new NameExpr(firstName), typeCursor));
} else if (packageModel.getGlobals().containsKey(firstName)) {
context.addUsedDeclarations(firstName);
return of(new TypedExpressionCursor(new NameExpr(firstName), packageModel.getGlobals().get(firstName)));
} else {
final Class<?> typeCursor;
// In OOPath a declaration is based on a position rather than a name.
// Only an OOPath chunk can have a backreference expression
Optional<DeclarationSpec> backReference = Optional.empty();
if (firstNode.getBackReferencesCount() > 0) {
List<DeclarationSpec> ooPathDeclarations = ruleContext.getOOPathDeclarations();
DeclarationSpec backReferenceDeclaration = ooPathDeclarations.get(ooPathDeclarations.size() - 1 - firstNode.getBackReferencesCount());
typeCursor = backReferenceDeclaration.getDeclarationClass();
backReference = of(backReferenceDeclaration);
context.addUsedDeclarations(backReferenceDeclaration.getBindingId());
} else {
typeCursor = originalTypeCursor;
}
Method firstAccessor = ClassUtils.getAccessor((!isInLineCast) ? typeCursor : patternType, firstName);
if (firstAccessor != null) {
// Hack to review - if a property is upper case it's probably not a react on property
if (!"".equals(firstName) && Character.isLowerCase(firstName.charAt(0))) {
context.addReactOnProperties(firstName);
}
final Class<?> typeOfFirstAccessor;
if (!isInLineCast) {
typeOfFirstAccessor = firstAccessor.getReturnType();
} else {
typeOfFirstAccessor = typeCursor;
}
NameExpr thisAccessor = new NameExpr("_this");
final NameExpr scope = backReference.map(d -> new NameExpr(d.getBindingId())).orElse(thisAccessor);
teCursor = of(new TypedExpressionCursor(new MethodCallExpr(scope, firstAccessor.getName()), typeOfFirstAccessor));
} else {
try {
Class<?> resolvedType = ruleContext.getTypeResolver().resolveType(firstName);
return of(new TypedExpressionCursor(null, resolvedType));
} catch (ClassNotFoundException e) {
// ignore
}
final Optional<Node> rootNode = findRootNodeViaParent(drlxExpr);
rootNode.ifPresent(n -> {
// In the error messages HalfBinary are transformed to Binary
Node withHalfBinaryReplaced = replaceAllHalfBinaryChildren(n);
ruleContext.addCompilationError(new ParseExpressionErrorResult((Expression) withHalfBinaryReplaced));
});
teCursor = Optional.empty();
}
}
return teCursor;
}
Aggregations