use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class SegmentCreationTest method testSinglePattern.
@Test
public void testSinglePattern() throws Exception {
KieBase kbase = buildKnowledgeBase(" A() \n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.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.ObjectTypeNode in project drools by kiegroup.
the class PassiveQueryTest method testPassiveQueryUsingSegmentPropagator.
@Test
public void testPassiveQueryUsingSegmentPropagator() throws Exception {
String str = "global java.util.List list\n" + "query Q (Integer i)\n" + " String( this == i.toString() )\n" + "end\n" + "rule R1a @Propagation(IMMEDIATE) when\n" + " $i : Integer()\n" + " ?Q( $i; )\n" + "then\n" + " list.add( \"R1a\" );\n" + "end\n" + "rule R1b @Propagation(IMMEDIATE) when\n" + " $i : Integer()\n" + " ?Q( $i; )\n" + " Long( intValue == $i )\n" + "then\n" + " list.add( \"R1b\" );\n" + "end\n" + "rule R2 when\n" + " $i : Integer()\n" + " ?Q( $i; )\n" + "then\n" + " list.add( \"R2\" );\n" + "end\n";
KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
KieSession ksession = kbase.newKieSession();
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert(1L);
FactHandle fh = ksession.insert(1);
ksession.insert("1");
Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
LeftInputAdapterNode lia = null;
for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
if (Integer.class == otn.getObjectType().getValueType().getClassType()) {
lia = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
break;
}
}
LeftTupleSink[] sinks = lia.getSinkPropagator().getSinks();
QueryElementNode q1 = (QueryElementNode) sinks[0];
QueryElementNode q2 = (QueryElementNode) sinks[1];
InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
wm.flushPropagations();
Memory memory1 = wm.getNodeMemory(q1);
assertTrue(memory1.getSegmentMemory().getStagedLeftTuples().isEmpty());
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("R2", list.get(0));
list.clear();
ksession.delete(fh);
ksession.insert(1);
ksession.fireAllRules();
assertEquals(3, list.size());
}
use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class PathEndNodeTest method testSubNetworkSharing.
@Test
public void testSubNetworkSharing() throws Exception {
String str = "package org.test \n" + "rule xxx \n" + "when \n" + " $s : String()\n" + " not( Integer() and Long() )\n" + "then \n" + "end \n" + "rule yyy \n" + "when \n" + " $s : String()\n" + " not( Integer() and Long() )\n" + "then \n" + "end \n";
InternalKnowledgeBase kbase = (InternalKnowledgeBase) new KieHelper().addContent(str, ResourceType.DRL).build();
EntryPointNode epn = kbase.getRete().getEntryPointNode(EntryPointId.DEFAULT);
ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(Long.class));
BetaNode beta1 = (BetaNode) otn.getObjectSinkPropagator().getSinks()[0];
RightInputAdapterNode rian = (RightInputAdapterNode) beta1.getSinkPropagator().getSinks()[0];
BetaNode beta2 = (BetaNode) rian.getObjectSinkPropagator().getSinks()[0];
LeftTupleSink[] sinks = beta2.getSinkPropagator().getSinks();
RuleTerminalNode rtn1 = (RuleTerminalNode) sinks[0];
RuleTerminalNode rtn2 = (RuleTerminalNode) sinks[1];
assertEquals(3, rian.getPathEndNodes().length);
assertTrue(asList(rian.getPathEndNodes()).containsAll(asList(rtn1, rtn2, rian)));
kbase.removeRule("org.test", "xxx");
assertEquals(2, rian.getPathEndNodes().length);
RuleTerminalNode remainingRTN = rtn1.getRule().getName().equals("yyy") ? rtn1 : rtn2;
assertTrue(asList(rian.getPathEndNodes()).containsAll(asList(remainingRTN, rian)));
}
use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class PropertySpecificTest method testBetaWithConstraintsWithWatches.
@Test
public void testBetaWithConstraintsWithWatches() {
String rule1 = "$b : B( a == 15) @watch(c) A( a == 10, b == $b.b ) @watch(s)";
KieBase kbase = getKnowledgeBase(rule1);
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
ObjectTypeNode otn = getObjectTypeNode(kbase, "A");
assertNotNull(otn);
Class classType = ((ClassObjectType) otn.getObjectType()).getClassType();
List<String> sp = getSettableProperties(wm, otn);
AlphaNode alphaNode = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "s"), sp), alphaNode.getInferredMask());
BetaNode betaNode = (BetaNode) alphaNode.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("b", "s"), sp), betaNode.getRightDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "s"), sp), betaNode.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("b", "c"), sp), betaNode.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "c"), sp), betaNode.getLeftInferredMask());
otn = getObjectTypeNode(kbase, "B");
alphaNode = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "c"), sp), alphaNode.getInferredMask());
assertEquals(calculatePositiveMask(classType, list("b", "c"), sp), betaNode.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "c"), sp), betaNode.getLeftInferredMask());
}
use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class PropertySpecificTest method testBetaWithConstraintsNoWatches.
@Test
public void testBetaWithConstraintsNoWatches() {
String rule1 = "$b : B(a == 15) A( a == 10, b == $b.b )";
KieBase kbase = getKnowledgeBase(rule1);
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
ObjectTypeNode otn = getObjectTypeNode(kbase, "A");
assertNotNull(otn);
Class classType = ((ClassObjectType) otn.getObjectType()).getClassType();
List<String> sp = getSettableProperties(wm, otn);
AlphaNode alphaNode = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b"), sp), alphaNode.getInferredMask());
BetaNode betaNode = (BetaNode) alphaNode.getObjectSinkPropagator().getSinks()[0];
// beta declares nothing
assertEquals(calculatePositiveMask(classType, list("b"), sp), betaNode.getRightDeclaredMask());
// beta infers from alpha
assertEquals(calculatePositiveMask(classType, list("a", "b"), sp), betaNode.getRightInferredMask());
otn = getObjectTypeNode(kbase, "B");
alphaNode = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b"), sp), alphaNode.getInferredMask());
assertEquals(calculatePositiveMask(classType, list("b"), sp), betaNode.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b"), sp), betaNode.getLeftInferredMask());
}
Aggregations