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