Search in sources :

Example 21 with ObjectTypeNode

use of org.drools.core.reteoo.ObjectTypeNode 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 22 with ObjectTypeNode

use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.

the class Misc2Test method testFromEPDontRequireLeftInput.

@Test
public void testFromEPDontRequireLeftInput() {
    // DROOLS-1014
    String drl = "rule R when\n" + "    $s1 : String() from entry-point \"xxx\"\n" + "then\n" + "end\n";
    KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
    Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
    LeftInputAdapterNode liaNode = null;
    for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
        Class<?> otnType = ((ClassObjectType) otn.getObjectType()).getClassType();
        if (String.class == otnType) {
            assertEquals(1, otn.getObjectSinkPropagator().size());
        } else if (InitialFact.class.isAssignableFrom(otnType)) {
            assertEquals(0, otn.getObjectSinkPropagator().size());
        } else {
            fail("There shouldn't be other OTNs");
        }
    }
}
Also used : Rete(org.drools.core.reteoo.Rete) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) InitialFact(org.drools.core.InitialFact) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieHelper(org.kie.internal.utils.KieHelper) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 23 with ObjectTypeNode

use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.

the class NodePositionInPathTest method test.

@Test
public void test() {
    String drl = "rule R1 when\n" + "    Integer()\n" + "    exists( Integer() and Integer() )\n" + "    String()\n" + "then\n" + "end\n" + "rule R2 when\n" + "    Integer()\n" + "    exists( Integer() and String() )\n" + "then\n" + "end\n";
    KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
    ReteDumper.dumpRete(((InternalKnowledgeBase) kbase));
    Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
    LeftInputAdapterNode liaNode = null;
    for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
        Class<?> otnType = ((ClassObjectType) otn.getObjectType()).getClassType();
        if (Integer.class == otnType) {
            liaNode = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
        }
    }
    assertEquals(0, liaNode.getPositionInPath());
    LeftTupleSink[] liaSinks = liaNode.getSinkPropagator().getSinks();
    BetaNode join1 = (BetaNode) liaSinks[0];
    assertEquals(1, join1.getPositionInPath());
    ExistsNode ex1 = (ExistsNode) liaSinks[1];
    assertEquals(1, ex1.getPositionInPath());
    BetaNode join2 = (BetaNode) ex1.getSinkPropagator().getSinks()[0];
    assertEquals(2, join2.getPositionInPath());
    RuleTerminalNode rtn1 = (RuleTerminalNode) join2.getSinkPropagator().getSinks()[0];
    assertEquals(3, rtn1.getPositionInPath());
    ExistsNode ex2 = (ExistsNode) liaSinks[2];
    assertEquals(1, ex2.getPositionInPath());
    RuleTerminalNode rtn2 = (RuleTerminalNode) ex2.getSinkPropagator().getSinks()[0];
    assertEquals(2, rtn2.getPositionInPath());
    BetaNode join3 = (BetaNode) join1.getSinkPropagator().getSinks()[0];
    assertEquals(2, join3.getPositionInPath());
    RightInputAdapterNode ria1 = (RightInputAdapterNode) join3.getSinkPropagator().getSinks()[0];
    assertEquals(3, ria1.getPositionInPath());
    BetaNode join4 = (BetaNode) join1.getSinkPropagator().getSinks()[1];
    assertEquals(2, join4.getPositionInPath());
    RightInputAdapterNode ria2 = (RightInputAdapterNode) join4.getSinkPropagator().getSinks()[0];
    assertEquals(3, ria2.getPositionInPath());
    LeftTupleNode[] rtn1PathNodes = rtn1.getPathNodes();
    assertEquals(4, rtn1PathNodes.length);
    checkNodePosition(rtn1PathNodes, liaNode);
    checkNodePosition(rtn1PathNodes, ex1);
    checkNodePosition(rtn1PathNodes, join2);
    checkNodePosition(rtn1PathNodes, rtn1);
    LeftTupleNode[] rtn2PathNodes = rtn2.getPathNodes();
    assertEquals(3, rtn2PathNodes.length);
    checkNodePosition(rtn2PathNodes, liaNode);
    checkNodePosition(rtn2PathNodes, ex2);
    checkNodePosition(rtn2PathNodes, rtn2);
    LeftTupleNode[] ria1PathNodes = ria1.getPathNodes();
    assertEquals(4, ria1PathNodes.length);
    checkNodePosition(ria1PathNodes, liaNode);
    checkNodePosition(ria1PathNodes, join1);
    checkNodePosition(ria1PathNodes, join3);
    checkNodePosition(ria1PathNodes, ria1);
    LeftTupleNode[] ria2PathNodes = ria2.getPathNodes();
    assertEquals(4, ria2PathNodes.length);
    checkNodePosition(ria2PathNodes, liaNode);
    checkNodePosition(ria2PathNodes, join1);
    checkNodePosition(ria2PathNodes, join4);
    checkNodePosition(ria2PathNodes, ria2);
}
Also used : Rete(org.drools.core.reteoo.Rete) BetaNode(org.drools.core.reteoo.BetaNode) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) KieHelper(org.kie.internal.utils.KieHelper) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) ExistsNode(org.drools.core.reteoo.ExistsNode) KieBase(org.kie.api.KieBase) LeftTupleNode(org.drools.core.reteoo.LeftTupleNode) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 24 with ObjectTypeNode

use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.

the class NodesPartitioningTest method checkNode.

private void checkNode(NetworkNode node) {
    if (node instanceof EntryPointNode) {
        assertSame(RuleBasePartitionId.MAIN_PARTITION, node.getPartitionId());
    } else if (node instanceof ObjectTypeNode) {
        assertSame(RuleBasePartitionId.MAIN_PARTITION, node.getPartitionId());
        checkPartitionedSinks((ObjectTypeNode) node);
    } else if (node instanceof ObjectSource) {
        ObjectSource source = ((ObjectSource) node).getParentObjectSource();
        if (!(source instanceof ObjectTypeNode)) {
            assertSame(source.getPartitionId(), node.getPartitionId());
        }
    } else if (node instanceof BetaNode) {
        ObjectSource rightInput = ((BetaNode) node).getRightInput();
        if (!(rightInput instanceof ObjectTypeNode)) {
            assertSame(rightInput.getPartitionId(), node.getPartitionId());
        }
        LeftTupleSource leftInput = ((BetaNode) node).getLeftTupleSource();
        assertSame(leftInput.getPartitionId(), node.getPartitionId());
    } else if (node instanceof TerminalNode) {
        LeftTupleSource leftInput = ((TerminalNode) node).getLeftTupleSource();
        assertSame(leftInput.getPartitionId(), node.getPartitionId());
    }
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) BetaNode(org.drools.core.reteoo.BetaNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ObjectSource(org.drools.core.reteoo.ObjectSource) TerminalNode(org.drools.core.reteoo.TerminalNode)

Example 25 with ObjectTypeNode

use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.

the class PropertySpecificTest method testRTNodeEmptyLHS.

@Test
public void testRTNodeEmptyLHS() {
    String rule = "package org.drools.compiler.integrationtests\n" + "rule r1\n" + "when\n" + "then\n" + "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(rule);
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode otn = getObjectTypeNode(kbase, "InitialFactImpl");
    assertNotNull(otn);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtNode = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
    assertEquals(AllSetBitMask.get(), rtNode.getDeclaredMask());
    assertEquals(AllSetBitMask.get(), rtNode.getInferredMask());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Aggregations

ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)116 Test (org.junit.Test)100 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)69 KieBase (org.kie.api.KieBase)64 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)60 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)40 ClassObjectType (org.drools.core.base.ClassObjectType)34 JoinNode (org.drools.core.reteoo.JoinNode)31 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)30 AlphaNode (org.drools.core.reteoo.AlphaNode)29 ArrayList (java.util.ArrayList)28 BetaNode (org.drools.core.reteoo.BetaNode)27 BetaMemory (org.drools.core.reteoo.BetaMemory)24 List (java.util.List)23 SegmentMemory (org.drools.core.reteoo.SegmentMemory)23 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)23 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)22 KieSession (org.kie.api.runtime.KieSession)20 Person (org.drools.compiler.Person)17 EntryPointNode (org.drools.core.reteoo.EntryPointNode)16