Search in sources :

Example 16 with SimpleName

use of com.github.javaparser.ast.expr.SimpleName 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());
}
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) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) Test(org.junit.Test)

Example 17 with SimpleName

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

the class SetNodeReferenceHandler method generateSwitchForSubNodes.

private MethodDeclaration generateSwitchForSubNodes(int partitionIndex, List<NetworkNode> subNodes, BlockStmt setNetworkNodeReferenceBody) {
    String switchMethodName = "setNetworkNode" + partitionIndex;
    BlockStmt callSwitchStatements = StaticJavaParser.parseBlock(switchStatementCall);
    callSwitchStatements.findAll(SimpleName.class, ne -> ne.toString().equals("setNetworkResultN")).forEach(n -> n.replace(new SimpleName("setNetworkResult" + partitionIndex)));
    callSwitchStatements.findAll(MethodCallExpr.class, mc -> mc.getNameAsString().equals("setNetworkNodeN")).forEach(n -> n.setName(new SimpleName("setNetworkNode" + partitionIndex)));
    callSwitchStatements.getStatements().forEach(setNetworkNodeReferenceBody::addStatement);
    MethodDeclaration switchMethod = new MethodDeclaration(nodeList(Modifier.privateModifier()), switchMethodName, PrimitiveType.booleanType(), nodeParameter());
    BlockStmt switchBodyStatements = switchMethod.getBody().orElseThrow(() -> new RuntimeException("No"));
    generateSwitchBody(switchBodyStatements, subNodes);
    return switchMethod;
}
Also used : SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) Parameter(com.github.javaparser.ast.body.Parameter) NodeList.nodeList(com.github.javaparser.ast.NodeList.nodeList) AlphaNode(org.drools.core.reteoo.AlphaNode) ArrayList(java.util.ArrayList) AbstractCompilerHandler.getVariableName(org.drools.ancompiler.AbstractCompilerHandler.getVariableName) StaticJavaParser.parseType(com.github.javaparser.StaticJavaParser.parseType) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) Sink(org.drools.core.reteoo.Sink) AbstractCompilerHandler.getVariableType(org.drools.ancompiler.AbstractCompilerHandler.getVariableType) NodeList(com.github.javaparser.ast.NodeList) SimpleName(com.github.javaparser.ast.expr.SimpleName) StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) VoidType(com.github.javaparser.ast.type.VoidType) NetworkNode(org.drools.core.common.NetworkNode) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) Statement(com.github.javaparser.ast.stmt.Statement) Modifier(com.github.javaparser.ast.Modifier) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) SimpleName(com.github.javaparser.ast.expr.SimpleName) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 18 with SimpleName

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

the class InlineFieldReferenceInitHandler method generateInitMethodForPartition.

private MethodDeclaration generateInitMethodForPartition(int partitionIndex, List<NetworkNode> nodeInPartition, BlockStmt setNetworkNodeReferenceBody) {
    String initWithIndex = "initNode" + partitionIndex;
    CompilationUnit initialisationCompilationUnit = new CompilationUnit();
    initialisationCompilationUnit.setPackageDeclaration(ObjectTypeNodeCompiler.PACKAGE_NAME);
    initialisationCompilationUnit.addClass(ucFirst(initWithIndex));
    partitionedNodeInitialisationClasses.put(partitionIndex, initialisationCompilationUnit);
    BlockStmt setFieldStatementCall = StaticJavaParser.parseBlock(statementCall);
    setFieldStatementCall.findAll(MethodCallExpr.class, mc -> mc.getNameAsString().equals("initNodeN")).forEach(n -> n.setName(new SimpleName(initWithIndex)));
    setFieldStatementCall.getStatements().forEach(setNetworkNodeReferenceBody::addStatement);
    MethodDeclaration initMethodWithIndex = new MethodDeclaration(nodeList(Modifier.publicModifier()), new VoidType(), initWithIndex);
    BlockStmt initBlockPerPartition = initMethodWithIndex.getBody().orElseThrow(() -> new RuntimeException("No"));
    generateInitBody(initBlockPerPartition, nodeInPartition, partitionIndex);
    return initMethodWithIndex;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) Parameter(com.github.javaparser.ast.body.Parameter) NodeList.nodeList(com.github.javaparser.ast.NodeList.nodeList) HashMap(java.util.HashMap) AlphaNode(org.drools.core.reteoo.AlphaNode) ArrayList(java.util.ArrayList) AbstractCompilerHandler.getVariableName(org.drools.ancompiler.AbstractCompilerHandler.getVariableName) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Sink(org.drools.core.reteoo.Sink) AbstractCompilerHandler.getVariableType(org.drools.ancompiler.AbstractCompilerHandler.getVariableType) NodeList(com.github.javaparser.ast.NodeList) SimpleName(com.github.javaparser.ast.expr.SimpleName) StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) Collection(java.util.Collection) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) VoidType(com.github.javaparser.ast.type.VoidType) NetworkNode(org.drools.core.common.NetworkNode) StringUtils.ucFirst(org.drools.core.util.StringUtils.ucFirst) Collectors(java.util.stream.Collectors) Modifier(com.github.javaparser.ast.Modifier) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) VoidType(com.github.javaparser.ast.type.VoidType) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) SimpleName(com.github.javaparser.ast.expr.SimpleName) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 19 with SimpleName

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

the class NodeTest method findParent.

@Test
public void findParent() {
    CompilationUnit cu = parse("class X{int x;}");
    SimpleName x = cu.getClassByName("X").get().getMember(0).asFieldDeclaration().getVariables().get(0).getName();
    assertEquals("int x;", x.findParent(FieldDeclaration.class).get().toString());
}
Also used : SimpleName(com.github.javaparser.ast.expr.SimpleName) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) Test(org.junit.Test)

Example 20 with SimpleName

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

the class AnnotationMemberDeclarationTest method whenSettingNameTheParentOfNameIsAssigned.

@Test
public void whenSettingNameTheParentOfNameIsAssigned() {
    AnnotationMemberDeclaration decl = new AnnotationMemberDeclaration();
    SimpleName name = new SimpleName("foo");
    decl.setName(name);
    assertTrue(name.getParentNode().isPresent());
    assertTrue(decl == name.getParentNode().get());
}
Also used : SimpleName(com.github.javaparser.ast.expr.SimpleName) 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