use of org.drools.core.reteoo.LeftTupleSink in project drools by kiegroup.
the class FromTest method testFromSharing.
@Test
public void testFromSharing() {
// Keeping original test as non-property reactive by default, just allowed.
String drl = "import " + ListsContainer.class.getCanonicalName() + "\n" + "global java.util.List output1;\n" + "global java.util.List output2;\n" + "rule R1 when\n" + " ListsContainer( $list : list1 )\n" + " $s : String( length == 2 ) from $list\n" + "then\n" + " output1.add($s);\n" + "end\n" + "rule R2 when\n" + " ListsContainer( $list : list2 )\n" + " $s : String( length == 2 ) from $list\n" + "then\n" + " output2.add($s);\n" + "end\n" + "rule R3 when\n" + " ListsContainer( $list : list2 )\n" + " $s : String( length == 2 ) from $list\n" + "then\n" + " output2.add($s);\n" + "end\n";
KieBase kbase = new KieHelper(PropertySpecificOption.ALLOWED).addContent(drl, ResourceType.DRL).build();
KieSession ksession = kbase.newKieSession();
ReteDumper.dumpRete(kbase);
List<String> output1 = new ArrayList<String>();
ksession.setGlobal("output1", output1);
List<String> output2 = new ArrayList<String>();
ksession.setGlobal("output2", output2);
FactHandle fh = ksession.insert(new ListsContainer());
ksession.fireAllRules();
assertEquals("bb", output1.get(0));
assertEquals("22", output2.get(0));
assertEquals("22", output2.get(1));
EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNodes().values().iterator().next();
ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(ListsContainer.class));
// There is only 1 LIA
assertEquals(1, otn.getObjectSinkPropagator().size());
LeftInputAdapterNode lian = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
// There are only 2 FromNodes since R2 and R3 are sharing the second From
LeftTupleSink[] sinks = lian.getSinkPropagator().getSinks();
assertEquals(2, sinks.length);
// The first from has R1 has sink
assertEquals(1, sinks[0].getSinkPropagator().size());
// The second from has both R2 and R3 as sinks
assertEquals(2, sinks[1].getSinkPropagator().size());
}
use of org.drools.core.reteoo.LeftTupleSink in project drools by kiegroup.
the class NodePositionInPathTest method test.
@Test
public void test() {
String drl = "rule R1 when\n" + " Integer()\n" + " exists( Integer() and Integer() )\n" + " String()\n" + "then\n" + "end\n" + "rule R2 when\n" + " Integer()\n" + " exists( Integer() and String() )\n" + "then\n" + "end\n";
KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
ReteDumper.dumpRete(((InternalKnowledgeBase) kbase));
Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
LeftInputAdapterNode liaNode = null;
for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
Class<?> otnType = ((ClassObjectType) otn.getObjectType()).getClassType();
if (Integer.class == otnType) {
liaNode = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
}
}
assertEquals(0, liaNode.getPositionInPath());
LeftTupleSink[] liaSinks = liaNode.getSinkPropagator().getSinks();
BetaNode join1 = (BetaNode) liaSinks[0];
assertEquals(1, join1.getPositionInPath());
ExistsNode ex1 = (ExistsNode) liaSinks[1];
assertEquals(1, ex1.getPositionInPath());
BetaNode join2 = (BetaNode) ex1.getSinkPropagator().getSinks()[0];
assertEquals(2, join2.getPositionInPath());
RuleTerminalNode rtn1 = (RuleTerminalNode) join2.getSinkPropagator().getSinks()[0];
assertEquals(3, rtn1.getPositionInPath());
ExistsNode ex2 = (ExistsNode) liaSinks[2];
assertEquals(1, ex2.getPositionInPath());
RuleTerminalNode rtn2 = (RuleTerminalNode) ex2.getSinkPropagator().getSinks()[0];
assertEquals(2, rtn2.getPositionInPath());
BetaNode join3 = (BetaNode) join1.getSinkPropagator().getSinks()[0];
assertEquals(2, join3.getPositionInPath());
RightInputAdapterNode ria1 = (RightInputAdapterNode) join3.getSinkPropagator().getSinks()[0];
assertEquals(3, ria1.getPositionInPath());
BetaNode join4 = (BetaNode) join1.getSinkPropagator().getSinks()[1];
assertEquals(2, join4.getPositionInPath());
RightInputAdapterNode ria2 = (RightInputAdapterNode) join4.getSinkPropagator().getSinks()[0];
assertEquals(3, ria2.getPositionInPath());
LeftTupleNode[] rtn1PathNodes = rtn1.getPathNodes();
assertEquals(4, rtn1PathNodes.length);
checkNodePosition(rtn1PathNodes, liaNode);
checkNodePosition(rtn1PathNodes, ex1);
checkNodePosition(rtn1PathNodes, join2);
checkNodePosition(rtn1PathNodes, rtn1);
LeftTupleNode[] rtn2PathNodes = rtn2.getPathNodes();
assertEquals(3, rtn2PathNodes.length);
checkNodePosition(rtn2PathNodes, liaNode);
checkNodePosition(rtn2PathNodes, ex2);
checkNodePosition(rtn2PathNodes, rtn2);
LeftTupleNode[] ria1PathNodes = ria1.getPathNodes();
assertEquals(4, ria1PathNodes.length);
checkNodePosition(ria1PathNodes, liaNode);
checkNodePosition(ria1PathNodes, join1);
checkNodePosition(ria1PathNodes, join3);
checkNodePosition(ria1PathNodes, ria1);
LeftTupleNode[] ria2PathNodes = ria2.getPathNodes();
assertEquals(4, ria2PathNodes.length);
checkNodePosition(ria2PathNodes, liaNode);
checkNodePosition(ria2PathNodes, join1);
checkNodePosition(ria2PathNodes, join4);
checkNodePosition(ria2PathNodes, ria2);
}
use of org.drools.core.reteoo.LeftTupleSink in project drools by kiegroup.
the class AlphaNetworkModifyTest method testModifyWithLiaToAcc.
@Test
public void testModifyWithLiaToAcc() {
// technically you can't have a modify with InitialFactImpl
// But added test for completeness
String str = "";
str += "package org.simple \n";
str += "import " + Person.class.getCanonicalName() + "\n";
str += "import " + Cheese.class.getCanonicalName() + "\n";
str += "import " + Cat.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
str += "rule x1 \n";
str += "when \n";
str += " Object() from accumulate( $p : Person() and Cheese(), collectList( $p ) )\n";
str += " Person() \n";
str += "then \n";
str += "end \n";
str += "rule x2 \n";
str += "when \n";
str += " Object() from accumulate( $ch : Cheese(), collectList( $ch ) )\n";
str += " Person() \n";
str += "then \n";
str += "end \n";
str += "rule x3 \n";
str += "when \n";
str += " Object() from accumulate( $ch : Cheese(), collectList( $ch ) )\n";
str += " Person() \n";
str += "then \n";
str += "end \n";
str += "rule x4 \n";
str += "when \n";
str += " Object() from accumulate( $ch : Cheese(), collectList( $ch ) )\n";
str += " Person() \n";
str += "then \n";
str += "end \n";
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession wm = kbase.newKieSession();
wm.fireAllRules();
ObjectTypeNode otnInit = getObjectTypeNode(kbase, "InitialFactImpl");
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) otnInit.getObjectSinkPropagator().getSinks()[0];
LeftTupleSink[] sinks = liaNode.getSinkPropagator().getSinks();
assertEquals(0, sinks[0].getLeftInputOtnId().getId());
assertEquals(1, sinks[1].getLeftInputOtnId().getId());
assertEquals(2, sinks[2].getLeftInputOtnId().getId());
ObjectTypeNode otnPerson = getObjectTypeNode(kbase, "Person");
ObjectTypeNode otnCheese = getObjectTypeNode(kbase, "Cheese");
assertEquals(0, otnPerson.getOtnIdCounter());
assertEquals(0, otnCheese.getOtnIdCounter());
wm.insert(new Person());
wm.insert(new Cheese());
wm.fireAllRules();
assertEquals(3, otnPerson.getOtnIdCounter());
assertEquals(2, otnCheese.getOtnIdCounter());
}
use of org.drools.core.reteoo.LeftTupleSink in project drools by kiegroup.
the class AccumulateTest method testAccumulateWithSameSubnetwork.
// (timeout = 10000)
@Test
public void testAccumulateWithSameSubnetwork() throws Exception {
String rule = "package org.drools.compiler.test;\n" + "import org.drools.compiler.Cheese;\n" + "import org.drools.compiler.Person;\n" + "global java.util.List list; \n" + "rule r1 salience 100 \n" + " when\n" + " $person : Person( name == 'Alice', $likes : likes )\n" + " $total : Number() from accumulate( $p : Person(likes != $likes, $l : likes) and $c : Cheese( type == $l ),\n" + " min($c.getPrice()) )\n" + " then\n" + " list.add( 'r1' + ':' + $total);\n" + "end\n" + "rule r2 \n" + " when\n" + " $person : Person( name == 'Alice', $likes : likes )\n" + " $total : Number() from accumulate( $p : Person(likes != $likes, $l : likes) and $c : Cheese( type == $l ),\n" + " max($c.getPrice()) )\n" + " then\n" + " list.add( 'r2' + ':' + $total);\n" + "end\n" + "";
// read in the source
KieBase kbase = loadKnowledgeBaseFromString(rule);
KieSession wm = createKnowledgeSession(kbase);
List list = new ArrayList();
wm.setGlobal("list", list);
// Check the network formation, to ensure the RiaNode is shared.
ObjectTypeNode cheeseOtn = LinkingTest.getObjectTypeNode(kbase, Cheese.class);
ObjectSink[] oSinks = cheeseOtn.getObjectSinkPropagator().getSinks();
assertEquals(1, oSinks.length);
JoinNode cheeseJoin = (JoinNode) oSinks[0];
LeftTupleSink[] ltSinks = cheeseJoin.getSinkPropagator().getSinks();
assertEquals(1, ltSinks.length);
RightInputAdapterNode rian = (RightInputAdapterNode) ltSinks[0];
// RiaNode is shared, if this has two outputs
assertEquals(2, rian.getObjectSinkPropagator().size());
wm.insert(new Cheese("stilton", 10));
wm.insert(new Person("Alice", "brie"));
wm.insert(new Person("Bob", "stilton"));
wm.fireAllRules();
assertEquals(2, list.size());
assertEquals("r1:10", list.get(0));
assertEquals("r2:10", list.get(1));
}
use of org.drools.core.reteoo.LeftTupleSink in project drools by kiegroup.
the class AlphaNetworkModifyTest method testModifyWithLiaToFrom.
@Test
public void testModifyWithLiaToFrom() {
// technically you can't have a modify with InitialFactImpl
// But added test for completeness
String str = "";
str += "package org.simple \n";
str += "import " + Person.class.getCanonicalName() + "\n";
str += "import " + Cheese.class.getCanonicalName() + "\n";
str += "import " + Cat.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
str += "rule x1 \n";
str += "when \n";
str += " $pe : Person() from list\n";
str += "then \n";
str += "end \n";
str += "rule x2 \n";
str += "when \n";
str += " $ch : Cheese() from list\n";
str += "then \n";
str += "end \n";
str += "rule x3 \n";
str += "when \n";
str += " $ch : Cheese() from list\n";
str += "then \n";
str += "end \n";
str += "rule x4 \n";
str += "when \n";
str += " $ch : Cheese() from list\n";
str += " eval( $ch != null ) \n";
str += "then \n";
str += "end \n";
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession wm = kbase.newKieSession();
wm.fireAllRules();
ObjectTypeNode otnInit = getObjectTypeNode(kbase, "InitialFactImpl");
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) otnInit.getObjectSinkPropagator().getSinks()[0];
LeftTupleSink[] sinks = liaNode.getSinkPropagator().getSinks();
assertEquals(2, sinks.length);
assertEquals(0, sinks[0].getLeftInputOtnId().getId());
assertEquals(1, sinks[1].getLeftInputOtnId().getId());
}
Aggregations