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