Search in sources :

Example 1 with TerminalNode

use of org.drools.core.reteoo.TerminalNode in project drools by kiegroup.

the class RuleNetworkEvaluator method evaluateNetwork.

public void evaluateNetwork(PathMemory pmem, RuleExecutor executor, InternalAgenda agenda) {
    SegmentMemory[] smems = pmem.getSegmentMemories();
    int smemIndex = 0;
    // 0
    SegmentMemory smem = smems[smemIndex];
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) smem.getRootNode();
    LinkedList<StackEntry> stack = new LinkedList<StackEntry>();
    NetworkNode node;
    Memory nodeMem;
    long bit = 1;
    if (liaNode == smem.getTipNode()) {
        // segment only has liaNode in it
        // nothing is staged in the liaNode, so skip to next segment
        // 1
        smem = smems[++smemIndex];
        node = smem.getRootNode();
        nodeMem = smem.getNodeMemories().getFirst();
    } else {
        // lia is in shared segment, so point to next node
        bit = 2;
        node = liaNode.getSinkPropagator().getFirstLeftTupleSink();
        // skip the liaNode memory
        nodeMem = smem.getNodeMemories().getFirst().getNext();
    }
    TupleSets<LeftTuple> srcTuples = smem.getStagedLeftTuples();
    if (log.isTraceEnabled()) {
        log.trace("Rule[name={}] segments={} {}", ((TerminalNode) pmem.getPathEndNode()).getRule().getName(), smems.length, srcTuples.toStringSizes());
    }
    outerEval(pmem, node, bit, nodeMem, smems, smemIndex, srcTuples, agenda, stack, true, 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) NetworkNode(org.drools.core.common.NetworkNode) LeftTuple(org.drools.core.reteoo.LeftTuple) LinkedList(org.drools.core.util.LinkedList) QueryTerminalNode(org.drools.core.reteoo.QueryTerminalNode) TerminalNode(org.drools.core.reteoo.TerminalNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 2 with TerminalNode

use of org.drools.core.reteoo.TerminalNode in project drools by kiegroup.

the class AddRemoveRule method flushLeftTupleIfNecessary.

public static boolean flushLeftTupleIfNecessary(InternalWorkingMemory wm, SegmentMemory sm, LeftTuple leftTuple, boolean streamMode, short stagedType) {
    PathMemory pmem = streamMode ? sm.getPathMemories().get(0) : sm.getFirstDataDrivenPathMemory();
    if (pmem == null) {
        return false;
    }
    TupleSets<LeftTuple> leftTupleSets = new TupleSetsImpl<LeftTuple>();
    if (leftTuple != null) {
        switch(stagedType) {
            case Tuple.INSERT:
                leftTupleSets.addInsert(leftTuple);
                break;
            case Tuple.DELETE:
                leftTupleSets.addDelete(leftTuple);
                break;
            case Tuple.UPDATE:
                leftTupleSets.addUpdate(leftTuple);
                break;
        }
    }
    forceFlushLeftTuple(pmem, sm, wm, leftTupleSets);
    if (pmem.isDataDriven() && pmem.getNodeType() == NodeTypeEnums.RightInputAdaterNode) {
        for (PathEndNode pnode : pmem.getPathEndNode().getPathEndNodes()) {
            if (pnode instanceof TerminalNode) {
                PathMemory outPmem = wm.getNodeMemory((TerminalNode) pnode);
                if (outPmem.isDataDriven()) {
                    SegmentMemory outSmem = outPmem.getSegmentMemories()[0];
                    if (outSmem != null) {
                        forceFlushLeftTuple(outPmem, outSmem, wm, new TupleSetsImpl<LeftTuple>());
                    }
                }
            }
        }
    }
    return true;
}
Also used : TupleSetsImpl(org.drools.core.common.TupleSetsImpl) SegmentMemory(org.drools.core.reteoo.SegmentMemory) PathEndNode(org.drools.core.reteoo.PathEndNode) AbstractTerminalNode(org.drools.core.reteoo.AbstractTerminalNode) TerminalNode(org.drools.core.reteoo.TerminalNode) LeftTuple(org.drools.core.reteoo.LeftTuple) PathMemory(org.drools.core.reteoo.PathMemory)

Example 3 with TerminalNode

use of org.drools.core.reteoo.TerminalNode in project drools by kiegroup.

the class NodesPartitioningTest method checkNode.

private void checkNode(NetworkNode node) {
    if (node instanceof EntryPointNode) {
        assertSame(RuleBasePartitionId.MAIN_PARTITION, node.getPartitionId());
    } else if (node instanceof ObjectTypeNode) {
        assertSame(RuleBasePartitionId.MAIN_PARTITION, node.getPartitionId());
        checkPartitionedSinks((ObjectTypeNode) node);
    } else if (node instanceof ObjectSource) {
        ObjectSource source = ((ObjectSource) node).getParentObjectSource();
        if (!(source instanceof ObjectTypeNode)) {
            assertSame(source.getPartitionId(), node.getPartitionId());
        }
    } else if (node instanceof BetaNode) {
        ObjectSource rightInput = ((BetaNode) node).getRightInput();
        if (!(rightInput instanceof ObjectTypeNode)) {
            assertSame(rightInput.getPartitionId(), node.getPartitionId());
        }
        LeftTupleSource leftInput = ((BetaNode) node).getLeftTupleSource();
        assertSame(leftInput.getPartitionId(), node.getPartitionId());
    } else if (node instanceof TerminalNode) {
        LeftTupleSource leftInput = ((TerminalNode) node).getLeftTupleSource();
        assertSame(leftInput.getPartitionId(), node.getPartitionId());
    }
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) BetaNode(org.drools.core.reteoo.BetaNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ObjectSource(org.drools.core.reteoo.ObjectSource) TerminalNode(org.drools.core.reteoo.TerminalNode)

Example 4 with TerminalNode

use of org.drools.core.reteoo.TerminalNode in project drools by kiegroup.

the class TerminalNodeIteratorTest method testTerminalNodeListener.

@Test
public void testTerminalNodeListener() {
    String str = "package org.kie.test \n" + "\n" + "rule rule1 when\n" + "then\n" + "end\n" + "rule rule2 when\n" + "then\n" + "end\n" + "rule rule3 when\n" + "    Object()" + "then\n" + "end\n" + "rule rule4 when\n" + "    Object()" + "then\n" + "end\n" + // this will result in two terminal nodes
    "rule rule5 when\n" + "    Object() or\n" + "    Object()\n" + "then\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    List<String> nodes = new ArrayList<String>();
    Iterator it = TerminalNodeIterator.iterator(kbase);
    for (TerminalNode node = (TerminalNode) it.next(); node != null; node = (TerminalNode) it.next()) {
        nodes.add(((RuleTerminalNode) node).getRule().getName());
    }
    assertEquals(6, nodes.size());
    assertTrue(nodes.contains("rule1"));
    assertTrue(nodes.contains("rule2"));
    assertTrue(nodes.contains("rule3"));
    assertTrue(nodes.contains("rule4"));
    assertTrue(nodes.contains("rule5"));
    int first = nodes.indexOf("rule5");
    int second = nodes.lastIndexOf("rule5");
    assertTrue(first != second);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) TerminalNodeIterator(org.drools.core.common.TerminalNodeIterator) Iterator(org.drools.core.util.Iterator) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) TerminalNode(org.drools.core.reteoo.TerminalNode) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 5 with TerminalNode

use of org.drools.core.reteoo.TerminalNode in project drools by kiegroup.

the class NamedConsequenceBuilder method buildTerminalNodeForNamedConsequence.

static RuleTerminalNode buildTerminalNodeForNamedConsequence(BuildContext context, NamedConsequence namedConsequence) {
    RuleImpl rule = context.getRule();
    GroupElement subrule = (GroupElement) context.peek();
    ActivationListenerFactory factory = context.getKnowledgeBase().getConfiguration().getActivationListenerFactory(rule.getActivationListener());
    context.setConsequenceName(namedConsequence.getConsequenceName());
    TerminalNode terminal = factory.createActivationListener(context.getNextId(), context.getTupleSource(), rule, subrule, // subruleIndex,
    0, context);
    context.setConsequenceName(null);
    return (RuleTerminalNode) terminal;
}
Also used : GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ActivationListenerFactory(org.drools.core.ActivationListenerFactory) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) TerminalNode(org.drools.core.reteoo.TerminalNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode)

Aggregations

TerminalNode (org.drools.core.reteoo.TerminalNode)8 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)3 ArrayList (java.util.ArrayList)2 ActivationListenerFactory (org.drools.core.ActivationListenerFactory)2 LeftTuple (org.drools.core.reteoo.LeftTuple)2 PathMemory (org.drools.core.reteoo.PathMemory)2 SegmentMemory (org.drools.core.reteoo.SegmentMemory)2 GroupElement (org.drools.core.rule.GroupElement)2 BaseNode (org.drools.core.common.BaseNode)1 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)1 Memory (org.drools.core.common.Memory)1 NetworkNode (org.drools.core.common.NetworkNode)1 TerminalNodeIterator (org.drools.core.common.TerminalNodeIterator)1 TupleSetsImpl (org.drools.core.common.TupleSetsImpl)1 UpdateContext (org.drools.core.common.UpdateContext)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 AbstractTerminalNode (org.drools.core.reteoo.AbstractTerminalNode)1 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)1 BetaMemory (org.drools.core.reteoo.BetaMemory)1