Search in sources :

Example 11 with SimpleName

use of com.github.javaparser.ast.expr.SimpleName in project drools by kiegroup.

the class FlattenScopeTest method flattenFields.

@Test
public void flattenFields() {
    List<Node> actual = flattenScope(MockTypeResolver.INSTANCE, expr("Field.INT"));
    List<Node> expected = asList(new NameExpr("Field"), new SimpleName("INT"));
    compareArrays(actual, expected);
}
Also used : Node(com.github.javaparser.ast.Node) SimpleName(com.github.javaparser.ast.expr.SimpleName) NameExpr(com.github.javaparser.ast.expr.NameExpr) Test(org.junit.Test)

Example 12 with SimpleName

use of com.github.javaparser.ast.expr.SimpleName in project drools by kiegroup.

the class PackageModel method buildRulesField.

private MethodCallExpr buildRulesField(ClassOrInterfaceDeclaration rulesClass) {
    MethodCallExpr rulesInit = new MethodCallExpr(null, "java.util.Arrays.asList");
    ClassOrInterfaceType rulesType = new ClassOrInterfaceType(null, new SimpleName("java.util.List"), new NodeList<Type>(toClassOrInterfaceType(Rule.class)));
    MethodDeclaration rulesGetter = new MethodDeclaration(NodeList.nodeList(publicModifier()), rulesType, "getRulesList");
    rulesGetter.createBody().addStatement(new ReturnStmt(rulesInit));
    rulesClass.addMember(rulesGetter);
    return rulesInit;
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SimpleName(com.github.javaparser.ast.expr.SimpleName) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 13 with SimpleName

use of com.github.javaparser.ast.expr.SimpleName in project drools by kiegroup.

the class DroolsMvelParserTest method testBindingOnRight.

@Test
public void testBindingOnRight() {
    String expr = "$n : name == \"Mario\" && $a : age > 20";
    DrlxExpression drlxExpression = parseExpression(parser, expr);
    Expression bExpr = drlxExpression.getExpr();
    assertTrue(bExpr instanceof BinaryExpr);
    Node left = ((BinaryExpr) bExpr).getLeft();
    assertTrue(left instanceof DrlxExpression);
    DrlxExpression leftExpr = (DrlxExpression) left;
    SimpleName leftBind = leftExpr.getBind();
    assertEquals("$n", leftBind.asString());
    Expression expression = leftExpr.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 DrlxExpression);
    DrlxExpression rightExpr = (DrlxExpression) right;
    SimpleName rightBind = rightExpr.getBind();
    assertEquals("$a", rightBind.asString());
    BinaryExpr binaryExpr2 = ((BinaryExpr) rightExpr.getExpr());
    assertEquals("age", toString(binaryExpr2.getLeft()));
    assertEquals("20", toString(binaryExpr2.getRight()));
    assertEquals(Operator.GREATER, binaryExpr2.getOperator());
}
Also used : DrlxParser.parseExpression(org.drools.mvel.parser.DrlxParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) PrintUtil.printNode(org.drools.mvel.parser.printer.PrintUtil.printNode) Node(com.github.javaparser.ast.Node) SimpleName(com.github.javaparser.ast.expr.SimpleName) Test(org.junit.Test)

Example 14 with SimpleName

use of com.github.javaparser.ast.expr.SimpleName in project drools by kiegroup.

the class DroolsMvelParserTest method test3BindingOn3Conditions.

@Test
public void test3BindingOn3Conditions() {
    String expr = "$n : name == \"Mario\" && $a : age > 20 && $l : likes != null";
    DrlxExpression drlxExpression = parseExpression(parser, expr);
    Expression bExpr = drlxExpression.getExpr();
    assertTrue(bExpr instanceof BinaryExpr);
    Expression left = ((BinaryExpr) bExpr).getLeft();
    assertTrue(left instanceof BinaryExpr);
    BinaryExpr leftExpr = (BinaryExpr) left;
    DrlxExpression first = (DrlxExpression) leftExpr.getLeft();
    DrlxExpression second = (DrlxExpression) leftExpr.getRight();
    DrlxExpression third = (DrlxExpression) ((BinaryExpr) bExpr).getRight();
    SimpleName bind = first.getBind();
    assertEquals("$n", bind.asString());
    BinaryExpr binaryExpr = ((BinaryExpr) first.getExpr());
    assertEquals("name", toString(binaryExpr.getLeft()));
    assertEquals("\"Mario\"", toString(binaryExpr.getRight()));
    assertEquals(Operator.EQUALS, binaryExpr.getOperator());
    bind = second.getBind();
    assertEquals("$a", bind.asString());
    binaryExpr = ((BinaryExpr) second.getExpr());
    assertEquals("age", toString(binaryExpr.getLeft()));
    assertEquals("20", toString(binaryExpr.getRight()));
    assertEquals(Operator.GREATER, binaryExpr.getOperator());
    bind = third.getBind();
    assertEquals("$l", bind.asString());
    binaryExpr = ((BinaryExpr) third.getExpr());
    assertEquals("likes", toString(binaryExpr.getLeft()));
    assertEquals("null", toString(binaryExpr.getRight()));
    assertEquals(Operator.NOT_EQUALS, binaryExpr.getOperator());
}
Also used : DrlxParser.parseExpression(org.drools.mvel.parser.DrlxParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) SimpleName(com.github.javaparser.ast.expr.SimpleName) Test(org.junit.Test)

Example 15 with SimpleName

use of com.github.javaparser.ast.expr.SimpleName 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());
}
Also used : DrlxParser.parseExpression(org.drools.mvel.parser.DrlxParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) SimpleName(com.github.javaparser.ast.expr.SimpleName) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) Test(org.junit.Test)

Aggregations

SimpleName (com.github.javaparser.ast.expr.SimpleName)34 Test (org.junit.Test)17 Expression (com.github.javaparser.ast.expr.Expression)15 NameExpr (com.github.javaparser.ast.expr.NameExpr)11 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)9 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)9 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 HalfBinaryExpr (org.drools.mvel.parser.ast.expr.HalfBinaryExpr)8 Parameter (com.github.javaparser.ast.body.Parameter)7 NodeList (com.github.javaparser.ast.NodeList)6 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)6 Node (com.github.javaparser.ast.Node)5 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)5 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)5 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)5 HashMap (java.util.HashMap)5 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)5 CompilationUnit (com.github.javaparser.ast.CompilationUnit)4