use of org.drools.core.reteoo.TupleMemory in project drools by kiegroup.
the class OOPathReactiveTests method testReactiveDeleteOnLia.
@Test
public void testReactiveDeleteOnLia() {
final String drl = "import org.drools.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";
final KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
final KieSession ksession = kbase.newKieSession();
final EntryPointNode epn = ((InternalKnowledgeBase) ksession.getKieBase()).getRete().getEntryPointNodes().values().iterator().next();
final ObjectTypeNode otn = epn.getObjectTypeNodes().values().iterator().next();
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();
}
}
use of org.drools.core.reteoo.TupleMemory in project drools by kiegroup.
the class RightMemory method getRightTuples.
public List<RightTuple> getRightTuples(Object... objects) {
BetaNode node = scenario.getBetaNode();
BetaMemory bm = scenario.getBm();
TupleMemory rtm = bm.getRightTupleMemory();
InternalWorkingMemory wm = scenario.getWorkingMemory();
if (objects == null) {
objects = new Object[0];
}
List<RightTuple> rightTuples = new ArrayList<RightTuple>();
for (Object object : objects) {
InternalFactHandle fh = (InternalFactHandle) wm.insert(object);
// node.createLeftTuple( fh, node, true );
RightTuple expectedRightTuple = new RightTupleImpl(fh, node);
expectedRightTuple.setPropagationContext(new PhreakPropagationContext());
rightTuples.add(expectedRightTuple);
}
scenario.setTestRightMemory(true);
return rightTuples;
}
use of org.drools.core.reteoo.TupleMemory in project drools by kiegroup.
the class PhreakExistsNode method doLeftDeletes.
public void doLeftDeletes(BetaMemory bm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
TupleMemory ltm = bm.getLeftTupleMemory();
for (LeftTuple leftTuple = srcLeftTuples.getDeleteFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
RightTuple blocker = leftTuple.getBlocker();
if (blocker == null) {
if (leftTuple.getMemory() != null) {
// it may have been staged and never actually added
ltm.remove(leftTuple);
}
} else {
if (leftTuple.getFirstChild() != null) {
// no need to update pctx, as no right available, and pctx will exist on a parent LeftTuple anyway
RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(leftTuple.getFirstChild(), trgLeftTuples, stagedLeftTuples);
}
blocker.removeBlocked(leftTuple);
}
leftTuple.clearStaged();
leftTuple = next;
}
}
use of org.drools.core.reteoo.TupleMemory in project drools by kiegroup.
the class RuleNetworkEvaluator method doExistentialUpdatesReorderChildLeftTuple.
public static void doExistentialUpdatesReorderChildLeftTuple(ReteEvaluator reteEvaluator, NotNode notNode, RightTuple rightTuple) {
BetaMemory bm = getBetaMemory(notNode, reteEvaluator);
TupleMemory rtm = bm.getRightTupleMemory();
boolean resumeFromCurrent = !(notNode.isIndexedUnificationJoin() || rtm.getIndexType().isComparison());
doRemoveExistentialRightMemoryForReorder(rtm, resumeFromCurrent, rightTuple);
doAddExistentialRightMemoryForReorder(rtm, resumeFromCurrent, rightTuple);
updateBlockersAndPropagate(notNode, rightTuple, reteEvaluator, rtm, bm.getContext(), notNode.getRawConstraints(), !resumeFromCurrent, null, null, null);
}
use of org.drools.core.reteoo.TupleMemory in project drools by kiegroup.
the class RuleNetworkEvaluator method doUpdatesReorderLeftMemory.
public static void doUpdatesReorderLeftMemory(BetaMemory bm, TupleSets<LeftTuple> srcLeftTuples) {
TupleMemory ltm = bm.getLeftTupleMemory();
// sides must first be re-ordered, to ensure iteration integrity
for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; leftTuple = leftTuple.getStagedNext()) {
if (leftTuple.getMemory() != null) {
ltm.remove(leftTuple);
}
}
for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; leftTuple = leftTuple.getStagedNext()) {
ltm.add(leftTuple);
for (LeftTuple childLeftTuple = leftTuple.getFirstChild(); childLeftTuple != null; ) {
LeftTuple childNext = childLeftTuple.getHandleNext();
childLeftTuple.reAddRight();
childLeftTuple = childNext;
}
}
}
Aggregations