use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.
the class LinkingTest method testNestedSubNetwork.
@Test
public void testNestedSubNetwork() 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 += " B() \n";
str += " exists( C() and D() and exists( E() and F() ) ) \n";
str += " G() \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(2, dNode.getSinkPropagator().size());
ExistsNode exists2n = (ExistsNode) dNode.getSinkPropagator().getSinks()[1];
JoinNode eNode = (JoinNode) dNode.getSinkPropagator().getSinks()[0];
JoinNode fNode = (JoinNode) eNode.getSinkPropagator().getSinks()[0];
RightInputAdapterNode riaNode2 = (RightInputAdapterNode) fNode.getSinkPropagator().getSinks()[0];
assertEquals(exists2n, riaNode2.getObjectSinkPropagator().getSinks()[0]);
RightInputAdapterNode riaNode1 = (RightInputAdapterNode) exists2n.getSinkPropagator().getSinks()[0];
assertEquals(exists1n, riaNode1.getObjectSinkPropagator().getSinks()[0]);
JoinNode gNode = (JoinNode) exists1n.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn = (RuleTerminalNode) gNode.getSinkPropagator().getSinks()[0];
wm.insert(new A());
wm.insert(new B());
wm.insert(new C());
wm.insert(new D());
wm.insert(new F());
wm.insert(new G());
PathMemory rs = (PathMemory) wm.getNodeMemory(rtn);
assertFalse(rs.isRuleLinked());
FactHandle fhE1 = wm.insert(new E());
FactHandle fhE2 = wm.insert(new E());
wm.flushPropagations();
assertTrue(rs.isRuleLinked());
wm.retract(fhE1);
wm.flushPropagations();
assertTrue(rs.isRuleLinked());
wm.retract(fhE2);
wm.flushPropagations();
assertFalse(rs.isRuleLinked());
}
use of org.drools.core.common.InternalWorkingMemory 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.common.InternalWorkingMemory in project drools by kiegroup.
the class LinkingTest method testSubNetworkSharing.
@Test
public void testSubNetworkSharing() throws Exception {
// Checks the network is correctly formed, with sharing
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 liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
assertEquals(3, liaNode.getSinkPropagator().size());
ExistsNode existsNode2 = (ExistsNode) liaNode.getSinkPropagator().getSinks()[1];
ExistsNode existsNode3 = (ExistsNode) liaNode.getSinkPropagator().getSinks()[2];
JoinNode joinNodeB = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
assertSame(joinNodeB.getRightInput(), getObjectTypeNode(kbase, B.class));
JoinNode joinNodeC = (JoinNode) joinNodeB.getSinkPropagator().getSinks()[0];
assertSame(joinNodeC.getRightInput(), getObjectTypeNode(kbase, C.class));
assertEquals(2, joinNodeC.getSinkPropagator().size());
JoinNode joinNodeD = (JoinNode) joinNodeC.getSinkPropagator().getSinks()[0];
assertSame(joinNodeD.getRightInput(), getObjectTypeNode(kbase, D.class));
assertEquals(2, joinNodeD.getSinkPropagator().size());
assertSame(existsNode2, ((RightInputAdapterNode) joinNodeC.getSinkPropagator().getSinks()[1]).getObjectSinkPropagator().getSinks()[0]);
assertSame(existsNode3, ((RightInputAdapterNode) joinNodeD.getSinkPropagator().getSinks()[1]).getObjectSinkPropagator().getSinks()[0]);
}
use of org.drools.core.common.InternalWorkingMemory 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.common.InternalWorkingMemory in project drools by kiegroup.
the class ActivationIteratorTest method testFilteredEagerEvaluation.
@Test(timeout = 10000)
public void testFilteredEagerEvaluation() throws Exception {
String str = "package org.simple \n" + "rule xxx @Propagation(EAGER) \n" + "when \n" + " $s : String()\n" + "then \n" + "end \n" + "rule yyy @Propagation(EAGER) \n" + "when \n" + " $s : String()\n" + "then \n" + "end \n";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(new ForceEagerActivationOption.FILTERED(new ForceEagerActivationFilter() {
@Override
public boolean accept(Rule rule) {
return rule.getName().equals("xxx");
}
}));
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = createKnowledgeSession(kbase, conf);
final List list = new ArrayList();
AgendaEventListener agendaEventListener = new DefaultAgendaEventListener() {
public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
list.add("activated");
}
};
ksession.addEventListener(agendaEventListener);
ksession.insert("test");
((InternalWorkingMemory) ksession).flushPropagations();
assertEquals(1, list.size());
}
Aggregations