use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.
the class MemoryLeakTest method testStagedTupleLeak.
@Test
public void testStagedTupleLeak() throws Exception {
// BZ-1056599
String str = "rule R1 when\n" + " $i : Integer()\n" + "then\n" + " insertLogical( $i.toString() );\n" + "end\n" + "\n" + "rule R2 when\n" + " $i : Integer()\n" + "then\n" + " delete( $i );\n" + "end\n" + "\n" + "rule R3 when\n" + " $l : Long()\n" + " $s : String( this == $l.toString() )\n" + "then\n" + "end\n";
KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
KieSession ksession = kbase.newKieSession();
for (int i = 0; i < 10; i++) {
ksession.insert(i);
ksession.fireAllRules();
}
Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
JoinNode joinNode = null;
for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
if (String.class == otn.getObjectType().getValueType().getClassType()) {
joinNode = (JoinNode) otn.getObjectSinkPropagator().getSinks()[0];
break;
}
}
assertNotNull(joinNode);
InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
BetaMemory memory = (BetaMemory) wm.getNodeMemory(joinNode);
TupleSets<RightTuple> stagedRightTuples = memory.getStagedRightTuples();
assertNull(stagedRightTuples.getDeleteFirst());
assertNull(stagedRightTuples.getInsertFirst());
}
use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.
the class AccumulateTest method testAccumulateWithSameSubnetwork.
// (timeout = 10000)
@Test
public void testAccumulateWithSameSubnetwork() throws Exception {
String rule = "package org.drools.compiler.test;\n" + "import org.drools.compiler.Cheese;\n" + "import org.drools.compiler.Person;\n" + "global java.util.List list; \n" + "rule r1 salience 100 \n" + " when\n" + " $person : Person( name == 'Alice', $likes : likes )\n" + " $total : Number() from accumulate( $p : Person(likes != $likes, $l : likes) and $c : Cheese( type == $l ),\n" + " min($c.getPrice()) )\n" + " then\n" + " list.add( 'r1' + ':' + $total);\n" + "end\n" + "rule r2 \n" + " when\n" + " $person : Person( name == 'Alice', $likes : likes )\n" + " $total : Number() from accumulate( $p : Person(likes != $likes, $l : likes) and $c : Cheese( type == $l ),\n" + " max($c.getPrice()) )\n" + " then\n" + " list.add( 'r2' + ':' + $total);\n" + "end\n" + "";
// read in the source
KieBase kbase = loadKnowledgeBaseFromString(rule);
KieSession wm = createKnowledgeSession(kbase);
List list = new ArrayList();
wm.setGlobal("list", list);
// Check the network formation, to ensure the RiaNode is shared.
ObjectTypeNode cheeseOtn = LinkingTest.getObjectTypeNode(kbase, Cheese.class);
ObjectSink[] oSinks = cheeseOtn.getObjectSinkPropagator().getSinks();
assertEquals(1, oSinks.length);
JoinNode cheeseJoin = (JoinNode) oSinks[0];
LeftTupleSink[] ltSinks = cheeseJoin.getSinkPropagator().getSinks();
assertEquals(1, ltSinks.length);
RightInputAdapterNode rian = (RightInputAdapterNode) ltSinks[0];
// RiaNode is shared, if this has two outputs
assertEquals(2, rian.getObjectSinkPropagator().size());
wm.insert(new Cheese("stilton", 10));
wm.insert(new Person("Alice", "brie"));
wm.insert(new Person("Bob", "stilton"));
wm.fireAllRules();
assertEquals(2, list.size());
assertEquals("r1:10", list.get(0));
assertEquals("r2:10", list.get(1));
}
use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.
the class LinkingTest method testNotNodeMasksWithoutConstraints.
@Test
public void testNotNodeMasksWithoutConstraints() throws Exception {
String str = "";
str += "package org.kie \n";
str += "import " + A.class.getCanonicalName() + "\n";
str += "import " + B.class.getCanonicalName() + "\n";
str += "import " + C.class.getCanonicalName() + "\n";
str += "import " + D.class.getCanonicalName() + "\n";
str += "import " + E.class.getCanonicalName() + "\n";
str += "import " + F.class.getCanonicalName() + "\n";
str += "import " + G.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
str += "rule rule1 when \n";
str += " A() \n";
str += " not( B( ) ) \n";
str += " C() \n";
str += "then \n";
str += "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
assertEquals(1, liaNode.getSinkPropagator().size());
wm.insert(new A());
wm.flushPropagations();
NotNode notNode = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
JoinNode cNode = (JoinNode) notNode.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn);
assertEquals(1, pmem.getSegmentMemories().length);
assertEquals(1, pmem.getAllLinkedMaskTest());
SegmentMemory sm = pmem.getSegmentMemories()[0];
assertEquals(7, sm.getAllLinkedMaskTest());
assertEquals(3, sm.getLinkedNodeMask());
assertFalse(sm.isSegmentLinked());
assertFalse(pmem.isRuleLinked());
wm.insert(new C());
wm.flushPropagations();
assertEquals(7, sm.getLinkedNodeMask());
assertTrue(sm.isSegmentLinked());
assertTrue(pmem.isRuleLinked());
}
use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.
the class LinkingTest method testNotNodeMasksWithConstraints.
@Test
public void testNotNodeMasksWithConstraints() throws Exception {
String str = "";
str += "package org.kie \n";
str += "import " + A.class.getCanonicalName() + "\n";
str += "import " + B.class.getCanonicalName() + "\n";
str += "import " + C.class.getCanonicalName() + "\n";
str += "import " + D.class.getCanonicalName() + "\n";
str += "import " + E.class.getCanonicalName() + "\n";
str += "import " + F.class.getCanonicalName() + "\n";
str += "import " + G.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
str += "rule rule1 when \n";
str += " $a : A() \n";
str += " not( B( value == $a.value ) ) \n";
str += " C() \n";
str += "then \n";
str += "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
assertEquals(1, liaNode.getSinkPropagator().size());
wm.insert(new A());
wm.flushPropagations();
NotNode notNode = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
JoinNode cNode = (JoinNode) notNode.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn);
assertEquals(1, pmem.getSegmentMemories().length);
assertEquals(1, pmem.getAllLinkedMaskTest());
SegmentMemory sm = pmem.getSegmentMemories()[0];
assertEquals(5, sm.getAllLinkedMaskTest());
assertEquals(3, sm.getLinkedNodeMask());
assertFalse(sm.isSegmentLinked());
assertFalse(pmem.isRuleLinked());
wm.insert(new C());
wm.flushPropagations();
// only 5 is needed to link, the 'not' turns on but it has no unfleunce either way
assertEquals(7, sm.getLinkedNodeMask());
assertTrue(sm.isSegmentLinked());
assertTrue(pmem.isRuleLinked());
}
use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.
the class LinkingTest method testNonReactiveSubNetworkOwnSegmentMasks.
@Test
public void testNonReactiveSubNetworkOwnSegmentMasks() throws Exception {
String str = "";
str += "package org.kie \n";
str += "import " + A.class.getCanonicalName() + "\n";
str += "import " + B.class.getCanonicalName() + "\n";
str += "import " + C.class.getCanonicalName() + "\n";
str += "import " + D.class.getCanonicalName() + "\n";
str += "import " + E.class.getCanonicalName() + "\n";
str += "import " + F.class.getCanonicalName() + "\n";
str += "import " + G.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
str += "rule rule1 when \n";
str += " A() \n";
str += " exists( B() and C() ) \n";
str += " exists( eval(1==1) ) \n";
str += " D() \n";
str += "then \n";
str += "end \n";
str += "rule rule2 when \n";
str += " A() \n";
str += " exists( B() and C() ) \n";
str += " exists( eval(1==1) ) \n";
str += " E() \n";
str += "then \n";
str += "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
assertEquals(2, liaNode.getSinkPropagator().size());
JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
ExistsNode exists1n = (ExistsNode) liaNode.getSinkPropagator().getSinks()[1];
EvalConditionNode evalNode = (EvalConditionNode) exists1n.getSinkPropagator().getSinks()[0];
ExistsNode exists2n = (ExistsNode) exists1n.getSinkPropagator().getSinks()[1];
JoinNode dNode = (JoinNode) exists2n.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn = (RuleTerminalNode) dNode.getSinkPropagator().getSinks()[0];
wm.insert(new A());
PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn);
assertEquals(4, pmem.getSegmentMemories().length);
// the exists eval segment does not need to be linked in
assertEquals(11, pmem.getAllLinkedMaskTest());
RightInputAdapterNode.RiaNodeMemory riaMem = (RightInputAdapterNode.RiaNodeMemory) wm.getNodeMemory((MemoryFactory) exists1n.getRightInput());
// second segment must be linked in
assertEquals(2, riaMem.getRiaPathMemory().getAllLinkedMaskTest());
wm.insert(new B());
wm.insert(new C());
assertEquals(2, riaMem.getRiaPathMemory().getSegmentMemories().length);
riaMem = (RightInputAdapterNode.RiaNodeMemory) wm.getNodeMemory((MemoryFactory) exists2n.getRightInput());
// no segments to be linked in
assertEquals(0, riaMem.getRiaPathMemory().getAllLinkedMaskTest());
}
Aggregations