use of com.github.javaparser.ast.Node in project drools by kiegroup.
the class AccumulateInline method parseAccumulatePattern.
void parseAccumulatePattern() {
PatternDescr pattern = accumulateDescr.getInputPattern();
if (pattern == null || pattern.getSource() == null) {
return;
}
PatternSourceDescr sourceDescr = pattern.getSource();
if (sourceDescr instanceof FromDescr) {
DeclarativeInvokerDescr invokerDescr = ((FromDescr) sourceDescr).getDataSource();
String mvelBlock = addCurlyBracesToBlock(addSemicolon(invokerDescr.getText()));
CompiledBlockResult fromCodeCompilationResult = mvelCompiler.compileStatement(mvelBlock);
BlockStmt fromBlock = fromCodeCompilationResult.statementResults();
for (Statement stmt : fromBlock.getStatements()) {
stmt.findAll(NameExpr.class).stream().map(Node::toString).filter(context::hasDeclaration).forEach(usedExternalDeclarations::add);
}
}
}
use of com.github.javaparser.ast.Node 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.Node in project drools by kiegroup.
the class DrlxParseUtil method findAllChildrenRecursiveRec.
private static void findAllChildrenRecursiveRec(List<Node> accumulator, Node e) {
for (Node child : e.getChildNodes()) {
accumulator.add(child);
findAllChildrenRecursiveRec(accumulator, child);
}
}
use of com.github.javaparser.ast.Node in project drools by kiegroup.
the class DrlxParseUtil method trasformHalfBinaryToBinary.
public static Expression trasformHalfBinaryToBinary(Expression drlxExpr) {
final Optional<Node> parent = drlxExpr.getParentNode();
if (drlxExpr instanceof HalfBinaryExpr && parent.isPresent()) {
HalfBinaryExpr halfBinaryExpr = (HalfBinaryExpr) drlxExpr;
Expression parentLeft = findLeftLeafOfNameExprTraversingParent(halfBinaryExpr);
Operator operator = toBinaryExprOperator(halfBinaryExpr.getOperator());
return new BinaryExpr(parentLeft, halfBinaryExpr.getRight(), operator);
}
return drlxExpr;
}
use of com.github.javaparser.ast.Node in project drools by kiegroup.
the class FlattenScopeTest method flattenArrayAccess.
@Test
public void flattenArrayAccess() {
List<Node> actual = flattenScope(MockTypeResolver.INSTANCE, expr("$p.getChildrenA()[0]"));
NameExpr name = new NameExpr("$p");
final MethodCallExpr mc = new MethodCallExpr(name, "getChildrenA", nodeList());
mc.setTypeArguments(NodeList.nodeList());
List<Node> expected = asList(name, mc, new ArrayAccessExpr(mc, new IntegerLiteralExpr(0)));
compareArrays(actual, expected);
}
Aggregations