Search in sources :

Example 81 with ObjectTypeNode

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

the class ParallelEvaluationTest method test.

@Test(timeout = 10000L)
public void test() {
    StringBuilder sb = new StringBuilder(400);
    sb.append("global java.util.List list;\n");
    for (int i = 0; i < 10; i++) {
        sb.append(getRule(i, ""));
    }
    KieBase kbase = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(MultithreadEvaluationOption.YES);
    EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNode(EntryPointId.DEFAULT);
    ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(Integer.class));
    assertTrue(((CompositePartitionAwareObjectSinkAdapter) otn.getObjectSinkPropagator()).isHashed());
    KieSession ksession = kbase.newKieSession();
    assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
    List<Integer> list = new DebugList<Integer>();
    ksession.setGlobal("list", list);
    for (int i = 0; i < 10; i++) {
        ksession.insert(i);
        ksession.insert("" + i);
    }
    ksession.fireAllRules();
    assertEquals(10, list.size());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) EntryPointNode(org.drools.core.reteoo.EntryPointNode) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) DebugList(org.drools.compiler.util.debug.DebugList) Test(org.junit.Test)

Example 82 with ObjectTypeNode

use of org.drools.core.reteoo.ObjectTypeNode 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 83 with ObjectTypeNode

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

the class SegmentCreationTest method testBranchCEMultipleSegments.

@Test
public void testBranchCEMultipleSegments() throws Exception {
    KieBase kbase = buildKnowledgeBase(// r1
    "   $a : A() \n", "   $a : A() \n" + "   if ( $a != null ) do[t1] \n" + // r2
    "   B() \n", "   $a : A() \n" + "   if ( $a != null ) do[t1] \n" + "   B() \n" + // r3
    "   C() \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()[1];
    JoinNode bNode = (JoinNode) cen1Node.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn2 = (RuleTerminalNode) bNode.getSinkPropagator().getSinks()[0];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[1];
    RuleTerminalNode rtn3 = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
    FactHandle bFh = wm.insert(new LinkingTest.B());
    FactHandle cFh = wm.insert(new LinkingTest.C());
    wm.flushPropagations();
    BetaMemory bNodeBm = (BetaMemory) wm.getNodeMemory(bNode);
    SegmentMemory bNodeSmem = bNodeBm.getSegmentMemory();
    // no beta nodes before branch CE, so never unlinks
    assertEquals(0, bNodeSmem.getAllLinkedMaskTest());
    assertEquals(2, bNodeSmem.getLinkedNodeMask());
    PathMemory pmemr2 = (PathMemory) wm.getNodeMemory(rtn2);
    assertEquals(1, pmemr2.getAllLinkedMaskTest());
    assertEquals(2, pmemr2.getLinkedSegmentMask());
    assertEquals(3, pmemr2.getSegmentMemories().length);
    assertFalse(pmemr2.isRuleLinked());
    PathMemory pmemr3 = (PathMemory) wm.getNodeMemory(rtn3);
    // notice only the first segment links
    assertEquals(1, pmemr3.getAllLinkedMaskTest());
    assertEquals(3, pmemr3.getSegmentMemories().length);
    assertFalse(pmemr3.isRuleLinked());
    BetaMemory cNodeBm = (BetaMemory) wm.getNodeMemory(cNode);
    SegmentMemory cNodeSmem = cNodeBm.getSegmentMemory();
    assertEquals(1, cNodeSmem.getAllLinkedMaskTest());
    assertEquals(1, cNodeSmem.getLinkedNodeMask());
    wm.insert(new LinkingTest.A());
    wm.flushPropagations();
    assertTrue(pmemr2.isRuleLinked());
    assertTrue(pmemr3.isRuleLinked());
    // retract B does not unlink the rule
    wm.delete(bFh);
    // retract C does not unlink the rule
    wm.delete(cFh);
    wm.flushPropagations();
    // b segment never unlinks, as it has no impact on path unlinking anyway
    assertEquals(3, pmemr2.getLinkedSegmentMask());
    assertTrue(pmemr2.isRuleLinked());
    // b segment never unlinks, as it has no impact on path unlinking anyway
    assertEquals(3, pmemr3.getLinkedSegmentMask());
    assertTrue(pmemr3.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) BetaMemory(org.drools.core.reteoo.BetaMemory) 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)

Example 84 with ObjectTypeNode

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

the class SegmentCreationTest method testSingleSharedEmptyLhs.

@Test
public void testSingleSharedEmptyLhs() throws Exception {
    KieBase kbase = buildKnowledgeBase(" ", " ");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode aotn = getObjectTypeNode(kbase, InitialFactImpl.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn1 = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn2 = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[1];
    wm.insert(new LinkingTest.A());
    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());
    // each RTN is in it's own segment
    SegmentMemory rtnSmem1 = smem.getFirst();
    assertEquals(rtn1, rtnSmem1.getRootNode());
    assertEquals(rtn1, rtnSmem1.getTipNode());
    SegmentMemory rtnSmem2 = rtnSmem1.getNext();
    assertEquals(rtn2, rtnSmem2.getRootNode());
    assertEquals(rtn2, rtnSmem2.getTipNode());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 85 with ObjectTypeNode

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

the class SegmentCreationTest method testSingleSharedPattern.

@Test
public void testSingleSharedPattern() throws Exception {
    KieBase kbase = buildKnowledgeBase("   A() \n", "   A() \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];
    RuleTerminalNode rtn2 = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[1];
    wm.insert(new LinkingTest.A());
    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());
    // each RTN is in it's own segment
    SegmentMemory rtnSmem1 = smem.getFirst();
    assertEquals(rtn1, rtnSmem1.getRootNode());
    assertEquals(rtn1, rtnSmem1.getTipNode());
    SegmentMemory rtnSmem2 = rtnSmem1.getNext();
    assertEquals(rtn2, rtnSmem2.getRootNode());
    assertEquals(rtn2, rtnSmem2.getTipNode());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Aggregations

ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)116 Test (org.junit.Test)100 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)69 KieBase (org.kie.api.KieBase)64 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)60 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)40 ClassObjectType (org.drools.core.base.ClassObjectType)34 JoinNode (org.drools.core.reteoo.JoinNode)31 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)30 AlphaNode (org.drools.core.reteoo.AlphaNode)29 ArrayList (java.util.ArrayList)28 BetaNode (org.drools.core.reteoo.BetaNode)27 BetaMemory (org.drools.core.reteoo.BetaMemory)24 List (java.util.List)23 SegmentMemory (org.drools.core.reteoo.SegmentMemory)23 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)23 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)22 KieSession (org.kie.api.runtime.KieSession)20 Person (org.drools.compiler.Person)17 EntryPointNode (org.drools.core.reteoo.EntryPointNode)16