Search in sources :

Example 16 with LeftInputAdapterNode

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

the class SegmentCreationTest method testMultiSharedPattern.

@Test
public void testMultiSharedPattern() throws Exception {
    KieBase kbase = buildKnowledgeBase("   A() \n", "   A() B() \n", "   A() B() 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];
    RuleTerminalNode rtn2 = (RuleTerminalNode) bNode.getSinkPropagator().getSinks()[0];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[1];
    RuleTerminalNode rtn3 = (RuleTerminalNode) cNode.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(bNode, bSmem.getTipNode());
    // child segment is not yet initialised, so null
    assertNull(bSmem.getFirst());
    // there is no next
    assertNull(bSmem.getNext());
    // child segments should now be initialised
    wm.fireAllRules();
    wm.flushPropagations();
    SegmentMemory rtnSmem2 = bSmem.getFirst();
    assertEquals(rtn2, rtnSmem2.getRootNode());
    assertEquals(rtn2, rtnSmem2.getTipNode());
    SegmentMemory cSmem = rtnSmem2.getNext();
    assertEquals(cNode, cSmem.getRootNode());
    // note rtn3 is in the same segment as C
    assertEquals(rtn3, cSmem.getTipNode());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) KieBase(org.kie.api.KieBase) JoinNode(org.drools.core.reteoo.JoinNode) 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 17 with LeftInputAdapterNode

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

the class FromTest method testFromSharingWithPropertyReactive.

@Test
public void testFromSharingWithPropertyReactive() {
    // As above but with property reactive as default
    String drl = "import " + ListsContainer.class.getCanonicalName() + "\n" + "global java.util.List output1;\n" + "global java.util.List output2;\n" + "rule R1 when\n" + "    ListsContainer( $list : list1 )\n" + "    $s : String( length == 2 ) from $list\n" + "then\n" + "    output1.add($s);\n" + "end\n" + "rule R2 when\n" + "    ListsContainer( $list : list2 )\n" + "    $s : String( length == 2 ) from $list\n" + "then\n" + "    output2.add($s);\n" + "end\n" + "rule R3 when\n" + "    ListsContainer( $list : list2 )\n" + "    $s : String( length == 2 ) from $list\n" + "then\n" + "    output2.add($s);\n" + "end\n";
    // property reactive as default:
    KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();
    ReteDumper.dumpRete(kbase);
    List<String> output1 = new ArrayList<String>();
    ksession.setGlobal("output1", output1);
    List<String> output2 = new ArrayList<String>();
    ksession.setGlobal("output2", output2);
    FactHandle fh = ksession.insert(new ListsContainer());
    ksession.fireAllRules();
    assertEquals("bb", output1.get(0));
    assertEquals("22", output2.get(0));
    assertEquals("22", output2.get(1));
    EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNodes().values().iterator().next();
    ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(ListsContainer.class));
    // There are 2 LIAs, one for the list1 and the other for the list2
    assertEquals(2, otn.getObjectSinkPropagator().size());
    LeftInputAdapterNode lia0 = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
    // There are only 2 FromNodes since R2 and R3 are sharing the second From
    // The first FROM node has R1 has sink
    LeftTupleSink[] sinks0 = lia0.getSinkPropagator().getSinks();
    assertEquals(1, sinks0.length);
    assertEquals(1, sinks0[0].getSinkPropagator().size());
    // The second FROM node has both R2 and R3 as sinks
    LeftInputAdapterNode lia1 = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[1];
    LeftTupleSink[] sinks1 = lia1.getSinkPropagator().getSinks();
    assertEquals(1, sinks1.length);
    assertEquals(2, sinks1[0].getSinkPropagator().size());
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) KieHelper(org.kie.internal.utils.KieHelper) EntryPointNode(org.drools.core.reteoo.EntryPointNode) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 18 with LeftInputAdapterNode

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

the class FromTest method testFromSharing.

@Test
public void testFromSharing() {
    // Keeping original test as non-property reactive by default, just allowed.
    String drl = "import " + ListsContainer.class.getCanonicalName() + "\n" + "global java.util.List output1;\n" + "global java.util.List output2;\n" + "rule R1 when\n" + "    ListsContainer( $list : list1 )\n" + "    $s : String( length == 2 ) from $list\n" + "then\n" + "    output1.add($s);\n" + "end\n" + "rule R2 when\n" + "    ListsContainer( $list : list2 )\n" + "    $s : String( length == 2 ) from $list\n" + "then\n" + "    output2.add($s);\n" + "end\n" + "rule R3 when\n" + "    ListsContainer( $list : list2 )\n" + "    $s : String( length == 2 ) from $list\n" + "then\n" + "    output2.add($s);\n" + "end\n";
    KieBase kbase = new KieHelper(PropertySpecificOption.ALLOWED).addContent(drl, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();
    ReteDumper.dumpRete(kbase);
    List<String> output1 = new ArrayList<String>();
    ksession.setGlobal("output1", output1);
    List<String> output2 = new ArrayList<String>();
    ksession.setGlobal("output2", output2);
    FactHandle fh = ksession.insert(new ListsContainer());
    ksession.fireAllRules();
    assertEquals("bb", output1.get(0));
    assertEquals("22", output2.get(0));
    assertEquals("22", output2.get(1));
    EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNodes().values().iterator().next();
    ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(ListsContainer.class));
    // There is only 1 LIA
    assertEquals(1, otn.getObjectSinkPropagator().size());
    LeftInputAdapterNode lian = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
    // There are only 2 FromNodes since R2 and R3 are sharing the second From
    LeftTupleSink[] sinks = lian.getSinkPropagator().getSinks();
    assertEquals(2, sinks.length);
    // The first from has R1 has sink
    assertEquals(1, sinks[0].getSinkPropagator().size());
    // The second from has both R2 and R3 as sinks
    assertEquals(2, sinks[1].getSinkPropagator().size());
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) KieHelper(org.kie.internal.utils.KieHelper) EntryPointNode(org.drools.core.reteoo.EntryPointNode) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 19 with LeftInputAdapterNode

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

the class AddRuleTest method testPopulatedSharedLiaNode.

@Test
public void testPopulatedSharedLiaNode() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A() B(1;) C() D() E()\n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new A(3));
    wm.insert(new B(1));
    wm.insert(new B(2));
    wm.insert(new C(1));
    wm.insert(new D(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(3, list.size());
    kbase1.addPackages(buildKnowledgePackage("r2", "   a : A() B(2;) C() D() E()\n"));
    ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode bNode1 = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode bNode2 = (JoinNode) liaNode.getSinkPropagator().getLastLeftTupleSink();
    BetaMemory bm = (BetaMemory) wm.getNodeMemory(bNode2);
    SegmentMemory sm = bm.getSegmentMemory();
    assertNotNull(sm.getStagedLeftTuples().getInsertFirst());
    assertNotNull(sm.getStagedLeftTuples().getInsertFirst().getStagedNext());
    assertNotNull(sm.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
    assertNull(sm.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext().getStagedNext());
    wm.fireAllRules();
    assertNull(sm.getStagedLeftTuples().getInsertFirst());
    assertEquals(6, list.size());
    assertEquals("r1", ((Match) list.get(0)).getRule().getName());
    assertEquals("r1", ((Match) list.get(1)).getRule().getName());
    assertEquals("r1", ((Match) list.get(2)).getRule().getName());
    assertEquals("r2", ((Match) list.get(3)).getRule().getName());
    assertEquals("r2", ((Match) list.get(4)).getRule().getName());
    assertEquals("r2", ((Match) list.get(5)).getRule().getName());
    List results = new ArrayList();
    results.add(((A) ((Match) list.get(3)).getDeclarationValue("a")).getObject());
    results.add(((A) ((Match) list.get(4)).getDeclarationValue("a")).getObject());
    results.add(((A) ((Match) list.get(5)).getDeclarationValue("a")).getObject());
    assertTrue(results.containsAll(asList(1, 2, 3)));
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) Match(org.kie.api.runtime.rule.Match) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 20 with LeftInputAdapterNode

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

the class AddRuleTest method testPopulatedRuleWithEvals.

@Test
public void testPopulatedRuleWithEvals() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   a:A() B() eval(1==1) eval(1==1) C(1;) \n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new A(3));
    wm.insert(new B(1));
    wm.insert(new C(1));
    wm.insert(new C(2));
    wm.fireAllRules();
    assertEquals(3, list.size());
    kbase1.addPackages(buildKnowledgePackage("r2", "   a:A() B() eval(1==1) eval(1==1) C(2;) \n"));
    ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
    EvalConditionNode e1 = (EvalConditionNode) bNode.getSinkPropagator().getFirstLeftTupleSink();
    EvalConditionNode e2 = (EvalConditionNode) e1.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c1Node = (JoinNode) e2.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c2Node = (JoinNode) e2.getSinkPropagator().getLastLeftTupleSink();
    LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();
    BetaMemory c1Mem = (BetaMemory) wm.getNodeMemory(c1Node);
    assertSame(sm.getFirst(), c1Mem.getSegmentMemory());
    assertEquals(3, c1Mem.getLeftTupleMemory().size());
    assertEquals(1, c1Mem.getRightTupleMemory().size());
    BetaMemory c2Mem = (BetaMemory) wm.getNodeMemory(c2Node);
    SegmentMemory c2Smem = sm.getFirst().getNext();
    assertSame(c2Smem, c2Mem.getSegmentMemory());
    assertEquals(0, c2Mem.getLeftTupleMemory().size());
    assertEquals(0, c2Mem.getRightTupleMemory().size());
    assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst());
    assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext());
    assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
    assertNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext().getStagedNext());
    wm.fireAllRules();
    assertEquals(3, c2Mem.getLeftTupleMemory().size());
    assertEquals(1, c2Mem.getRightTupleMemory().size());
    assertNull(c2Smem.getStagedLeftTuples().getInsertFirst());
    assertEquals(6, list.size());
    assertEquals("r1", ((Match) list.get(0)).getRule().getName());
    assertEquals("r1", ((Match) list.get(1)).getRule().getName());
    assertEquals("r1", ((Match) list.get(2)).getRule().getName());
    assertEquals("r2", ((Match) list.get(3)).getRule().getName());
    assertEquals(3, ((A) ((Match) list.get(3)).getDeclarationValue("a")).getObject());
    assertEquals("r2", ((Match) list.get(4)).getRule().getName());
    assertEquals(2, ((A) ((Match) list.get(4)).getDeclarationValue("a")).getObject());
    assertEquals("r2", ((Match) list.get(5)).getRule().getName());
    assertEquals(1, ((A) ((Match) list.get(5)).getDeclarationValue("a")).getObject());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) EvalConditionNode(org.drools.core.reteoo.EvalConditionNode) JoinNode(org.drools.core.reteoo.JoinNode) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) Match(org.kie.api.runtime.rule.Match) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Aggregations

LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)72 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)60 Test (org.junit.Test)56 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)48 KieBase (org.kie.api.KieBase)35 JoinNode (org.drools.core.reteoo.JoinNode)28 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)28 SegmentMemory (org.drools.core.reteoo.SegmentMemory)28 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)27 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)25 BetaMemory (org.drools.core.reteoo.BetaMemory)25 ArrayList (java.util.ArrayList)17 PathMemory (org.drools.core.reteoo.PathMemory)15 List (java.util.List)12 FactHandle (org.kie.api.runtime.rule.FactHandle)12 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)12 ClassObjectType (org.drools.core.base.ClassObjectType)11 AlphaNode (org.drools.core.reteoo.AlphaNode)11 NotNode (org.drools.core.reteoo.NotNode)11 ExistsNode (org.drools.core.reteoo.ExistsNode)10