use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.
the class SegmentCreationTest method tesSubnetworkAfterShare.
@Test
public void tesSubnetworkAfterShare() throws Exception {
KieBase kbase = buildKnowledgeBase(" A() \n", " A() not ( B() and 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];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
RightInputAdapterNode riaNode = (RightInputAdapterNode) cNode.getSinkPropagator().getSinks()[0];
NotNode notNode = (NotNode) liaNode.getSinkPropagator().getSinks()[2];
RuleTerminalNode rtn2 = (RuleTerminalNode) notNode.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(riaNode, bSmem.getTipNode());
SegmentMemory notSmem = bSmem.getNext();
assertEquals(notNode, notSmem.getRootNode());
assertEquals(rtn2, notSmem.getTipNode());
// child segment is not yet initialised, so null
assertNull(bSmem.getFirst());
}
use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.
the class SegmentCreationTest method testSingleEmptyLhs.
@Test
public void testSingleEmptyLhs() throws Exception {
KieBase kbase = buildKnowledgeBase(" ");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
ObjectTypeNode aotn = getObjectTypeNode(kbase, InitialFactImpl.class);
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
RuleTerminalNode rtn = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
wm.insert(new LinkingTest.A());
wm.flushPropagations();
// LiaNode and Rule are in same segment
LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
SegmentMemory smem = liaMem.getSegmentMemory();
assertEquals(liaNode, smem.getRootNode());
assertEquals(rtn, smem.getTipNode());
assertNull(smem.getNext());
assertNull(smem.getFirst());
}
use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.
the class SegmentCreationTest method testSubnetworkNoSharing.
@Test
public void testSubnetworkNoSharing() throws Exception {
KieBase kbase = buildKnowledgeBase(" A() not ( B() and C() ) \n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.class);
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
RightInputAdapterNode riaNode = (RightInputAdapterNode) cNode.getSinkPropagator().getSinks()[0];
NotNode notNode = (NotNode) liaNode.getSinkPropagator().getSinks()[1];
RuleTerminalNode rtn1 = (RuleTerminalNode) notNode.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());
assertNull(smem.getNext());
smem = smem.getFirst();
// it's nested inside of smem, so lookup from wm
SegmentMemory bSmem = wm.getNodeMemory(bNode).getSegmentMemory();
assertEquals(smem, bSmem);
assertEquals(bNode, bSmem.getRootNode());
assertEquals(riaNode, bSmem.getTipNode());
BetaMemory bm = (BetaMemory) wm.getNodeMemory(notNode);
assertEquals(bm.getSegmentMemory(), smem.getNext());
// check subnetwork ref was made
assertEquals(bSmem, bm.getRiaRuleMemory().getSegmentMemory());
}
use of org.drools.core.common.InternalWorkingMemory 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.common.InternalWorkingMemory in project drools by kiegroup.
the class MarshallingTest method testSerializeWorkingMemoryAndRuleBase2.
@Test
public void testSerializeWorkingMemoryAndRuleBase2() throws Exception {
Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Serializable.drl");
InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
// serialise a hashmap with the RuleBase as a key
Map map = new HashMap();
map.put("x", kBase);
map = SerializationHelper.serializeObject(map);
kBase = (InternalKnowledgeBase) map.get("x");
KieSession session = kBase.newKieSession();
// serialise the working memory before population
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
kBase.addPackages(kpkgs);
session.setGlobal("list", new ArrayList());
final Person bob = new Person("bob");
session.insert(bob);
((InternalWorkingMemory) session).flushPropagations();
org.kie.api.definition.rule.Rule[] rules = (org.kie.api.definition.rule.Rule[]) kBase.getPackage("org.drools.compiler.test").getRules().toArray(new org.kie.api.definition.rule.Rule[0]);
assertEquals(4, rules.length);
assertEquals("match Person 1", rules[0].getName());
assertEquals("match Person 2", rules[1].getName());
assertEquals("match Person 3", rules[2].getName());
assertEquals("match Integer", rules[3].getName());
assertEquals(1, session.getObjects().size());
assertEquals(bob, IteratorToList.convert(session.getObjects().iterator()).get(0));
assertEquals(2, ((InternalAgenda) session.getAgenda()).agendaSize());
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
session.fireAllRules();
final List list = (List) session.getGlobal("list");
assertEquals(3, list.size());
// because of agenda-groups
assertEquals(new Integer(4), list.get(0));
assertEquals(2, session.getObjects().size());
assertTrue(IteratorToList.convert(session.getObjects().iterator()).contains(bob));
assertTrue(IteratorToList.convert(session.getObjects().iterator()).contains(new Person("help")));
}
Aggregations