Search in sources :

Example 61 with LeftTuple

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;
}
Also used : LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple)

Example 62 with LeftTuple

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;
}
Also used : PhreakNotNode(org.drools.core.phreak.PhreakNotNode) NotNode(org.drools.core.reteoo.NotNode) PhreakNotNode(org.drools.core.phreak.PhreakNotNode) SegmentMemory(org.drools.core.reteoo.SegmentMemory) PhreakExistsNode(org.drools.core.phreak.PhreakExistsNode) PhreakJoinNode(org.drools.core.phreak.PhreakJoinNode) LeftTuple(org.drools.core.reteoo.LeftTuple)

Example 63 with LeftTuple

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);
    }
}
Also used : FastIterator(org.drools.core.util.FastIterator) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple)

Example 64 with LeftTuple

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();
    }
}
Also used : ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) ReactiveFromNode(org.drools.core.reteoo.ReactiveFromNode) BetaMemory(org.drools.core.reteoo.BetaMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) EntryPointNode(org.drools.core.reteoo.EntryPointNode) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.compiler.oopath.model.Man) Woman(org.drools.compiler.oopath.model.Woman) Child(org.drools.compiler.oopath.model.Child) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Toy(org.drools.compiler.oopath.model.Toy) Test(org.junit.Test)

Example 65 with LeftTuple

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();
    }
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) LeftTuple(org.drools.core.reteoo.LeftTuple) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

LeftTuple (org.drools.core.reteoo.LeftTuple)125 RightTuple (org.drools.core.reteoo.RightTuple)41 TupleMemory (org.drools.core.reteoo.TupleMemory)37 InternalFactHandle (org.drools.core.common.InternalFactHandle)34 FastIterator (org.drools.core.util.FastIterator)22 BetaConstraints (org.drools.core.common.BetaConstraints)21 BetaMemory (org.drools.core.reteoo.BetaMemory)21 ContextEntry (org.drools.core.rule.ContextEntry)20 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)18 PhreakJoinNode.updateChildLeftTuple (org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple)16 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)13 SegmentMemory (org.drools.core.reteoo.SegmentMemory)13 Test (org.junit.Test)13 KieSession (org.kie.api.runtime.KieSession)13 Tuple (org.drools.core.spi.Tuple)12 WorkingMemory (org.drools.core.WorkingMemory)11 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)11 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)10 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)9 ArrayList (java.util.ArrayList)8