use of com.github.javaparser.ast.expr.EnclosedExpr in project drools by kiegroup.
the class ConstraintUtil method processExpression.
private static void processExpression(Expression expr) {
if (expr instanceof MethodCallExpr) {
MethodCallExpr mcExpr = (MethodCallExpr) expr;
if (canInverse(mcExpr)) {
inverseMethodCallExpr(mcExpr);
}
} else if (expr instanceof BinaryExpr) {
BinaryExpr bExpr = (BinaryExpr) expr;
if (bExpr.getOperator() == BinaryExpr.Operator.AND || bExpr.getOperator() == BinaryExpr.Operator.OR) {
Expression left = bExpr.getLeft();
processExpression(left);
Expression right = bExpr.getRight();
processExpression(right);
}
} else if (expr instanceof UnaryExpr) {
Expression expression = ((UnaryExpr) expr).getExpression();
processExpression(expression);
} else if (expr instanceof EnclosedExpr) {
Expression inner = ((EnclosedExpr) expr).getInner();
processExpression(inner);
}
}
use of com.github.javaparser.ast.expr.EnclosedExpr in project drools by kiegroup.
the class DroolsMvelParserTest method testEnclosedBindVariable.
@Test
public void testEnclosedBindVariable() {
String expr = "($n : name == \"Mario\")";
DrlxExpression drlxExpression = parseExpression(parser, expr);
Expression enclosedExpr = drlxExpression.getExpr();
assertTrue(enclosedExpr instanceof EnclosedExpr);
Expression inner = ((EnclosedExpr) enclosedExpr).getInner();
assertTrue(inner instanceof DrlxExpression);
DrlxExpression innerDrlxExpression = (DrlxExpression) inner;
SimpleName bind = innerDrlxExpression.getBind();
assertEquals("$n", bind.asString());
Expression expression = innerDrlxExpression.getExpr();
BinaryExpr binaryExpr = ((BinaryExpr) expression);
assertEquals("name", toString(binaryExpr.getLeft()));
assertEquals("\"Mario\"", toString(binaryExpr.getRight()));
assertEquals(Operator.EQUALS, binaryExpr.getOperator());
}
use of com.github.javaparser.ast.expr.EnclosedExpr in project drools by kiegroup.
the class DroolsMvelParserTest method testComplexEnclosedBindVariable.
@Test
public void testComplexEnclosedBindVariable() {
String expr = "($n : name == \"Mario\") && (age > 20)";
DrlxExpression drlxExpression = parseExpression(parser, expr);
Expression bExpr = drlxExpression.getExpr();
assertTrue(bExpr instanceof BinaryExpr);
Node left = ((BinaryExpr) bExpr).getLeft();
assertTrue(left instanceof EnclosedExpr);
Expression inner = ((EnclosedExpr) left).getInner();
assertTrue(inner instanceof DrlxExpression);
DrlxExpression innerDrlxExpression = (DrlxExpression) inner;
SimpleName bind = innerDrlxExpression.getBind();
assertEquals("$n", bind.asString());
Expression expression = innerDrlxExpression.getExpr();
BinaryExpr binaryExpr = ((BinaryExpr) expression);
assertEquals("name", toString(binaryExpr.getLeft()));
assertEquals("\"Mario\"", toString(binaryExpr.getRight()));
assertEquals(Operator.EQUALS, binaryExpr.getOperator());
Node right = ((BinaryExpr) bExpr).getRight();
assertTrue(right instanceof EnclosedExpr);
Expression expression2 = ((EnclosedExpr) right).getInner();
BinaryExpr binaryExpr2 = ((BinaryExpr) expression2);
assertEquals("age", toString(binaryExpr2.getLeft()));
assertEquals("20", toString(binaryExpr2.getRight()));
assertEquals(Operator.GREATER, binaryExpr2.getOperator());
}
use of com.github.javaparser.ast.expr.EnclosedExpr in project drools by kiegroup.
the class ASTCompilerVisitor method visit.
@Override
public DirectCompilerResult visit(IfExpressionNode n) {
DirectCompilerResult condition = n.getCondition().accept(this);
DirectCompilerResult thenExpr = n.getThenExpression().accept(this);
DirectCompilerResult elseExpr = n.getElseExpression().accept(this);
return DirectCompilerResult.of(new ConditionalExpr(new BinaryExpr(Expressions.nativeInstanceOf(Constants.BooleanT, condition.getExpression()), Expressions.reflectiveCastTo(Constants.BooleanT, condition.getExpression()), BinaryExpr.Operator.AND), new EnclosedExpr(thenExpr.getExpression()), new EnclosedExpr(elseExpr.getExpression())), // should find common type between then/else
thenExpr.resultType).withFD(condition).withFD(thenExpr).withFD(elseExpr);
}
use of com.github.javaparser.ast.expr.EnclosedExpr in project drools by kiegroup.
the class CompilerBytecodeLoader method getCompilationUnit.
public <T> CompilationUnit getCompilationUnit(Class<T> clazz, String templateResourcePath, String cuPackage, String cuClass, String feelExpression, Expression theExpression, Set<FieldDeclaration> fieldDeclarations) {
CompilationUnit cu = parse(CompilerBytecodeLoader.class.getResourceAsStream(templateResourcePath));
cu.setPackageDeclaration(cuPackage);
final String className = templateResourcePath.substring(1, templateResourcePath.length() - 5);
ClassOrInterfaceDeclaration classSource = cu.getClassByName(className).orElseThrow(() -> new IllegalArgumentException("Cannot find class by name " + className));
classSource.setName(cuClass);
MethodDeclaration lookupMethod = cu.findFirst(MethodDeclaration.class).orElseThrow(() -> new RuntimeException("Something unexpected changed in the template."));
lookupMethod.setComment(new JavadocComment(" FEEL: " + feelExpression + " "));
ReturnStmt returnStmt = lookupMethod.findFirst(ReturnStmt.class).orElseThrow(() -> new RuntimeException("Something unexpected changed in the template."));
Expression expr;
if (clazz.equals(CompiledFEELUnaryTests.class)) {
expr = new CastExpr(StaticJavaParser.parseType("java.util.List"), new EnclosedExpr(theExpression));
} else {
expr = theExpression;
}
returnStmt.setExpression(expr);
List<ClassOrInterfaceDeclaration> classDecls = cu.findAll(ClassOrInterfaceDeclaration.class);
if (classDecls.size() != 1) {
throw new RuntimeException("Something unexpected changed in the template.");
}
ClassOrInterfaceDeclaration classDecl = classDecls.get(0);
fieldDeclarations.stream().filter(fd -> !isUnaryTest(fd)).sorted(new SortFieldDeclarationStrategy()).forEach(classDecl::addMember);
fieldDeclarations.stream().filter(fd -> fd.getVariable(0).getName().asString().startsWith("UT")).sorted(new SortFieldDeclarationStrategy()).forEach(classDecl::addMember);
if (generateClassListener != null) {
generateClassListener.generatedClass(cu);
}
LOG.debug("{}", cu);
return cu;
}
Aggregations