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;
}
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());
}
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);
}
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);
}
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)));
}
Aggregations