use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class BackwardChainingTest method testSubNetworksAndQueries.
@Test(timeout = 10000)
public void testSubNetworksAndQueries() throws Exception {
String str = "" + "package org.drools.compiler.test \n" + "import java.util.List\n" + "import java.util.ArrayList\n" + "import java.util.Map\n" + "import java.util.HashMap\n" + "global List list\n" + "dialect \"mvel\"\n" + "\n" + "declare Location\n" + " thing : String \n" + " location : String \n" + "end" + "\n" + "declare Edible\n" + " thing : String\n" + "end" + "\n" + "query whereFood( String x, String y ) \n" + " Location(x, y;) Edible(x;) \n" + "end\n" + "\n" + "query look(String place, List food ) \n" + " $s : String() // just here to give a OTN lookup point\n" + " food := List() from accumulate( whereFood(thing, place;) ," + " collectList( thing ) )\n" + " exists( whereFood(thing, place;) )\n" + " not( whereFood(thing, place;) and\n " + " String( this == $s ) from thing )\n" + "end\n" + "\n" + "rule init when\n" + "then\n" + " \n" + " insert( new Location(\"apple\", \"kitchen\") );\n" + " insert( new Location(\"crackers\", \"kitchen\") );\n" + " insert( new Location(\"broccoli\", \"kitchen\") );\n" + " insert( new Location(\"computer\", \"office\") );\n" + " insert( new Edible(\"apple\") );\n" + " insert( new Edible(\"crackers\") );\n" + "end\n" + "";
logger.debug(str);
KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(str));
// Get the accumulate node, so we can test it's memory later
// now check beta memory was correctly cleared
List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
ObjectTypeNode node = null;
for (ObjectTypeNode n : nodes) {
if (((ClassObjectType) n.getObjectType()).getClassType() == String.class) {
node = n;
break;
}
}
BetaNode stringBetaNode = (BetaNode) node.getObjectSinkPropagator().getSinks()[0];
QueryElementNode queryElementNode1 = (QueryElementNode) stringBetaNode.getSinkPropagator().getSinks()[0];
RightInputAdapterNode riaNode1 = (RightInputAdapterNode) queryElementNode1.getSinkPropagator().getSinks()[0];
AccumulateNode accNode = (AccumulateNode) riaNode1.getObjectSinkPropagator().getSinks()[0];
QueryElementNode queryElementNode2 = (QueryElementNode) accNode.getSinkPropagator().getSinks()[0];
RightInputAdapterNode riaNode2 = (RightInputAdapterNode) queryElementNode2.getSinkPropagator().getSinks()[0];
ExistsNode existsNode = (ExistsNode) riaNode2.getObjectSinkPropagator().getSinks()[0];
QueryElementNode queryElementNode3 = (QueryElementNode) existsNode.getSinkPropagator().getSinks()[0];
FromNode fromNode = (FromNode) queryElementNode3.getSinkPropagator().getSinks()[0];
RightInputAdapterNode riaNode3 = (RightInputAdapterNode) fromNode.getSinkPropagator().getSinks()[0];
NotNode notNode = (NotNode) riaNode3.getObjectSinkPropagator().getSinks()[0];
KieSession ksession = createKnowledgeSession(kbase);
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) ksession);
AccumulateMemory accMemory = (AccumulateMemory) wm.getNodeMemory(accNode);
BetaMemory existsMemory = (BetaMemory) wm.getNodeMemory(existsNode);
FromMemory fromMemory = (FromMemory) wm.getNodeMemory(fromNode);
BetaMemory notMemory = (BetaMemory) wm.getNodeMemory(notNode);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
ksession.setGlobal("list", list);
FactHandle fh = ksession.insert("bread");
ksession.fireAllRules();
final List food = new ArrayList();
QueryResults results = null;
// Execute normal query and check no subnetwork tuples are left behind
results = ksession.getQueryResults("look", new Object[] { "kitchen", Variable.v });
assertEquals(1, results.size());
for (org.kie.api.runtime.rule.QueryResultsRow row : results) {
food.addAll((Collection) row.get("food"));
logger.debug(row.get("food").toString());
}
assertEquals(2, food.size());
assertContains(new String[] { "crackers", "apple" }, food);
assertEquals(0, accMemory.getBetaMemory().getRightTupleMemory().size());
assertEquals(0, existsMemory.getRightTupleMemory().size());
assertEquals(0, fromMemory.getBetaMemory().getLeftTupleMemory().size());
assertEquals(0, notMemory.getRightTupleMemory().size());
// Now execute an open query and ensure the memory is left populated
food.clear();
final List foodUpdated = new ArrayList();
LiveQuery query = ksession.openLiveQuery("look", new Object[] { "kitchen", Variable.v }, new ViewChangedEventListener() {
public void rowUpdated(Row row) {
foodUpdated.addAll((Collection) row.get("food"));
}
public void rowDeleted(Row row) {
}
public void rowInserted(Row row) {
food.addAll((Collection) row.get("food"));
}
});
assertEquals(2, food.size());
assertContains(new String[] { "crackers", "apple" }, food);
assertEquals(2, accMemory.getBetaMemory().getRightTupleMemory().size());
assertEquals(2, existsMemory.getRightTupleMemory().size());
assertEquals(2, fromMemory.getBetaMemory().getLeftTupleMemory().size());
assertEquals(0, notMemory.getRightTupleMemory().size());
food.clear();
// Now try again, make sure it only delete's it's own tuples
results = ksession.getQueryResults("look", new Object[] { "kitchen", Variable.v });
assertEquals(1, results.size());
for (org.kie.api.runtime.rule.QueryResultsRow row : results) {
food.addAll((Collection) row.get("food"));
logger.debug(row.get("food").toString());
}
assertEquals(2, food.size());
assertContains(new String[] { "crackers", "apple" }, food);
assertEquals(2, accMemory.getBetaMemory().getRightTupleMemory().size());
assertEquals(2, existsMemory.getRightTupleMemory().size());
assertEquals(2, fromMemory.getBetaMemory().getLeftTupleMemory().size());
assertEquals(0, notMemory.getRightTupleMemory().size());
food.clear();
// do an update and check it's still memory size 2
// however this time the food should be empty, as 'crackers' now blocks the not.
ksession.update(fh, "crackers");
ksession.fireAllRules();
assertEquals(2, accMemory.getBetaMemory().getRightTupleMemory().size());
assertEquals(2, existsMemory.getRightTupleMemory().size());
assertEquals(2, fromMemory.getBetaMemory().getLeftTupleMemory().size());
assertEquals(1, notMemory.getRightTupleMemory().size());
assertEquals(0, foodUpdated.size());
// do an update and check it's still memory size 2
// this time
ksession.update(fh, "oranges");
ksession.fireAllRules();
assertEquals(2, accMemory.getBetaMemory().getRightTupleMemory().size());
assertEquals(2, existsMemory.getRightTupleMemory().size());
assertEquals(2, fromMemory.getBetaMemory().getLeftTupleMemory().size());
assertEquals(0, notMemory.getRightTupleMemory().size());
assertEquals(2, food.size());
assertContains(new String[] { "crackers", "apple" }, food);
// Close the open
query.close();
assertEquals(0, accMemory.getBetaMemory().getRightTupleMemory().size());
assertEquals(0, existsMemory.getRightTupleMemory().size());
assertEquals(0, fromMemory.getBetaMemory().getLeftTupleMemory().size());
assertEquals(0, notMemory.getRightTupleMemory().size());
}
use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class CepEspTest method testEventExpiration3.
@Test(timeout = 10000)
public void testEventExpiration3() throws Exception {
// read in the source
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_EventExpiration3.drl");
Map<ObjectType, ObjectTypeNode> objectTypeNodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes(EntryPointId.DEFAULT);
ObjectTypeNode node = objectTypeNodes.get(new ClassObjectType(StockTick.class));
assertNotNull(node);
// the expiration policy @expires(10m) should override the temporal operator usage
assertEquals(TimeIntervalParser.parse("10m")[0] + 1, node.getExpirationOffset());
}
use of org.drools.core.reteoo.ObjectTypeNode 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());
}
use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class AlphaNetworkModifyTest method testModifyWithLiaToEval.
@Test
public void testModifyWithLiaToEval() {
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() \n";
str += " $ch : Cheese() \n";
str += " $ca : Cat() \n";
str += "then \n";
str += "end \n";
str += "rule x2 \n";
str += "when \n";
str += " $ch : Cheese() \n";
str += " $ca : Cat() \n";
str += " $pe : Person() \n";
str += "then \n";
str += "end \n";
str += "rule x3 \n";
str += "when \n";
str += " $ch : Cheese() \n";
str += "then \n";
str += "end \n";
str += "rule x4 \n";
str += "when \n";
str += " $ch : Cheese() \n";
str += " eval( $ch != null ) \n";
str += "then \n";
str += "end \n";
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession wm = kbase.newKieSession();
ObjectTypeNode otnPerson = getObjectTypeNode(kbase, "Person");
ObjectTypeNode otnCheese = getObjectTypeNode(kbase, "Cheese");
ObjectTypeNode otnCat = getObjectTypeNode(kbase, "Cat");
assertEquals(0, otnPerson.getOtnIdCounter());
assertEquals(0, otnCheese.getOtnIdCounter());
assertEquals(0, otnCat.getOtnIdCounter());
wm.insert(new Person());
wm.insert(new Cat("yyy"));
wm.insert(new Cheese());
wm.fireAllRules();
assertEquals(2, otnPerson.getOtnIdCounter());
assertEquals(4, otnCheese.getOtnIdCounter());
assertEquals(2, otnCat.getOtnIdCounter());
}
use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.
the class RemoveRuleTest method testRemoveBigRule.
@Test
public void testRemoveBigRule() throws Exception {
// JBRULES-3496
final String str = "package org.drools.compiler.test\n" + "\n" + "declare SimpleFact\n" + " patientSpaceId : String\n" + " block : int\n" + "end\n" + "\n" + "declare SimpleMembership\n" + " patientSpaceId : String\n" + " listId : String\n" + "end\n" + "\n" + "declare SimplePatient\n" + " spaceId : String\n" + "end\n" + "\n" + "rule \"RTR - 47146 retract\"\n" + "agenda-group \"list membership\"\n" + "when\n" + " $listMembership0 : SimpleMembership( $listMembershipPatientSpaceIdRoot : patientSpaceId, ( listId != null && listId == \"47146\" ) )\n" + " not ( $patient0 : SimplePatient( $patientSpaceIdRoot : spaceId, spaceId != null && spaceId == $listMembershipPatientSpaceIdRoot ) \n" + " and ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 1 )\n" + " ) or ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 2 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 3 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 4 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 5 )\n" + " ) ) and ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 6 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 7 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 8 )\n" + " ) ) and ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 9 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 10 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 11 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 12 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 13 )\n" + " ) or ( (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 14 )\n" + " ) and (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 15 )\n" + " ) ) or ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 16 )\n" + " ) and ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 17 )\n" + " ) ) or ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 18 )\n" + " ) and (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 19 )\n" + " ) ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 20 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 21 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 22 )\n" + " ) or ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 23 )\n" + " ) and (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 24 )\n" + " ) ) ) and ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 25 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 26 )\n" + " ) ) )\n" + "then\n" + "end\n";
final Collection<KiePackage> kpgs = loadKnowledgePackagesFromString(str);
assertEquals(1, kpgs.size());
final InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
kbase.addPackages(kpgs);
kbase.removeKiePackage(kpgs.iterator().next().getName());
final EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNodes().values().iterator().next();
for (final ObjectTypeNode otn : epn.getObjectTypeNodes().values()) {
final ObjectSink[] sinks = otn.getObjectSinkPropagator().getSinks();
if (sinks.length > 0) {
fail(otn + " has sinks " + Arrays.toString(sinks));
}
}
}
Aggregations