use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.
the class SegmentCreationTest method testSingleSharedEmptyLhs.
@Test
public void testSingleSharedEmptyLhs() throws Exception {
KieBase kbase = buildKnowledgeBase(" ", " ");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
ObjectTypeNode aotn = getObjectTypeNode(kbase, InitialFactImpl.class);
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
RuleTerminalNode rtn1 = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn2 = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[1];
wm.insert(new LinkingTest.A());
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());
// each RTN is in it's own segment
SegmentMemory rtnSmem1 = smem.getFirst();
assertEquals(rtn1, rtnSmem1.getRootNode());
assertEquals(rtn1, rtnSmem1.getTipNode());
SegmentMemory rtnSmem2 = rtnSmem1.getNext();
assertEquals(rtn2, rtnSmem2.getRootNode());
assertEquals(rtn2, rtnSmem2.getTipNode());
}
use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.
the class AddRuleTest method testPopulatedSharedLiaNode.
@Test
public void testPopulatedSharedLiaNode() throws Exception {
InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", " A() B(1;) C() X() 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 X(1));
wm.insert(new E(1));
wm.fireAllRules();
assertEquals(3, list.size());
kbase1.addPackages(buildKnowledgePackage("r2", " a : A() B(2;) C() X() 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.common.InternalWorkingMemory in project drools by kiegroup.
the class AddRuleTest method testPopulatedRuleMidwayShare.
@Test
public void testPopulatedRuleMidwayShare() throws Exception {
InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", " a : A() B() C(1;) X() 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 C(1));
wm.insert(new C(2));
wm.insert(new X(1));
wm.insert(new E(1));
wm.fireAllRules();
assertEquals(3, list.size());
kbase1.addPackages(buildKnowledgePackage("r2", " a : A() B() C(2;) X() E()\n"));
ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
JoinNode c1Node = (JoinNode) bNode.getSinkPropagator().getFirstLeftTupleSink();
JoinNode c2Node = (JoinNode) bNode.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());
}
use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.
the class AddRuleTest method testPopulatedMultipleShares.
@Test
public void testPopulatedMultipleShares() throws Exception {
InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", " A(1;) A(2;) B(1;) B(2;) C(1;) X() 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(2));
wm.insert(new A(3));
wm.insert(new B(1));
wm.insert(new B(2));
wm.insert(new C(1));
wm.insert(new C(2));
wm.insert(new X(1));
wm.insert(new E(1));
wm.fireAllRules();
assertEquals(2, list.size());
kbase1.addPackages(buildKnowledgePackage("r2", " A(1;) A(2;) B(1;) B(2;) C(2;) X() E()\n"));
kbase1.addPackages(buildKnowledgePackage("r3", " A(1;) A(3;) B(1;) B(2;) C(2;) X() E()\n"));
wm.fireAllRules();
System.out.println(list);
assertEquals(5, list.size());
assertEquals("r1", ((Match) list.get(0)).getRule().getName());
assertEquals("r1", ((Match) list.get(1)).getRule().getName());
// only one A3
assertEquals("r3", ((Match) list.get(2)).getRule().getName());
assertEquals("r2", ((Match) list.get(3)).getRule().getName());
assertEquals("r2", ((Match) list.get(4)).getRule().getName());
}
use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.
the class AddRuleTest method testPopulatedSingleRuleNoSharingWithSubnetworkAtStart.
@Test
public void testPopulatedSingleRuleNoSharingWithSubnetworkAtStart() throws Exception {
KieServices ks = KieServices.get();
KieContainer kieContainer = KieUtil.getKieContainerFromDrls(kieBaseTestConfiguration, KieSessionTestConfiguration.STATEFUL_PSEUDO);
InternalWorkingMemory wm = (InternalWorkingMemory) kieContainer.newKieSession();
wm.insert(new A(1));
wm.insert(new A(2));
wm.insert(new X(1));
wm.insert(new E(1));
wm.insert(new C(2));
wm.fireAllRules();
String rule = buildKnowledgePackageDrl("r1", " A() not( B() and C() ) X() E()\n");
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "testPopulatedSingleRuleNoSharingWithSubnetworkAtStart", "2.0.0");
KieUtil.getKieModuleFromDrls(releaseId2, kieBaseTestConfiguration, KieSessionTestConfiguration.STATEFUL_PSEUDO, new HashMap<>(), rule);
kieContainer.updateToVersion(releaseId2);
List list = new ArrayList();
wm.setGlobal("list", list);
ObjectTypeNode aotn = getObjectTypeNode(wm.getKnowledgeBase().getRete(), A.class);
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
LiaNodeMemory lm = wm.getNodeMemory(liaNode);
SegmentMemory sm = lm.getSegmentMemory();
assertNull(sm.getStagedLeftTuples().getInsertFirst());
SegmentMemory subSm = sm.getFirst();
SegmentMemory mainSm = subSm.getNext();
assertNotNull(subSm.getStagedLeftTuples().getInsertFirst());
assertNotNull(subSm.getStagedLeftTuples().getInsertFirst().getStagedNext());
assertNull(subSm.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
assertNotNull(mainSm.getStagedLeftTuples().getInsertFirst());
assertNotNull(mainSm.getStagedLeftTuples().getInsertFirst().getStagedNext());
assertNull(mainSm.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
wm.fireAllRules();
assertNull(subSm.getStagedLeftTuples().getInsertFirst());
assertNull(mainSm.getStagedLeftTuples().getInsertFirst());
assertEquals(2, list.size());
assertEquals("r1", ((Match) list.get(0)).getRule().getName());
}
Aggregations