Search in sources :

Example 11 with InternalWorkingMemory

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

the class AddRemoveRule method addRule.

/**
 * This method is called after the rule nodes have been added to the network
 * For add tuples are processed after the segments and pmems have been adjusted
 */
public static void addRule(TerminalNode tn, InternalWorkingMemory[] wms, InternalKnowledgeBase kBase) {
    if (log.isTraceEnabled()) {
        log.trace("Adding Rule {}", tn.getRule().getName());
    }
    boolean hasProtos = kBase.hasSegmentPrototypes();
    boolean hasWms = wms.length > 0;
    if (!hasProtos && !hasWms) {
        return;
    }
    RuleImpl rule = tn.getRule();
    LeftTupleNode firstSplit = getNetworkSplitPoint(tn);
    PathEndNodes pathEndNodes = getPathEndNodes(kBase, firstSplit, tn, rule, hasProtos, hasWms);
    for (InternalWorkingMemory wm : wms) {
        wm.flushPropagations();
        if (NodeTypeEnums.LeftInputAdapterNode == firstSplit.getType() && firstSplit.getAssociationsSize() == 1) {
            // rule added with no sharing
            insertLiaFacts(firstSplit, wm);
        } else {
            PathEndNodeMemories tnms = getPathEndMemories(wm, pathEndNodes);
            if (tnms.subjectPmem == null) {
                // If the existing PathMemories are not yet initialized there are no Segments or tuples to process
                continue;
            }
            Map<PathMemory, SegmentMemory[]> prevSmemsLookup = reInitPathMemories(tnms.otherPmems, null);
            // must collect all visited SegmentMemories, for link notification
            Set<SegmentMemory> smemsToNotify = handleExistingPaths(tn, prevSmemsLookup, tnms.otherPmems, wm, ExistingPathStrategy.ADD_STRATEGY);
            addNewPaths(wm, smemsToNotify, tnms.subjectPmems);
            processLeftTuples(firstSplit, wm, true, rule);
            notifySegments(smemsToNotify, wm);
        }
    }
    if (hasWms) {
        insertFacts(pathEndNodes, wms);
    } else {
        for (PathEndNode node : pathEndNodes.otherEndNodes) {
            node.resetPathMemSpec(null);
        }
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) LeftTupleNode(org.drools.core.reteoo.LeftTupleNode) PathEndNode(org.drools.core.reteoo.PathEndNode) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PathMemory(org.drools.core.reteoo.PathMemory)

Example 12 with InternalWorkingMemory

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

the class AddRemoveRule method insertPeerLeftTuple.

/**
 * Create all missing peers
 */
private static LeftTuple insertPeerLeftTuple(LeftTuple lt, LeftTupleSinkNode node, InternalWorkingMemory wm) {
    LeftInputAdapterNode.LiaNodeMemory liaMem = null;
    if (node.getLeftTupleSource().getType() == NodeTypeEnums.LeftInputAdapterNode) {
        liaMem = wm.getNodeMemory(((LeftInputAdapterNode) node.getLeftTupleSource()));
    }
    LeftTuple peer = node.createPeer(lt);
    Memory memory = wm.getNodeMemories().peekNodeMemory(node);
    if (memory == null || memory.getSegmentMemory() == null) {
        throw new IllegalStateException("Defensive Programming: this should not be possilbe, as the addRule code should init child segments if they are needed ");
    }
    if (liaMem == null) {
        memory.getSegmentMemory().getStagedLeftTuples().addInsert(peer);
    } else {
        // If parent is Lian, then this must be called, so that any linking or unlinking can be done.
        LeftInputAdapterNode.doInsertSegmentMemory(wm, true, liaMem, memory.getSegmentMemory(), peer, node.getLeftTupleSource().isStreamMode());
    }
    return peer;
}
Also used : Memory(org.drools.core.common.Memory) PathMemory(org.drools.core.reteoo.PathMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentNodeMemory(org.drools.core.reteoo.SegmentNodeMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) RiaNodeMemory(org.drools.core.reteoo.RightInputAdapterNode.RiaNodeMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) TupleMemory(org.drools.core.reteoo.TupleMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 13 with InternalWorkingMemory

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

the class ProtobufInputMarshaller method readFactHandles.

public static void readFactHandles(MarshallerReaderContext context, org.drools.core.marshalling.impl.ProtobufMessages.EntryPoint _ep, ObjectStore objectStore, List<PropagationContext> pctxs) throws IOException, ClassNotFoundException {
    InternalWorkingMemory wm = context.wm;
    EntryPoint entryPoint = ((StatefulKnowledgeSessionImpl) context.wm).getEntryPointMap().get(_ep.getEntryPointId());
    // load the handles
    for (ProtobufMessages.FactHandle _handle : _ep.getHandleList()) {
        InternalFactHandle handle = readFactHandle(context, entryPoint, _handle);
        context.handles.put(handle.getId(), handle);
        if (!_handle.getIsJustified()) {
            // BeliefSystem handles the Object type
            if (handle.getObject() != null) {
                objectStore.addHandle(handle, handle.getObject());
            }
            // add handle to object type node
            assertHandleIntoOTN(context, wm, handle, pctxs);
        }
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) FactHandle(org.drools.core.marshalling.impl.ProtobufMessages.FactHandle) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 14 with InternalWorkingMemory

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

the class MarshallingTest method testAccumulate2.

@Test
public void testAccumulate2() throws Exception {
    String str = "package org.drools\n" + "\n" + "import org.drools.compiler.Message\n" + "\n" + "rule MyRule\n" + "  when\n" + "    Number( intValue >= 5 ) from accumulate ( m: Message( ), count( m ) )\n" + "  then\n" + "    System.out.println(\"Found messages\");\n" + "end\n";
    KieBase kBase = loadKnowledgeBaseFromString(str);
    KieSession ksession = kBase.newKieSession();
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.insert(new Message());
    ksession.insert(new Message());
    ksession.insert(new Message());
    ksession.insert(new Message());
    ksession.insert(new Message());
    ((InternalWorkingMemory) ksession).flushPropagations();
    assertEquals(1, ((InternalAgenda) ksession.getAgenda()).agendaSize());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Message(org.drools.compiler.Message) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 15 with InternalWorkingMemory

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

the class SegmentCreationTest method testBranchCESingleSegment.

@Test
public void testBranchCESingleSegment() throws Exception {
    KieBase kbase = buildKnowledgeBase("   $a : A() \n" + "   if ( $a != null ) do[t1] \n" + "   B() \n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    ConditionalBranchNode cen1Node = (ConditionalBranchNode) liaNode.getSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) cen1Node.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn1 = (RuleTerminalNode) bNode.getSinkPropagator().getSinks()[0];
    FactHandle bFh = wm.insert(new LinkingTest.B());
    wm.flushPropagations();
    LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory smem = liaMem.getSegmentMemory();
    assertEquals(1, smem.getAllLinkedMaskTest());
    // B links, but it will not trigger mask
    assertEquals(4, smem.getLinkedNodeMask());
    assertFalse(smem.isSegmentLinked());
    PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn1);
    assertEquals(1, pmem.getAllLinkedMaskTest());
    assertEquals(0, pmem.getLinkedSegmentMask());
    assertFalse(pmem.isRuleLinked());
    wm.insert(new LinkingTest.A());
    wm.flushPropagations();
    // A links in segment
    assertEquals(5, smem.getLinkedNodeMask());
    assertTrue(smem.isSegmentLinked());
    assertEquals(1, pmem.getLinkedSegmentMask());
    assertTrue(pmem.isRuleLinked());
    // retract B does not unlink the rule
    wm.delete(bFh);
    wm.flushPropagations();
    assertEquals(1, pmem.getLinkedSegmentMask());
    assertTrue(pmem.isRuleLinked());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) FactHandle(org.kie.api.runtime.rule.FactHandle) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ConditionalBranchNode(org.drools.core.reteoo.ConditionalBranchNode) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Aggregations

InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)147 Test (org.junit.Test)107 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)72 KieBase (org.kie.api.KieBase)53 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)47 ArrayList (java.util.ArrayList)46 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)45 SegmentMemory (org.drools.core.reteoo.SegmentMemory)42 List (java.util.List)39 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)36 BetaMemory (org.drools.core.reteoo.BetaMemory)33 KieSession (org.kie.api.runtime.KieSession)32 JoinNode (org.drools.core.reteoo.JoinNode)30 PathMemory (org.drools.core.reteoo.PathMemory)29 ClassObjectType (org.drools.core.base.ClassObjectType)26 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)25 FactHandle (org.kie.api.runtime.rule.FactHandle)23 BetaNode (org.drools.core.reteoo.BetaNode)22 KieHelper (org.kie.internal.utils.KieHelper)22 AlphaNode (org.drools.core.reteoo.AlphaNode)19