use of org.drools.core.reteoo.RuleTerminalNode in project drools by kiegroup.
the class MarshallingTest method testEmptyRule.
@Test
public void testEmptyRule() throws Exception {
String rule = "package org.drools.compiler.test;\n";
rule += "global java.util.List list\n";
rule += "rule \"Rule 1\"\n";
rule += "when\n";
rule += "then\n";
rule += " list.add( \"fired\" );\n";
rule += "end";
KieBase kBase = loadKnowledgeBaseFromString(rule);
// Make sure the rete node map is created correctly
Map<Integer, BaseNode> nodes = RuleBaseNodes.getNodeMap((InternalKnowledgeBase) kBase);
assertEquals(2, nodes.size());
assertEquals("InitialFactImpl", ((ClassObjectType) ((ObjectTypeNode) nodes.get(2)).getObjectType()).getClassType().getSimpleName());
assertEquals("Rule 1", ((RuleTerminalNode) nodes.get(4)).getRule().getName());
KieSession session = kBase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
session.fireAllRules();
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
assertEquals(1, ((List) session.getGlobal("list")).size());
assertEquals("fired", ((List) session.getGlobal("list")).get(0));
}
use of org.drools.core.reteoo.RuleTerminalNode in project drools by kiegroup.
the class SegmentCreationTest method testBranchCESingleSegment.
@Test
public void testBranchCESingleSegment() throws Exception {
KieBase kbase = buildKnowledgeBase(" $a : A() \n" + " if ( $a != null ) do[t1] \n" + " B() \n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.class);
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
ConditionalBranchNode cen1Node = (ConditionalBranchNode) liaNode.getSinkPropagator().getSinks()[0];
JoinNode bNode = (JoinNode) cen1Node.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn1 = (RuleTerminalNode) bNode.getSinkPropagator().getSinks()[0];
FactHandle bFh = wm.insert(new LinkingTest.B());
wm.flushPropagations();
LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
SegmentMemory smem = liaMem.getSegmentMemory();
assertEquals(1, smem.getAllLinkedMaskTest());
// B links, but it will not trigger mask
assertEquals(4, smem.getLinkedNodeMask());
assertFalse(smem.isSegmentLinked());
PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn1);
assertEquals(1, pmem.getAllLinkedMaskTest());
assertEquals(0, pmem.getLinkedSegmentMask());
assertFalse(pmem.isRuleLinked());
wm.insert(new LinkingTest.A());
wm.flushPropagations();
// A links in segment
assertEquals(5, smem.getLinkedNodeMask());
assertTrue(smem.isSegmentLinked());
assertEquals(1, pmem.getLinkedSegmentMask());
assertTrue(pmem.isRuleLinked());
// retract B does not unlink the rule
wm.delete(bFh);
wm.flushPropagations();
assertEquals(1, pmem.getLinkedSegmentMask());
assertTrue(pmem.isRuleLinked());
}
use of org.drools.core.reteoo.RuleTerminalNode 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.reteoo.RuleTerminalNode 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.reteoo.RuleTerminalNode 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());
}
Aggregations