Search in sources :

Example 6 with NetworkNode

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");
    }
}
Also used : VoidType(com.github.javaparser.ast.type.VoidType) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ArrayList(java.util.ArrayList) NodeList.nodeList(com.github.javaparser.ast.NodeList.nodeList) ArrayList(java.util.ArrayList) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) NetworkNode(org.drools.core.common.NetworkNode)

Example 7 with NetworkNode

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

Example 8 with NetworkNode

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;
}
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 9 with NetworkNode

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;
}
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 10 with NetworkNode

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");
    }
}
Also used : VoidType(com.github.javaparser.ast.type.VoidType) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ArrayList(java.util.ArrayList) NodeList.nodeList(com.github.javaparser.ast.NodeList.nodeList) ArrayList(java.util.ArrayList) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) NetworkNode(org.drools.core.common.NetworkNode)

Aggregations

NetworkNode (org.drools.core.common.NetworkNode)19 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)6 NodeList (com.github.javaparser.ast.NodeList)5 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)5 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)5 SegmentMemory (org.drools.core.reteoo.SegmentMemory)5 NodeList.nodeList (com.github.javaparser.ast.NodeList.nodeList)4 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)4 VoidType (com.github.javaparser.ast.type.VoidType)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 AlphaNode (org.drools.core.reteoo.AlphaNode)4 QueryTerminalNode (org.drools.core.reteoo.QueryTerminalNode)4 StaticJavaParser.parseStatement (com.github.javaparser.StaticJavaParser.parseStatement)3 Memory (org.drools.core.common.Memory)3 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)3 BetaMemory (org.drools.core.reteoo.BetaMemory)3 ConditionalBranchMemory (org.drools.core.reteoo.ConditionalBranchNode.ConditionalBranchMemory)3 EvalMemory (org.drools.core.reteoo.EvalConditionNode.EvalMemory)3 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)3