use of org.drools.core.reteoo.NotNode in project drools by kiegroup.
the class LinkingTest method testNotNodeUnlinksWithNoConstriants.
@Test
public void testNotNodeUnlinksWithNoConstriants() {
String str = "";
str += "package org.kie \n";
str += "import " + A.class.getCanonicalName() + "\n";
str += "import " + B.class.getCanonicalName() + "\n";
str += "import " + C.class.getCanonicalName() + "\n";
str += "import " + D.class.getCanonicalName() + "\n";
str += "import " + E.class.getCanonicalName() + "\n";
str += "import " + F.class.getCanonicalName() + "\n";
str += "import " + G.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
str += "rule rule1 when \n";
str += " $a : A() \n";
str += " not B() \n";
str += " $c : C() \n";
str += "then \n";
str += " list.add( 'x' ); \n";
str += "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
ObjectTypeNode aotn = getObjectTypeNode(kbase, A.class);
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
List list = new ArrayList();
wm.setGlobal("list", list);
LeftInputAdapterNode aNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
NotNode bNode = (NotNode) aNode.getSinkPropagator().getSinks()[0];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
SegmentUtilities.createSegmentMemory(cNode, wm);
LiaNodeMemory amem = (LiaNodeMemory) wm.getNodeMemory(aNode);
// Only NotNode is linked in
assertEquals(2, amem.getSegmentMemory().getLinkedNodeMask());
FactHandle fha = wm.insert(new A());
FactHandle fhb = wm.insert(new B());
FactHandle fhc = wm.insert(new C());
wm.fireAllRules();
assertEquals(0, list.size());
// NotNode unlinks, which is allowed because it has no variable constraints
assertEquals(5, amem.getSegmentMemory().getLinkedNodeMask());
// NotNode links back in again, which is allowed because it has no variable constraints
wm.retract(fhb);
wm.flushPropagations();
assertEquals(7, amem.getSegmentMemory().getLinkedNodeMask());
wm.fireAllRules();
assertEquals(1, list.size());
// Now try with lots of facthandles on NotNode
list.clear();
List<FactHandle> handles = new ArrayList<FactHandle>();
for (int i = 0; i < 5; i++) {
handles.add(wm.insert(new B()));
}
wm.fireAllRules();
assertEquals(0, list.size());
assertEquals(5, amem.getSegmentMemory().getLinkedNodeMask());
for (FactHandle fh : handles) {
wm.retract(fh);
}
wm.flushPropagations();
assertEquals(7, amem.getSegmentMemory().getLinkedNodeMask());
wm.fireAllRules();
assertEquals(1, list.size());
}
use of org.drools.core.reteoo.NotNode 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.NotNode in project drools by kiegroup.
the class SegmentCreationTest method tesShareInSubnetwork.
@Test
public void tesShareInSubnetwork() throws Exception {
KieBase kbase = buildKnowledgeBase(" A() \n", " A() B() C() \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];
RuleTerminalNode rtn2 = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
RightInputAdapterNode riaNode = (RightInputAdapterNode) cNode.getSinkPropagator().getSinks()[1];
NotNode notNode = (NotNode) liaNode.getSinkPropagator().getSinks()[2];
RuleTerminalNode rtn3 = (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(cNode, bSmem.getTipNode());
// segment is not initialized yet
assertNull(bSmem.getFirst());
wm.fireAllRules();
SegmentMemory rtn2Smem = bSmem.getFirst();
assertEquals(rtn2, rtn2Smem.getRootNode());
assertEquals(rtn2, rtn2Smem.getTipNode());
SegmentMemory riaSmem = rtn2Smem.getNext();
assertEquals(riaNode, riaSmem.getRootNode());
assertEquals(riaNode, riaSmem.getTipNode());
SegmentMemory notSmem = bSmem.getNext();
assertEquals(notNode, notSmem.getRootNode());
assertEquals(rtn3, notSmem.getTipNode());
}
use of org.drools.core.reteoo.NotNode in project drools by kiegroup.
the class LeftTupleIterator method getFirstLeftTuple.
public LeftTuple getFirstLeftTuple(LeftTupleSource source, LeftTupleSink sink, InternalWorkingMemory wm) {
if (source instanceof AccumulateNode) {
AccumulateMemory accmem = (AccumulateMemory) wm.getNodeMemory((MemoryFactory) source);
BetaMemory memory = accmem.getBetaMemory();
FastIterator localIt = memory.getLeftTupleMemory().fullFastIterator();
Tuple leftTuple = BetaNode.getFirstTuple(memory.getLeftTupleMemory(), localIt);
if (leftTuple != null) {
AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
return accctx.getResultLeftTuple();
}
return null;
}
if (source instanceof JoinNode || source instanceof NotNode || source instanceof FromNode || source instanceof AccumulateNode) {
BetaMemory memory;
FastIterator localIt;
if (source instanceof FromNode) {
memory = ((FromMemory) wm.getNodeMemory((MemoryFactory) source)).getBetaMemory();
} else if (source instanceof AccumulateNode) {
memory = ((AccumulateMemory) wm.getNodeMemory((MemoryFactory) source)).getBetaMemory();
} else {
memory = (BetaMemory) wm.getNodeMemory((MemoryFactory) source);
}
localIt = memory.getLeftTupleMemory().fullFastIterator();
Tuple leftTuple = BetaNode.getFirstTuple(memory.getLeftTupleMemory(), localIt);
while (leftTuple != null) {
for (LeftTuple childleftTuple = leftTuple.getFirstChild(); childleftTuple != null; childleftTuple = childleftTuple.getHandleNext()) {
if (childleftTuple.getTupleSink() == sink) {
return childleftTuple;
}
}
leftTuple = (LeftTuple) localIt.next(leftTuple);
}
}
if (source instanceof ExistsNode) {
BetaMemory memory = (BetaMemory) wm.getNodeMemory((MemoryFactory) source);
FastIterator localIt = memory.getRightTupleMemory().fullFastIterator();
RightTuple rightTuple = (RightTuple) BetaNode.getFirstTuple(memory.getRightTupleMemory(), localIt);
while (rightTuple != null) {
if (rightTuple.getBlocked() != null) {
for (LeftTuple leftTuple = rightTuple.getBlocked(); leftTuple != null; leftTuple = leftTuple.getBlockedNext()) {
for (LeftTuple childleftTuple = leftTuple.getFirstChild(); childleftTuple != null; childleftTuple = childleftTuple.getHandleNext()) {
if (childleftTuple.getTupleSink() == sink) {
return childleftTuple;
}
}
}
}
rightTuple = (RightTuple) localIt.next(rightTuple);
}
} else if (source instanceof LeftInputAdapterNode) {
ObjectSource os = ((LeftInputAdapterNode) source).getParentObjectSource();
while (!(os instanceof ObjectTypeNode)) {
os = os.getParentObjectSource();
}
ObjectTypeNode otn = (ObjectTypeNode) os;
otnIterator = wm.getNodeMemory(otn).iterator();
while (otnIterator.hasNext()) {
InternalFactHandle handle = otnIterator.next();
LeftTuple leftTuple = handle.findFirstLeftTuple(lt -> lt.getTupleSink() == sink);
if (leftTuple != null) {
return leftTuple;
}
}
} else if (source instanceof EvalConditionNode || source instanceof QueryElementNode) {
LeftTuple parentLeftTuple = getFirstLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, wm);
while (parentLeftTuple != null) {
for (LeftTuple leftTuple = parentLeftTuple.getFirstChild(); leftTuple != null; leftTuple = leftTuple.getHandleNext()) {
if (leftTuple.getTupleSink() == sink) {
return leftTuple;
}
}
parentLeftTuple = getNextLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, parentLeftTuple, wm);
}
}
return null;
}
Aggregations