use of org.drools.core.reteoo.RightInputAdapterNode in project drools by kiegroup.
the class SegmentCreationTest method tesShareInSubnetwork.
@Test
public void tesShareInSubnetwork() throws Exception {
KieBase kbase = buildKnowledgeBase(" A() \n", " A() B() C() \n", " A() not ( B() and C() ) \n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.class);
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
RuleTerminalNode rtn1 = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[1];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn2 = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
RightInputAdapterNode riaNode = (RightInputAdapterNode) cNode.getSinkPropagator().getSinks()[1];
NotNode notNode = (NotNode) liaNode.getSinkPropagator().getSinks()[2];
RuleTerminalNode rtn3 = (RuleTerminalNode) notNode.getSinkPropagator().getSinks()[0];
wm.insert(new LinkingTest.A());
wm.insert(new LinkingTest.B());
wm.insert(new LinkingTest.C());
wm.flushPropagations();
// LiaNode is in it's own segment
LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
SegmentMemory smem = liaMem.getSegmentMemory();
assertEquals(liaNode, smem.getRootNode());
assertEquals(liaNode, smem.getTipNode());
SegmentMemory rtnSmem1 = smem.getFirst();
assertEquals(rtn1, rtnSmem1.getRootNode());
assertEquals(rtn1, rtnSmem1.getTipNode());
SegmentMemory bSmem = rtnSmem1.getNext();
assertEquals(bNode, bSmem.getRootNode());
assertEquals(cNode, bSmem.getTipNode());
// segment is not initialized yet
assertNull(bSmem.getFirst());
wm.fireAllRules();
SegmentMemory rtn2Smem = bSmem.getFirst();
assertEquals(rtn2, rtn2Smem.getRootNode());
assertEquals(rtn2, rtn2Smem.getTipNode());
SegmentMemory riaSmem = rtn2Smem.getNext();
assertEquals(riaNode, riaSmem.getRootNode());
assertEquals(riaNode, riaSmem.getTipNode());
SegmentMemory notSmem = bSmem.getNext();
assertEquals(notNode, notSmem.getRootNode());
assertEquals(rtn3, notSmem.getTipNode());
}
use of org.drools.core.reteoo.RightInputAdapterNode in project drools by kiegroup.
the class PathEndNodeTest method testSubNetworkSharing.
@Test
public void testSubNetworkSharing() throws Exception {
String str = "package org.test \n" + "rule xxx \n" + "when \n" + " $s : String()\n" + " not( Integer() and Long() )\n" + "then \n" + "end \n" + "rule yyy \n" + "when \n" + " $s : String()\n" + " not( Integer() and Long() )\n" + "then \n" + "end \n";
InternalKnowledgeBase kbase = (InternalKnowledgeBase) new KieHelper().addContent(str, ResourceType.DRL).build();
EntryPointNode epn = kbase.getRete().getEntryPointNode(EntryPointId.DEFAULT);
ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(Long.class));
BetaNode beta1 = (BetaNode) otn.getObjectSinkPropagator().getSinks()[0];
RightInputAdapterNode rian = (RightInputAdapterNode) beta1.getSinkPropagator().getSinks()[0];
BetaNode beta2 = (BetaNode) rian.getObjectSinkPropagator().getSinks()[0];
LeftTupleSink[] sinks = beta2.getSinkPropagator().getSinks();
RuleTerminalNode rtn1 = (RuleTerminalNode) sinks[0];
RuleTerminalNode rtn2 = (RuleTerminalNode) sinks[1];
assertEquals(3, rian.getPathEndNodes().length);
assertTrue(asList(rian.getPathEndNodes()).containsAll(asList(rtn1, rtn2, rian)));
kbase.removeRule("org.test", "xxx");
assertEquals(2, rian.getPathEndNodes().length);
RuleTerminalNode remainingRTN = rtn1.getRule().getName().equals("yyy") ? rtn1 : rtn2;
assertTrue(asList(rian.getPathEndNodes()).containsAll(asList(remainingRTN, rian)));
}
use of org.drools.core.reteoo.RightInputAdapterNode in project drools by kiegroup.
the class ProtobufOutputMarshaller method writeRIANodeMemory.
private static ProtobufMessages.NodeMemory writeRIANodeMemory(final int nodeId, final BaseNode node, final NodeMemories memories) {
RightInputAdapterNode riaNode = (RightInputAdapterNode) node;
ObjectSink[] sinks = riaNode.getObjectSinkPropagator().getSinks();
BetaNode betaNode = (BetaNode) sinks[0];
Memory betaMemory = memories.peekNodeMemory(betaNode);
if (betaMemory == null) {
return null;
}
BetaMemory bm;
if (betaNode.getType() == NodeTypeEnums.AccumulateNode) {
bm = ((AccumulateMemory) betaMemory).getBetaMemory();
} else {
bm = (BetaMemory) betaMemory;
}
// for RIA nodes, we need to store the ID of the created handles
bm.getRightTupleMemory().iterator();
if (bm.getRightTupleMemory().size() > 0) {
ProtobufMessages.NodeMemory.RIANodeMemory.Builder _ria = ProtobufMessages.NodeMemory.RIANodeMemory.newBuilder();
final org.drools.core.util.Iterator it = bm.getRightTupleMemory().iterator();
// iterates over all propagated handles and assert them to the new sink
for (RightTuple entry = (RightTuple) it.next(); entry != null; entry = (RightTuple) it.next()) {
LeftTuple leftTuple = entry instanceof LeftTuple ? // with phreak the entry is always both a right and a left tuple
(LeftTuple) entry : // this is necessary only for reteoo
(LeftTuple) entry.getFactHandle().getObject();
InternalFactHandle handle = (InternalFactHandle) leftTuple.getFactHandle();
if (handle == null) {
continue;
}
FactHandle _handle = ProtobufMessages.FactHandle.newBuilder().setId(handle.getId()).setRecency(handle.getRecency()).build();
_ria.addContext(ProtobufMessages.NodeMemory.RIANodeMemory.RIAContext.newBuilder().setTuple(PersisterHelper.createTuple(leftTuple)).setResultHandle(_handle).build());
}
return ProtobufMessages.NodeMemory.newBuilder().setNodeId(nodeId).setNodeType(ProtobufMessages.NodeMemory.NodeType.RIA).setRia(_ria.build()).build();
}
return null;
}
use of org.drools.core.reteoo.RightInputAdapterNode in project drools by kiegroup.
the class ReteooRuleBuilder method setPathEndNodes.
private void setPathEndNodes(BuildContext context) {
// Store the paths in reverse order, from the outermost (the main path) to the innermost subnetwork paths
PathEndNode[] pathEndNodes = context.getPathEndNodes().toArray(new PathEndNode[context.getPathEndNodes().size()]);
for (int i = 0; i < pathEndNodes.length; i++) {
PathEndNode node = context.getPathEndNodes().get(pathEndNodes.length - 1 - i);
pathEndNodes[i] = node;
if (node instanceof RightInputAdapterNode && node.getPathEndNodes() != null) {
PathEndNode[] riaPathEndNodes = new PathEndNode[node.getPathEndNodes().length + i];
System.arraycopy(pathEndNodes, 0, riaPathEndNodes, 0, i);
System.arraycopy(node.getPathEndNodes(), 0, riaPathEndNodes, i, node.getPathEndNodes().length);
node.setPathEndNodes(riaPathEndNodes);
} else {
node.setPathEndNodes(pathEndNodes);
}
}
}
use of org.drools.core.reteoo.RightInputAdapterNode in project drools by kiegroup.
the class RuleNetworkEvaluator method getOffset.
public static int getOffset(NetworkNode node) {
LeftTupleSource lt;
int offset = 1;
if (NodeTypeEnums.isTerminalNode(node)) {
lt = ((TerminalNode) node).getLeftTupleSource();
offset++;
} else if (node.getType() == NodeTypeEnums.RightInputAdaterNode) {
lt = ((RightInputAdapterNode) node).getLeftTupleSource();
} else {
lt = (LeftTupleSource) node;
}
while (lt.getType() != NodeTypeEnums.LeftInputAdapterNode) {
offset++;
lt = lt.getLeftTupleSource();
}
return offset;
}
Aggregations