use of org.drools.core.reteoo.BetaMemory in project drools by kiegroup.
the class IndexingTest method testBuildsIndexedMemory.
@Test(timeout = 10000)
public void testBuildsIndexedMemory() {
// tests indexes are correctly built
String drl = "";
drl += "package org.drools.compiler.test\n";
drl += "import org.drools.compiler.Person\n";
drl += "global java.util.List list\n";
drl += "rule test1\n";
drl += "when\n";
drl += " $p1 : Person($name : name )\n";
// indexed
drl += " $p2 : Person(name == $name)\n";
// indexed
drl += " $p3 : Person(name == $p1.name)\n";
// not indexed
drl += " $p4 : Person(address.street == $p1.address.street)\n";
// indexed
drl += " $p5 : Person(address.street == $p1.name)\n";
// drl += " $p6 : Person( $name == name)\n"; // not indexed and won't compile
// indexed
drl += " $p7 : Person(addresses[\"key\"].street == $p1.name)\n";
// indexed
drl += " $p8 : Person(addresses[0].street == $p1.name)\n";
// not indexed
drl += " $p9 : Person(name == $p1.address.street)\n";
// indexed
drl += " $p10 : Person(addresses[0].street + 'xx' == $p1.name)\n";
// not indexed
drl += " $p11 : Person(addresses[$p1].street == $p1.name)\n";
drl += "then\n";
drl += "end\n";
KieBase kbase = loadKnowledgeBaseFromString(drl);
ObjectTypeNode node = getObjectTypeNode(kbase, Person.class);
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
// $p2
JoinNode j2 = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
// $p3
JoinNode j3 = (JoinNode) j2.getSinkPropagator().getSinks()[0];
// $p4
JoinNode j4 = (JoinNode) j3.getSinkPropagator().getSinks()[0];
// $p5
JoinNode j5 = (JoinNode) j4.getSinkPropagator().getSinks()[0];
// JoinNode j6 = ( JoinNode ) j5.getSinkPropagator().getSinks()[0]; // $p6 // won't compile
// $p7
JoinNode j7 = (JoinNode) j5.getSinkPropagator().getSinks()[0];
// $p8
JoinNode j8 = (JoinNode) j7.getSinkPropagator().getSinks()[0];
// $p9
JoinNode j9 = (JoinNode) j8.getSinkPropagator().getSinks()[0];
// $p10
JoinNode j10 = (JoinNode) j9.getSinkPropagator().getSinks()[0];
// $p11
JoinNode j11 = (JoinNode) j10.getSinkPropagator().getSinks()[0];
SingleBetaConstraints c = (SingleBetaConstraints) j2.getRawConstraints();
assertEquals("$name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
assertTrue(c.isIndexed());
BetaMemory bm = (BetaMemory) wm.getNodeMemory(j2);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
c = (SingleBetaConstraints) j3.getRawConstraints();
assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
assertTrue(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j3);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
c = (SingleBetaConstraints) j4.getRawConstraints();
assertEquals("$p1", c.getConstraint().getRequiredDeclarations()[0].getIdentifier());
assertFalse(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j4);
assertTrue(bm.getLeftTupleMemory() instanceof TupleList);
assertTrue(bm.getRightTupleMemory() instanceof TupleList);
c = (SingleBetaConstraints) j5.getRawConstraints();
assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
assertTrue(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j5);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
// won't compile
// c = ( SingleBetaConstraints ) j6.getRawConstraints();
// assertEquals( "name", ((VariableConstraint)c.getConstraint()).getRequiredDeclarations()[0].getIdentifier() );
// assertFalse( c.isIndexed() );
// bm = ( BetaMemory ) wm.getNodeMemory( j6 );
// assertTrue( bm.getLeftTupleMemory() instanceof LeftTupleList );
// assertTrue( bm.getRightTupleMemory() instanceof RightTupleList );
c = (SingleBetaConstraints) j7.getRawConstraints();
assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
assertTrue(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j7);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
c = (SingleBetaConstraints) j8.getRawConstraints();
assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
assertTrue(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j8);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
c = (SingleBetaConstraints) j9.getRawConstraints();
assertEquals("$p1", c.getConstraint().getRequiredDeclarations()[0].getIdentifier());
assertFalse(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j9);
assertTrue(bm.getLeftTupleMemory() instanceof TupleList);
assertTrue(bm.getRightTupleMemory() instanceof TupleList);
c = (SingleBetaConstraints) j10.getRawConstraints();
assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
assertTrue(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j10);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
c = (SingleBetaConstraints) j11.getRawConstraints();
assertEquals("$p1", c.getConstraint().getRequiredDeclarations()[0].getIdentifier());
assertFalse(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j11);
assertTrue(bm.getLeftTupleMemory() instanceof TupleList);
assertTrue(bm.getRightTupleMemory() instanceof TupleList);
}
use of org.drools.core.reteoo.BetaMemory in project drools by kiegroup.
the class LinkingTest method testJoinNodes.
@Test
public void testJoinNodes() 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 += " $b : B() \n";
str += " $c : C() \n";
str += "then \n";
str += " list.add( $a.getValue() + \":\"+ $b.getValue() + \":\" + $c.getValue() ); \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 aotn = getObjectTypeNode(kbase, A.class);
ObjectTypeNode botn = getObjectTypeNode(kbase, B.class);
ObjectTypeNode cotn = getObjectTypeNode(kbase, C.class);
InternalWorkingMemory wm = (InternalWorkingMemory) kbase.newKieSession();
List list = new ArrayList();
wm.setGlobal("list", list);
for (int i = 0; i < 3; i++) {
wm.insert(new A(i));
}
for (int i = 0; i < 3; i++) {
wm.insert(new B(i));
}
for (int i = 0; i < 29; i++) {
wm.insert(new C(i));
}
wm.flushPropagations();
LeftInputAdapterNode aNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
JoinNode bNode = (JoinNode) aNode.getSinkPropagator().getSinks()[0];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
LiaNodeMemory amem = (LiaNodeMemory) wm.getNodeMemory(aNode);
BetaMemory bmem = (BetaMemory) wm.getNodeMemory(bNode);
BetaMemory cmem = (BetaMemory) wm.getNodeMemory(cNode);
// amem.getSegmentMemory().getStagedLeftTuples().insertSize() == 3
assertNotNull(amem.getSegmentMemory().getStagedLeftTuples().getInsertFirst());
assertNotNull(amem.getSegmentMemory().getStagedLeftTuples().getInsertFirst().getStagedNext());
assertNotNull(amem.getSegmentMemory().getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
assertNull(amem.getSegmentMemory().getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext().getStagedNext());
// assertEquals( 3, bmem.getStagedRightTuples().insertSize() );
assertNotNull(bmem.getStagedRightTuples().getInsertFirst());
assertNotNull(bmem.getStagedRightTuples().getInsertFirst().getStagedNext());
assertNotNull(bmem.getStagedRightTuples().getInsertFirst().getStagedNext().getStagedNext());
assertNull(bmem.getStagedRightTuples().getInsertFirst().getStagedNext().getStagedNext().getStagedNext());
wm.fireAllRules();
assertNull(amem.getSegmentMemory().getStagedLeftTuples().getInsertFirst());
assertNull(bmem.getStagedRightTuples().getInsertFirst());
assertNull(cmem.getStagedRightTuples().getInsertFirst());
assertEquals(261, list.size());
assertTrue(list.contains("2:2:14"));
assertTrue(list.contains("1:0:6"));
assertTrue(list.contains("0:1:1"));
assertTrue(list.contains("2:2:14"));
assertTrue(list.contains("0:0:25"));
}
use of org.drools.core.reteoo.BetaMemory in project drools by kiegroup.
the class LinkingTest method testSubNetworkSharingMemories.
@Test
public void testSubNetworkSharingMemories() throws Exception {
// checks the memory sharing works, and linking, uses the already checked network from testSubNetworkSharing
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 += "global java.util.List list \n";
str += "rule rule1 when \n";
str += " A() \n";
str += " B() \n";
str += " C() \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 += " D() \n";
str += "then \n";
str += "end \n";
str += "rule rule3 when \n";
str += " A() \n";
str += " exists( B() and C() and D() ) \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 liaNodeA = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
ExistsNode existsNode2 = (ExistsNode) liaNodeA.getSinkPropagator().getSinks()[1];
ExistsNode existsNode3 = (ExistsNode) liaNodeA.getSinkPropagator().getSinks()[2];
JoinNode joinNodeB = (JoinNode) liaNodeA.getSinkPropagator().getSinks()[0];
JoinNode joinNodeC = (JoinNode) joinNodeB.getSinkPropagator().getSinks()[0];
JoinNode joinNodeD1 = (JoinNode) joinNodeC.getSinkPropagator().getSinks()[0];
JoinNode joinNodeD2 = (JoinNode) existsNode2.getSinkPropagator().getSinks()[0];
JoinNode joinNodeE = (JoinNode) existsNode3.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn1 = (RuleTerminalNode) joinNodeD1.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn2 = (RuleTerminalNode) joinNodeD2.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn3 = (RuleTerminalNode) joinNodeE.getSinkPropagator().getSinks()[0];
FactHandle fha = wm.insert(new A());
wm.insert(new B());
wm.insert(new C());
wm.insert(new D());
wm.flushPropagations();
BetaMemory bm = null;
LiaNodeMemory liam = (LiaNodeMemory) wm.getNodeMemory(liaNodeA);
BetaMemory bm1 = (BetaMemory) wm.getNodeMemory(joinNodeB);
BetaMemory bm2 = (BetaMemory) wm.getNodeMemory(joinNodeC);
BetaMemory bm3 = (BetaMemory) wm.getNodeMemory(joinNodeD1);
assertEquals(1, liam.getNodePosMaskBit());
assertEquals(1, bm1.getNodePosMaskBit());
assertEquals(2, bm2.getNodePosMaskBit());
assertEquals(1, bm3.getNodePosMaskBit());
assertNotSame(liam.getSegmentMemory(), bm1.getSegmentMemory());
assertSame(bm1.getSegmentMemory(), bm2.getSegmentMemory());
assertNotSame(bm2.getSegmentMemory(), bm3.getSegmentMemory());
BetaMemory bm4 = (BetaMemory) wm.getNodeMemory(existsNode2);
BetaMemory bm5 = (BetaMemory) wm.getNodeMemory(joinNodeD2);
assertEquals(1, bm4.getNodePosMaskBit());
assertEquals(2, bm5.getNodePosMaskBit());
assertSame(bm4.getSegmentMemory(), bm5.getSegmentMemory());
PathMemory rs1 = (PathMemory) wm.getNodeMemory(rtn1);
PathMemory rs2 = (PathMemory) wm.getNodeMemory(rtn2);
PathMemory rs3 = (PathMemory) wm.getNodeMemory(rtn3);
assertTrue(rs1.isRuleLinked());
assertTrue(rs2.isRuleLinked());
// no E yet
assertFalse(rs3.isRuleLinked());
wm.insert(new E());
wm.flushPropagations();
BetaMemory bm6 = (BetaMemory) wm.getNodeMemory(existsNode3);
BetaMemory bm7 = (BetaMemory) wm.getNodeMemory(joinNodeE);
assertEquals(1, bm6.getNodePosMaskBit());
assertEquals(2, bm7.getNodePosMaskBit());
assertSame(bm6.getSegmentMemory(), bm7.getSegmentMemory());
assertTrue(rs1.isRuleLinked());
assertTrue(rs2.isRuleLinked());
assertTrue(rs3.isRuleLinked());
wm.retract(fha);
// need to have rules evalulated, for unlinking to happen
wm.fireAllRules();
assertFalse(rs1.isRuleLinked());
assertFalse(rs2.isRuleLinked());
assertFalse(rs3.isRuleLinked());
}
use of org.drools.core.reteoo.BetaMemory in project drools by kiegroup.
the class LinkingTest method testSubNetworkRiaLinking.
@Test
public void testSubNetworkRiaLinking() 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 += "global java.util.List list \n";
str += "rule rule1 when \n";
str += " A() \n";
str += " B() \n";
str += " exists( C() and D() ) \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(1, liaNode.getSinkPropagator().size());
JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
assertEquals(2, bNode.getSinkPropagator().size());
ExistsNode exists1n = (ExistsNode) bNode.getSinkPropagator().getSinks()[1];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
JoinNode dNode = (JoinNode) cNode.getSinkPropagator().getSinks()[0];
assertEquals(1, dNode.getSinkPropagator().size());
RightInputAdapterNode riaNode1 = (RightInputAdapterNode) dNode.getSinkPropagator().getSinks()[0];
JoinNode eNode = (JoinNode) exists1n.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn = (RuleTerminalNode) eNode.getSinkPropagator().getSinks()[0];
SegmentUtilities.createSegmentMemory(exists1n, wm);
BetaMemory existsBm = (BetaMemory) wm.getNodeMemory(exists1n);
assertEquals(0, existsBm.getSegmentMemory().getLinkedNodeMask());
FactHandle fhc = wm.insert(new C());
FactHandle fhd = wm.insert(new D());
wm.flushPropagations();
// exists is start of new segment
assertEquals(1, existsBm.getSegmentMemory().getLinkedNodeMask());
wm.retract(fhd);
wm.flushPropagations();
assertEquals(0, existsBm.getSegmentMemory().getLinkedNodeMask());
PathMemory rs = (PathMemory) wm.getNodeMemory(rtn);
assertFalse(rs.isRuleLinked());
wm.insert(new A());
wm.flushPropagations();
assertFalse(rs.isRuleLinked());
wm.insert(new B());
wm.flushPropagations();
assertFalse(rs.isRuleLinked());
wm.insert(new E());
wm.flushPropagations();
assertFalse(rs.isRuleLinked());
wm.insert(new D());
wm.flushPropagations();
assertTrue(rs.isRuleLinked());
wm.retract(fhc);
wm.flushPropagations();
assertFalse(rs.isRuleLinked());
wm.insert(new C());
wm.flushPropagations();
assertTrue(rs.isRuleLinked());
}
use of org.drools.core.reteoo.BetaMemory 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());
}
Aggregations