use of org.drools.core.reteoo.TupleMemory in project drools by kiegroup.
the class PhreakJoinNode method doRightDeletes.
public void doRightDeletes(BetaMemory bm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
TupleMemory rtm = bm.getRightTupleMemory();
for (RightTuple rightTuple = srcRightTuples.getDeleteFirst(); rightTuple != null; ) {
RightTuple next = rightTuple.getStagedNext();
if (rightTuple.getMemory() != null) {
// it may have been staged and never actually added
rtm.remove(rightTuple);
}
if (rightTuple.getFirstChild() != null) {
LeftTuple childLeftTuple = rightTuple.getFirstChild();
childLeftTuple.setPropagationContext(rightTuple.getPropagationContext());
while (childLeftTuple != null) {
LeftTuple nextChild = childLeftTuple.getRightParentNext();
RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
childLeftTuple = nextChild;
}
}
rightTuple.clearStaged();
rightTuple = next;
}
}
use of org.drools.core.reteoo.TupleMemory in project drools by kiegroup.
the class PhreakJoinNode method doLeftInserts.
public void doLeftInserts(JoinNode joinNode, LeftTupleSink sink, BetaMemory bm, ReteEvaluator reteEvaluator, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
TupleMemory ltm = bm.getLeftTupleMemory();
TupleMemory rtm = bm.getRightTupleMemory();
ContextEntry[] contextEntry = bm.getContext();
BetaConstraints constraints = joinNode.getRawConstraints();
for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
boolean useLeftMemory = RuleNetworkEvaluator.useLeftMemory(joinNode, leftTuple);
if (useLeftMemory) {
ltm.add(leftTuple);
}
FastIterator it = joinNode.getRightIterator(rtm);
constraints.updateFromTuple(contextEntry, reteEvaluator, leftTuple);
for (RightTuple rightTuple = joinNode.getFirstRightTuple(leftTuple, rtm, it); rightTuple != null; rightTuple = (RightTuple) it.next(rightTuple)) {
if (constraints.isAllowedCachedLeft(contextEntry, rightTuple.getFactHandle())) {
insertChildLeftTuple(trgLeftTuples, leftTuple, rightTuple, null, null, sink, useLeftMemory);
}
}
leftTuple.clearStaged();
leftTuple = next;
}
constraints.resetTuple(contextEntry);
}
use of org.drools.core.reteoo.TupleMemory in project drools by kiegroup.
the class PhreakSubnetworkNotExistsNode method doSubNetworkNode.
public static void doSubNetworkNode(BetaNode node, LeftTupleSink sink, BetaMemory bm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
TupleSets<RightTuple> srcRightTuples = bm.getStagedRightTuples().takeAll();
TupleMemory ltm = bm.getLeftTupleMemory();
boolean tupleMemoryEnabled = node.isLeftTupleMemoryEnabled();
deleteLeft(srcLeftTuples, trgLeftTuples, stagedLeftTuples, ltm);
insertRight(node, sink, trgLeftTuples, stagedLeftTuples, srcRightTuples, tupleMemoryEnabled);
insertLeft(node, sink, srcLeftTuples, trgLeftTuples, ltm, tupleMemoryEnabled);
updateRight(srcRightTuples);
deleteRight(node, sink, trgLeftTuples, stagedLeftTuples, srcRightTuples);
updateLeft(srcLeftTuples, trgLeftTuples, stagedLeftTuples, ltm);
srcRightTuples.resetAll();
srcLeftTuples.resetAll();
}
use of org.drools.core.reteoo.TupleMemory in project drools by kiegroup.
the class PhreakActivationIterator method processLeftTuples.
public static void processLeftTuples(LeftTupleSource node, List<AgendaItem> agendaItems, Set<RuleTerminalNode> nodeSet, ReteEvaluator reteEvaluator) {
LeftTupleSource node1 = node;
while (NodeTypeEnums.LeftInputAdapterNode != node1.getType()) {
node1 = node1.getLeftTupleSource();
}
int maxShareCount = node1.getAssociationsSize();
while (NodeTypeEnums.LeftInputAdapterNode != node.getType()) {
Memory memory = reteEvaluator.getNodeMemory((MemoryFactory) node);
if (memory.getSegmentMemory() == null) {
// segment has never been initialized, which means the rule has never been linked.
return;
}
if (node.getAssociationsSize() == maxShareCount) {
// the recurse must start from the first split node, otherwise we get partial overlaps in propagations
if (NodeTypeEnums.isBetaNode(node)) {
BetaMemory bm;
if (NodeTypeEnums.AccumulateNode == node.getType()) {
AccumulateMemory am = (AccumulateMemory) memory;
bm = am.getBetaMemory();
FastIterator it = bm.getLeftTupleMemory().fullFastIterator();
Tuple lt = BetaNode.getFirstTuple(bm.getLeftTupleMemory(), it);
for (; lt != null; lt = (LeftTuple) it.next(lt)) {
AccumulateContext accctx = (AccumulateContext) lt.getContextObject();
collectFromPeers(accctx.getResultLeftTuple(), agendaItems, nodeSet, reteEvaluator);
}
} else if (NodeTypeEnums.ExistsNode == node.getType()) {
bm = (BetaMemory) reteEvaluator.getNodeMemory((MemoryFactory) node);
// done off the RightTupleMemory, as exists only have unblocked tuples on the left side
FastIterator it = bm.getRightTupleMemory().fullFastIterator();
RightTuple rt = (RightTuple) BetaNode.getFirstTuple(bm.getRightTupleMemory(), it);
for (; rt != null; rt = (RightTuple) it.next(rt)) {
for (LeftTuple lt = rt.getBlocked(); lt != null; lt = lt.getBlockedNext()) {
if (lt.getFirstChild() != null) {
collectFromPeers(lt.getFirstChild(), agendaItems, nodeSet, reteEvaluator);
}
}
}
} else {
bm = (BetaMemory) reteEvaluator.getNodeMemory((MemoryFactory) node);
FastIterator it = bm.getLeftTupleMemory().fullFastIterator();
Tuple lt = BetaNode.getFirstTuple(bm.getLeftTupleMemory(), it);
for (; lt != null; lt = (LeftTuple) it.next(lt)) {
if (lt.getFirstChild() != null) {
collectFromLeftInput(lt.getFirstChild(), agendaItems, nodeSet, reteEvaluator);
}
}
}
return;
} else if (NodeTypeEnums.FromNode == node.getType()) {
FromMemory fm = (FromMemory) reteEvaluator.getNodeMemory((MemoryFactory) node);
TupleMemory ltm = fm.getBetaMemory().getLeftTupleMemory();
FastIterator it = ltm.fullFastIterator();
for (LeftTuple lt = (LeftTuple) ltm.getFirst(null); lt != null; lt = (LeftTuple) it.next(lt)) {
if (lt.getFirstChild() != null) {
collectFromLeftInput(lt.getFirstChild(), agendaItems, nodeSet, reteEvaluator);
}
}
return;
}
}
node = node.getLeftTupleSource();
}
// No beta or from nodes, so must retrieve LeftTuples from the LiaNode.
// This is done by scanning all the LeftTuples referenced from the FactHandles in the ObjectTypeNode
LeftInputAdapterNode lian = (LeftInputAdapterNode) node;
if (!lian.isTerminal()) {
Memory memory = reteEvaluator.getNodeMemory((MemoryFactory) node);
if (memory.getSegmentMemory() == null) {
// segment has never been initialized, which means the rule has never been linked.
return;
}
}
ObjectSource os = lian.getObjectSource();
while (os.getType() != NodeTypeEnums.ObjectTypeNode) {
os = os.getParentObjectSource();
}
ObjectTypeNode otn = (ObjectTypeNode) os;
final ObjectTypeNodeMemory omem = reteEvaluator.getNodeMemory(otn);
LeftTupleSink firstLiaSink = lian.getSinkPropagator().getFirstLeftTupleSink();
java.util.Iterator<InternalFactHandle> it = omem.iterator();
while (it.hasNext()) {
InternalFactHandle fh = it.next();
fh.forEachLeftTuple(lt -> {
if (lt.getTupleSink() == firstLiaSink) {
collectFromLeftInput(lt, agendaItems, nodeSet, reteEvaluator);
}
});
}
}
use of org.drools.core.reteoo.TupleMemory in project drools by kiegroup.
the class OOPathReactiveTest method testReactiveDeleteOnLia.
@Test
public void testReactiveDeleteOnLia() {
final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $toy: /wife/children[age > 10]/toys )\n" + "then\n" + " list.add( $toy.getName() );\n" + "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
KieSession ksession = kbase.newKieSession();
final EntryPointNode epn = ((InternalKnowledgeBase) ksession.getKieBase()).getRete().getEntryPointNodes().values().iterator().next();
final ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(Man.class));
final LeftInputAdapterNode lian = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
final ReactiveFromNode from1 = (ReactiveFromNode) lian.getSinkPropagator().getSinks()[0];
final ReactiveFromNode from2 = (ReactiveFromNode) from1.getSinkPropagator().getSinks()[0];
final ReactiveFromNode from3 = (ReactiveFromNode) from2.getSinkPropagator().getSinks()[0];
final BetaMemory betaMemory = ((InternalWorkingMemory) ksession).getNodeMemory(from3).getBetaMemory();
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Woman alice = new Woman("Alice", 38);
final Man bob = new Man("Bob", 40);
bob.setWife(alice);
final Child charlie = new Child("Charles", 12);
final Child debbie = new Child("Debbie", 11);
alice.addChild(charlie);
alice.addChild(debbie);
charlie.addToy(new Toy("car"));
charlie.addToy(new Toy("ball"));
debbie.addToy(new Toy("doll"));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("car", "ball", "doll");
final TupleMemory tupleMemory = betaMemory.getLeftTupleMemory();
Assertions.assertThat(betaMemory.getLeftTupleMemory().size()).isEqualTo(2);
Iterator<LeftTuple> it = tupleMemory.iterator();
for (LeftTuple next = it.next(); next != null; next = it.next()) {
final Object obj = next.getFactHandle().getObject();
Assertions.assertThat(obj == charlie || obj == debbie).isTrue();
}
list.clear();
debbie.setAge(10);
ksession.fireAllRules();
Assertions.assertThat(list).hasSize(0);
;
Assertions.assertThat(betaMemory.getLeftTupleMemory().size()).isEqualTo(1);
it = tupleMemory.iterator();
for (LeftTuple next = it.next(); next != null; next = it.next()) {
final Object obj = next.getFactHandle().getObject();
Assertions.assertThat(obj == charlie).isTrue();
}
}
Aggregations