use of org.drools.core.common.NetworkNode in project drools by kiegroup.
the class SetNodeReferenceHandler method emitCode.
public void emitCode(StringBuilder builder) {
List<MethodDeclaration> allMethods = new ArrayList<>();
MethodDeclaration methodDeclaration = new MethodDeclaration(nodeList(Modifier.protectedModifier()), METHOD_NAME, new VoidType(), nodeParameter());
allMethods.add(methodDeclaration);
BlockStmt setNetworkNodeReference = methodDeclaration.getBody().orElseThrow(() -> new RuntimeException("No block statement"));
List<List<NetworkNode>> partitionedNodes = ListUtils.partition(nodes, 20);
for (int i = 0; i < partitionedNodes.size(); i++) {
List<NetworkNode> subNodes = partitionedNodes.get(i);
MethodDeclaration m = generateSwitchForSubNodes(i, subNodes, setNetworkNodeReference);
allMethods.add(m);
}
for (MethodDeclaration md : allMethods) {
builder.append(md.toString());
builder.append("\n");
}
}
use of org.drools.core.common.NetworkNode 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)));
}
use of org.drools.core.common.NetworkNode 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;
}
use of org.drools.core.common.NetworkNode 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;
}
use of org.drools.core.common.NetworkNode in project drools by kiegroup.
the class InlineFieldReferenceInitHandler method emitCode.
public void emitCode(StringBuilder builder) {
List<MethodDeclaration> allMethods = new ArrayList<>();
MethodDeclaration methodDeclaration = new MethodDeclaration(nodeList(Modifier.publicModifier()), new VoidType(), METHOD_NAME);
allMethods.add(methodDeclaration);
BlockStmt setNetworkNodeReference = methodDeclaration.getBody().orElseThrow(() -> new RuntimeException("No block statement"));
List<List<NetworkNode>> partitionedNodes = ListUtils.partition(nodes, 20);
for (int i = 0; i < partitionedNodes.size(); i++) {
List<NetworkNode> nodesInPartition = partitionedNodes.get(i);
MethodDeclaration m = generateInitMethodForPartition(i, nodesInPartition, setNetworkNodeReference);
allMethods.add(m);
}
for (MethodDeclaration md : allMethods) {
builder.append(md.toString());
builder.append("\n");
}
}
Aggregations