use of org.drools.core.reteoo.LeftTuple in project drools by kiegroup.
the class BaseLeftTuplesBuilder method delete.
public T delete(Object... objects) {
this.testStagedDelete = true;
if (objects == null) {
objects = new Object[0];
}
for (int i = 0; i < objects.length; i++) {
if (!(objects[i] instanceof Pair)) {
Object o1 = objects[i];
InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(o1);
LeftTuple leftTuple = new LeftTupleImpl(fh1, sink, true);
leftTuples.addDelete(leftTuple);
} else {
Pair p = (Pair) objects[i];
InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(p.getO1());
LeftTuple leftTuple1 = new LeftTupleImpl(fh1, sink, true);
InternalFactHandle fh2 = (InternalFactHandle) wm.getFactHandle(p.getO2());
LeftTuple leftTuple2 = sink.createLeftTuple(leftTuple1, new RightTupleImpl(fh2), sink);
leftTuples.addDelete(leftTuple2);
}
}
return (T) this;
}
use of org.drools.core.reteoo.LeftTuple in project drools by kiegroup.
the class Scenario method run.
public Scenario run() {
previousResultTuples = bm.getSegmentMemory().getFirst().getStagedLeftTuples();
actualResultLeftTuples = new TupleSetsImpl<LeftTuple>();
if (phreakNode == PhreakJoinNode.class) {
new PhreakJoinNode().doNode((JoinNode) betaNode, sinkNode, bm, wm, leftTuples, actualResultLeftTuples, previousResultTuples);
} else if (phreakNode == PhreakNotNode.class) {
new PhreakNotNode().doNode((NotNode) betaNode, sinkNode, bm, wm, leftTuples, actualResultLeftTuples, previousResultTuples);
} else if (phreakNode == PhreakExistsNode.class) {
new PhreakExistsNode().doNode((ExistsNode) betaNode, sinkNode, bm, wm, leftTuples, actualResultLeftTuples, previousResultTuples);
}
if (expectedResultBuilder != null) {
assertEquals(expectedResultBuilder.get(), actualResultLeftTuples, expectedResultBuilder.isTestStagedInsert(), expectedResultBuilder.isTestStagedDelete(), expectedResultBuilder.isTestStagedUpdate());
}
if (!preStagedBuilders.isEmpty()) {
for (StagedBuilder stagedBuilder : preStagedBuilders) {
TupleSets<LeftTuple> expected = stagedBuilder.get();
TupleSets<LeftTuple> actual = stagedBuilder.getSegmentMemory().getStagedLeftTuples();
assertEquals(expected, actual, stagedBuilder.isTestStagedInsert(), stagedBuilder.isTestStagedDelete(), stagedBuilder.isTestStagedUpdate());
}
}
SegmentMemory smem = bm.getSegmentMemory();
SegmentPropagator.propagate(smem, actualResultLeftTuples, wm);
if (testLeftMemory) {
equalsLeftMemory(leftMemory);
}
if (testRightMemory) {
equalsRightMemory(rightMemory);
}
if (!postStagedBuilders.isEmpty()) {
for (StagedBuilder stagedBuilder : postStagedBuilders) {
TupleSets<LeftTuple> expected = stagedBuilder.get();
TupleSets<LeftTuple> actual = stagedBuilder.getSegmentMemory().getStagedLeftTuples();
assertEquals(expected, actual, stagedBuilder.isTestStagedInsert(), stagedBuilder.isTestStagedDelete(), stagedBuilder.isTestStagedUpdate());
}
}
return this;
}
use of org.drools.core.reteoo.LeftTuple in project drools by kiegroup.
the class Scenario method equalsLeftMemory.
public void equalsLeftMemory(List<LeftTuple> leftTuples) {
TupleMemory ltm = bm.getLeftTupleMemory();
int length = 0;
for (LeftTuple expectedLeftTuple : leftTuples) {
FastIterator it = betaNode.getLeftIterator(ltm);
Tuple actualLeftTuple = null;
for (actualLeftTuple = BetaNode.getFirstTuple(ltm, it); actualLeftTuple != null; actualLeftTuple = (LeftTuple) it.next(actualLeftTuple)) {
if (expectedLeftTuple.equals(actualLeftTuple)) {
length++;
break;
}
}
if (actualLeftTuple == null) {
fail("Could not find LeftTuple: " + expectedLeftTuple);
}
}
if (leftTuples.size() != ltm.size()) {
fail("LeftTuple memory size did not match: " + length);
}
}
use of org.drools.core.reteoo.LeftTuple 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.LeftTuple in project drools by kiegroup.
the class Misc2Test method testDynamicAddRule.
@Test
public void testDynamicAddRule() {
// DROOLS-17
String str = "import " + Misc2Test.A.class.getCanonicalName() + "\n" + "rule r1 when\n" + " $a : A( f1 == 1 )\n" + "then\n" + "end\n" + "\n" + "rule r2 when\n" + " $a : A( f2 == 1 )\n" + "then\n" + "end\n" + "\n" + "rule r3 when\n" + " $a : A( f3 == 1 )" + "then\n" + "end";
String str2 = "import " + Misc2Test.A.class.getCanonicalName() + "\n" + "rule r4 when\n" + " $a : A( f2 == 1, f4 == 1 )" + "then\n" + "end";
InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBaseFromString(str);
KieSession ksession = kbase.newKieSession();
FactHandle fh = ksession.insert(new A(1, 1, 1, 1));
ksession.fireAllRules();
kbase.addPackages(loadKnowledgePackagesFromString(str2));
ksession.fireAllRules();
// this second insert forces the regeneration of the otnIds
ksession.insert(new A(2, 2, 2, 2));
LeftTuple leftTuple = ((DefaultFactHandle) fh).getFirstLeftTuple();
ObjectTypeNode.Id letTupleOtnId = leftTuple.getInputOtnId();
leftTuple = leftTuple.getHandleNext();
while (leftTuple != null) {
assertTrue(letTupleOtnId.before(leftTuple.getInputOtnId()));
letTupleOtnId = leftTuple.getInputOtnId();
leftTuple = leftTuple.getHandleNext();
}
}
Aggregations