Search in sources :

Example 6 with SwitchStmt

use of com.github.javaparser.ast.stmt.SwitchStmt in project drools by kiegroup.

the class SwitchStmtT method toJavaExpression.

@Override
public Node toJavaExpression() {
    SwitchStmt stmt = new SwitchStmt();
    stmt.setSelector((Expression) selector.toJavaExpression());
    stmt.setEntries(NodeList.nodeList(entries.stream().map(TypedExpression::toJavaExpression).map(SwitchEntry.class::cast).collect(Collectors.toList())));
    return stmt;
}
Also used : SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt)

Example 7 with SwitchStmt

use of com.github.javaparser.ast.stmt.SwitchStmt in project checker-framework by typetools.

the class DoubleJavaParserVisitor method visit.

@Override
public void visit(final SwitchStmt node1, final Node other) {
    SwitchStmt node2 = (SwitchStmt) other;
    defaultAction(node1, node2);
    visitLists(node1.getEntries(), node2.getEntries());
    node1.getSelector().accept(this, node2.getSelector());
}
Also used : SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt)

Example 8 with SwitchStmt

use of com.github.javaparser.ast.stmt.SwitchStmt in project drools by kiegroup.

the class PropagatorCompilerHandler method extractMethod.

private void extractMethod(SwitchEntry switchEntry) {
    String label = switchEntry.getLabels().stream().map(Node::toString).collect(Collectors.joining());
    SwitchStmt switchStatement = switchEntry.findAncestor(SwitchStmt.class).orElseThrow(() -> new CouldNotCreateAlphaNetworkCompilerException("SwitchEntry without SwitchStatement"));
    int index = switchStatement.getEntries().indexOf(switchEntry);
    String selectorString = switchStatement.getSelector().toString();
    String newMethodName = String.format("extractedPropagated_%s_%d", md5Hash(selectorString), index);
    // First statement is actual block, second statement is break
    BlockStmt switchEntryStatements = (BlockStmt) switchEntry.getStatements().get(0);
    MethodDeclaration extractedMethod = new MethodDeclaration().setModifiers(nodeList(Modifier.publicModifier())).setName(newMethodName).setParameters(methodParameters()).setType(new VoidType()).setBody(switchEntryStatements);
    extractedMethod.setComment(new LineComment(selectorString + " " + label));
    MethodCallExpr callExtractedMethod = new MethodCallExpr().setName(newMethodName).setArguments(arguments());
    switchEntry.setStatements(nodeList(new ExpressionStmt(callExtractedMethod), new BreakStmt()));
    extractedMethods.add(extractedMethod);
}
Also used : VoidType(com.github.javaparser.ast.type.VoidType) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BreakStmt(com.github.javaparser.ast.stmt.BreakStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) LineComment(com.github.javaparser.ast.comments.LineComment) IndexableConstraint(org.drools.core.rule.IndexableConstraint) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 9 with SwitchStmt

use of com.github.javaparser.ast.stmt.SwitchStmt in project drools by kiegroup.

the class PropagatorCompilerHandler method startRangeIndex.

@Override
public void startRangeIndex(AlphaRangeIndex alphaRangeIndex) {
    String rangeIndexVariableName = getRangeIndexVariableName(alphaRangeIndex, getMinIdFromRangeIndex(alphaRangeIndex));
    String matchingResultVariableName = rangeIndexVariableName + "_result";
    String matchingNodeVariableName = matchingResultVariableName + "_node";
    ExpressionStmt matchingResultVariable = localVariable(parseType("java.util.Collection<org.drools.core.reteoo.AlphaNode>"), matchingResultVariableName, new MethodCallExpr(new NameExpr(rangeIndexVariableName), "getMatchingAlphaNodes", nodeList(new MethodCallExpr(new NameExpr(FACT_HANDLE_PARAM_NAME), "getObject"))));
    final BlockStmt currentBlockStatement = getCurrentBlockStatement();
    currentBlockStatement.addStatement(matchingResultVariable);
    BlockStmt body = new BlockStmt();
    ForEachStmt forEachStmt = new ForEachStmt(new VariableDeclarationExpr(parseType("org.drools.core.reteoo.AlphaNode"), matchingNodeVariableName), new NameExpr(matchingResultVariableName), body);
    currentBlockStatement.addStatement(forEachStmt);
    SwitchStmt switchStatement = new SwitchStmt().setSelector(new MethodCallExpr(new NameExpr(matchingNodeVariableName), "getId"));
    this.currentStatement.push(switchStatement);
    body.addStatement(switchStatement);
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) ForEachStmt(com.github.javaparser.ast.stmt.ForEachStmt) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 10 with SwitchStmt

use of com.github.javaparser.ast.stmt.SwitchStmt in project drools by kiegroup.

the class SetNodeReferenceHandler method generateSwitchBody.

private void generateSwitchBody(BlockStmt switchBodyStatements, List<NetworkNode> subNodes) {
    SwitchStmt switchStmt = new SwitchStmt();
    switchStmt.setSelector(parseExpression("node.getId()"));
    NodeList<SwitchEntry> entries = new NodeList<>();
    for (NetworkNode n : subNodes) {
        String assignStatementString;
        if (n instanceof AlphaNode) {
            assignStatementString = getVariableAssignmentStatementAlphaNode((AlphaNode) n);
        } else {
            assignStatementString = getVariableAssignmentStatement(n);
        }
        Statement assignStmt = parseStatement(assignStatementString);
        SwitchEntry se = new SwitchEntry(nodeList(new IntegerLiteralExpr(n.getId())), SwitchEntry.Type.STATEMENT_GROUP, nodeList(assignStmt, new ReturnStmt(new BooleanLiteralExpr(true))));
        entries.add(se);
    }
    switchStmt.setEntries(entries);
    switchBodyStatements.addStatement(switchStmt);
    switchBodyStatements.addStatement(new ReturnStmt(new BooleanLiteralExpr(false)));
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) Statement(com.github.javaparser.ast.stmt.Statement) NodeList(com.github.javaparser.ast.NodeList) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry) NetworkNode(org.drools.core.common.NetworkNode) AlphaNode(org.drools.core.reteoo.AlphaNode) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt)

Aggregations

SwitchStmt (com.github.javaparser.ast.stmt.SwitchStmt)17 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)9 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)6 SwitchEntry (com.github.javaparser.ast.stmt.SwitchEntry)6 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)4 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)4 NameExpr (com.github.javaparser.ast.expr.NameExpr)4 Statement (com.github.javaparser.ast.stmt.Statement)4 CompilationUnit (com.github.javaparser.ast.CompilationUnit)3 NodeList (com.github.javaparser.ast.NodeList)3 Expression (com.github.javaparser.ast.expr.Expression)3 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)3 StaticJavaParser.parseStatement (com.github.javaparser.StaticJavaParser.parseStatement)2 Node (com.github.javaparser.ast.Node)2 IntegerLiteralExpr (com.github.javaparser.ast.expr.IntegerLiteralExpr)2 ForEachStmt (com.github.javaparser.ast.stmt.ForEachStmt)2 IfStmt (com.github.javaparser.ast.stmt.IfStmt)2 SwitchEntryStmt (com.github.javaparser.ast.stmt.SwitchEntryStmt)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2