Search in sources :

Example 11 with NetworkNode

use of org.drools.core.common.NetworkNode in project drools by kiegroup.

the class SessionInspector method gatherNodeInfo.

private void gatherNodeInfo(NetworkNode parent, Stack<NetworkNode> nodeStack, StatefulKnowledgeSessionInfo info) {
    if (!info.visited(parent)) {
        nodeStack.push(parent);
        NetworkNodeVisitor visitor = visitors.get(parent.getType());
        if (visitor != null) {
            visitor.visit(parent, nodeStack, info);
        } else {
            throw new RuntimeException("No visitor found for node class: " + parent.getClass() + " node: " + parent);
        }
        visitChildren(parent, nodeStack, info);
        nodeStack.pop();
    } else {
        // if already visited, then assign the same rules to the nodes currently in the stack
        Set<RuleImpl> rules = info.getNodeInfo(parent).getRules();
        for (NetworkNode snode : nodeStack) {
            for (RuleImpl rule : rules) {
                info.assign(snode, rule);
            }
        }
    }
}
Also used : RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) NetworkNode(org.drools.core.common.NetworkNode)

Example 12 with NetworkNode

use of org.drools.core.common.NetworkNode in project drools by kiegroup.

the class PhreakTimerNode method evaluate.

private static void evaluate(PathMemory pmem, InternalAgenda agenda, LeftTupleSink sink, TimerNodeMemory tm, TupleSets<LeftTuple> trgLeftTuples) {
    SegmentMemory[] smems = pmem.getSegmentMemories();
    SegmentMemory sm = tm.getSegmentMemory();
    int smemIndex = 0;
    for (SegmentMemory smem : smems) {
        if (smem == sm) {
            break;
        }
        smemIndex++;
    }
    long bit = 1;
    for (NetworkNode node = sm.getRootNode(); node != sink; node = ((LeftTupleSource) node).getSinkPropagator().getFirstLeftTupleSink()) {
        // update the bit to the correct node position.
        bit = bit << 1;
    }
    RuleNetworkEvaluator rne = new RuleNetworkEvaluator();
    rne.outerEval(pmem, sink, bit, tm, smems, smemIndex, trgLeftTuples, agenda, new LinkedList<>(), true, pmem.getRuleAgendaItem().getRuleExecutor());
}
Also used : LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) SegmentMemory(org.drools.core.reteoo.SegmentMemory) NetworkNode(org.drools.core.common.NetworkNode)

Example 13 with NetworkNode

use of org.drools.core.common.NetworkNode in project drools by kiegroup.

the class RuleNetworkEvaluator method evalStackEntry.

public void evalStackEntry(StackEntry entry, LinkedList<StackEntry> stack, RuleExecutor executor, InternalAgenda agenda) {
    NetworkNode node = entry.getNode();
    Memory nodeMem = entry.getNodeMem();
    TupleSets<LeftTuple> trgTuples = entry.getTrgTuples();
    if (node.getType() == NodeTypeEnums.QueryElementNode) {
        // copy across the results, if any from the query node memory
        QueryElementNodeMemory qmem = (QueryElementNodeMemory) nodeMem;
        qmem.setNodeCleanWithoutNotify();
        qmem.getResultLeftTuples().addTo(trgTuples);
    }
    LeftTupleSinkNode sink = entry.getSink();
    PathMemory pmem = entry.getRmem();
    SegmentMemory[] smems = entry.getSmems();
    int smemIndex = entry.getSmemIndex();
    boolean processRian = entry.isProcessRian();
    long bit = entry.getBit();
    if (entry.isResumeFromNextNode()) {
        SegmentMemory smem = smems[smemIndex];
        if (node != smem.getTipNode()) {
            // get next node and node memory in the segment
            LeftTupleSink nextSink = sink.getNextLeftTupleSinkNode();
            if (nextSink == null) {
                node = sink;
            } else {
                // there is a nested subnetwork, take out path
                node = nextSink;
            }
            nodeMem = nodeMem.getNext();
            // update bit to new node
            bit = bit << 1;
        } else {
            // Reached end of segment, start on new segment.
            SegmentPropagator.propagate(smem, trgTuples, agenda.getWorkingMemory());
            smem = smems[++smemIndex];
            trgTuples = smem.getStagedLeftTuples().takeAll();
            node = smem.getRootNode();
            nodeMem = smem.getNodeMemories().getFirst();
            // update bit to start of new segment
            bit = 1;
        }
    }
    if (log.isTraceEnabled()) {
        int offset = getOffset(node);
        log.trace("{} Resume {} {}", indent(offset), node.toString(), trgTuples.toStringSizes());
    }
    innerEval(pmem, node, bit, nodeMem, smems, smemIndex, trgTuples, agenda, stack, processRian, executor);
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) Memory(org.drools.core.common.Memory) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) PathMemory(org.drools.core.reteoo.PathMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ConditionalBranchMemory(org.drools.core.reteoo.ConditionalBranchNode.ConditionalBranchMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) TupleMemory(org.drools.core.reteoo.TupleMemory) TimerNodeMemory(org.drools.core.reteoo.TimerNode.TimerNodeMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) EvalMemory(org.drools.core.reteoo.EvalConditionNode.EvalMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) RiaPathMemory(org.drools.core.reteoo.RiaPathMemory) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) NetworkNode(org.drools.core.common.NetworkNode) LeftTuple(org.drools.core.reteoo.LeftTuple) LeftTupleSinkNode(org.drools.core.reteoo.LeftTupleSinkNode) PathMemory(org.drools.core.reteoo.PathMemory) RiaPathMemory(org.drools.core.reteoo.RiaPathMemory)

Example 14 with NetworkNode

use of org.drools.core.common.NetworkNode in project drools by kiegroup.

the class InlineFieldReferenceInitHandler method generateInitBody.

private void generateInitBody(BlockStmt initBodyPerPartition, List<NetworkNode> subNodes, int partitionIndex) {
    CompilationUnit partitionedCompilationUnit = partitionedNodeInitialisationClasses.get(partitionIndex);
    PackageDeclaration partitionedClassPackage = (PackageDeclaration) partitionedCompilationUnit.getChildNodes().get(0);
    ClassOrInterfaceDeclaration partitionedClass = (ClassOrInterfaceDeclaration) partitionedCompilationUnit.getChildNodes().get(1);
    for (NetworkNode n : subNodes) {
        if (n instanceof CanInlineInANC) {
            String variableName;
            final ClassOrInterfaceType initMethodReturnType;
            if (n instanceof AlphaNode) {
                variableName = getVariableName((AlphaNode) n);
                initMethodReturnType = StaticJavaParser.parseClassOrInterfaceType(getVariableType((AlphaNode) n).getCanonicalName());
            } else {
                variableName = getVariableName((Sink) n);
                initMethodReturnType = StaticJavaParser.parseClassOrInterfaceType(getVariableType((Sink) n).getCanonicalName());
            }
            String initMethodName = String.format("init%s", variableName);
            // We'll need to partition the lambda creation as with too many the compiler crashes
            // We'll move it to a separated class
            MethodDeclaration initMethod = new MethodDeclaration(nodeList(Modifier.publicModifier(), Modifier.staticModifier()), initMethodName, initMethodReturnType, parametersFromAdditionalFields());
            Expression initExpressionForNode = ((CanInlineInANC) n).toANCInlinedForm();
            partitionedClass.addMember(initMethod);
            BlockStmt initMethodBlock = new BlockStmt();
            initMethodBlock.addStatement(new ReturnStmt(initExpressionForNode));
            initMethod.setBody(initMethodBlock);
            String callInitMethodAndAssignToFieldString = String.format("%s = %s.%s.%s(ctx);", variableName, partitionedClassPackage.getNameAsString(), partitionedClass.getNameAsString(), initMethodName);
            initBodyPerPartition.addStatement(parseStatement(callInitMethodAndAssignToFieldString));
        }
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NetworkNode(org.drools.core.common.NetworkNode) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) AlphaNode(org.drools.core.reteoo.AlphaNode) Sink(org.drools.core.reteoo.Sink) Expression(com.github.javaparser.ast.expr.Expression) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration)

Example 15 with NetworkNode

use of org.drools.core.common.NetworkNode in project drools by kiegroup.

the class PhreakTimerNode method evaluate.

private static void evaluate(PathMemory pmem, ActivationsManager activationsManager, LeftTupleSink sink, TimerNodeMemory tm, TupleSets<LeftTuple> trgLeftTuples) {
    SegmentMemory[] smems = pmem.getSegmentMemories();
    SegmentMemory sm = tm.getSegmentMemory();
    int smemIndex = 0;
    for (SegmentMemory smem : smems) {
        if (smem == sm) {
            break;
        }
        smemIndex++;
    }
    long bit = 1;
    for (NetworkNode node = sm.getRootNode(); node != sink; node = ((LeftTupleSource) node).getSinkPropagator().getFirstLeftTupleSink()) {
        // update the bit to the correct node position.
        bit = bit << 1;
    }
    RuleNetworkEvaluator.INSTANCE.outerEval(pmem, sink, bit, tm, smems, smemIndex, trgLeftTuples, activationsManager, new LinkedList<>(), true, pmem.getRuleAgendaItem().getRuleExecutor());
}
Also used : LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) SegmentMemory(org.drools.core.reteoo.SegmentMemory) 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