Search in sources :

Example 16 with RightInputAdapterNode

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());
}
Also used : NotNode(org.drools.core.reteoo.NotNode) SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 17 with RightInputAdapterNode

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)));
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) BetaNode(org.drools.core.reteoo.BetaNode) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) KieHelper(org.kie.internal.utils.KieHelper) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 18 with RightInputAdapterNode

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;
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.drools.core.marshalling.impl.ProtobufMessages.FactHandle) QueryElementFactHandle(org.drools.core.common.QueryElementFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) Memory(org.drools.core.common.Memory) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) ObjectSink(org.drools.core.reteoo.ObjectSink) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode)

Example 19 with RightInputAdapterNode

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);
        }
    }
}
Also used : PathEndNode(org.drools.core.reteoo.PathEndNode) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode)

Example 20 with RightInputAdapterNode

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;
}
Also used : LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode)

Aggregations

RightInputAdapterNode (org.drools.core.reteoo.RightInputAdapterNode)22 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)10 BetaMemory (org.drools.core.reteoo.BetaMemory)10 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)10 Test (org.junit.Test)10 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)8 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)8 BetaNode (org.drools.core.reteoo.BetaNode)7 JoinNode (org.drools.core.reteoo.JoinNode)7 LeftTupleSource (org.drools.core.reteoo.LeftTupleSource)7 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)6 PathMemory (org.drools.core.reteoo.PathMemory)6 KieBase (org.kie.api.KieBase)6 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)5 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)5 ExistsNode (org.drools.core.reteoo.ExistsNode)5 ObjectSink (org.drools.core.reteoo.ObjectSink)5 SegmentMemory (org.drools.core.reteoo.SegmentMemory)5 ArrayList (java.util.ArrayList)4 Memory (org.drools.core.common.Memory)4