Search in sources :

Example 6 with EntryPointNode

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

the class FromTest method testFromSharingWithPropertyReactive.

@Test
public void testFromSharingWithPropertyReactive() {
    // As above but with property reactive as default
    String drl = "import " + ListsContainer.class.getCanonicalName() + "\n" + "global java.util.List output1;\n" + "global java.util.List output2;\n" + "rule R1 when\n" + "    ListsContainer( $list : list1 )\n" + "    $s : String( length == 2 ) from $list\n" + "then\n" + "    output1.add($s);\n" + "end\n" + "rule R2 when\n" + "    ListsContainer( $list : list2 )\n" + "    $s : String( length == 2 ) from $list\n" + "then\n" + "    output2.add($s);\n" + "end\n" + "rule R3 when\n" + "    ListsContainer( $list : list2 )\n" + "    $s : String( length == 2 ) from $list\n" + "then\n" + "    output2.add($s);\n" + "end\n";
    // property reactive as default:
    KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();
    ReteDumper.dumpRete(kbase);
    List<String> output1 = new ArrayList<String>();
    ksession.setGlobal("output1", output1);
    List<String> output2 = new ArrayList<String>();
    ksession.setGlobal("output2", output2);
    FactHandle fh = ksession.insert(new ListsContainer());
    ksession.fireAllRules();
    assertEquals("bb", output1.get(0));
    assertEquals("22", output2.get(0));
    assertEquals("22", output2.get(1));
    EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNodes().values().iterator().next();
    ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(ListsContainer.class));
    // There are 2 LIAs, one for the list1 and the other for the list2
    assertEquals(2, otn.getObjectSinkPropagator().size());
    LeftInputAdapterNode lia0 = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
    // There are only 2 FromNodes since R2 and R3 are sharing the second From
    // The first FROM node has R1 has sink
    LeftTupleSink[] sinks0 = lia0.getSinkPropagator().getSinks();
    assertEquals(1, sinks0.length);
    assertEquals(1, sinks0[0].getSinkPropagator().size());
    // The second FROM node has both R2 and R3 as sinks
    LeftInputAdapterNode lia1 = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[1];
    LeftTupleSink[] sinks1 = lia1.getSinkPropagator().getSinks();
    assertEquals(1, sinks1.length);
    assertEquals(2, sinks1[0].getSinkPropagator().size());
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) KieHelper(org.kie.internal.utils.KieHelper) EntryPointNode(org.drools.core.reteoo.EntryPointNode) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 7 with EntryPointNode

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

the class FromTest method testFromSharing.

@Test
public void testFromSharing() {
    // Keeping original test as non-property reactive by default, just allowed.
    String drl = "import " + ListsContainer.class.getCanonicalName() + "\n" + "global java.util.List output1;\n" + "global java.util.List output2;\n" + "rule R1 when\n" + "    ListsContainer( $list : list1 )\n" + "    $s : String( length == 2 ) from $list\n" + "then\n" + "    output1.add($s);\n" + "end\n" + "rule R2 when\n" + "    ListsContainer( $list : list2 )\n" + "    $s : String( length == 2 ) from $list\n" + "then\n" + "    output2.add($s);\n" + "end\n" + "rule R3 when\n" + "    ListsContainer( $list : list2 )\n" + "    $s : String( length == 2 ) from $list\n" + "then\n" + "    output2.add($s);\n" + "end\n";
    KieBase kbase = new KieHelper(PropertySpecificOption.ALLOWED).addContent(drl, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();
    ReteDumper.dumpRete(kbase);
    List<String> output1 = new ArrayList<String>();
    ksession.setGlobal("output1", output1);
    List<String> output2 = new ArrayList<String>();
    ksession.setGlobal("output2", output2);
    FactHandle fh = ksession.insert(new ListsContainer());
    ksession.fireAllRules();
    assertEquals("bb", output1.get(0));
    assertEquals("22", output2.get(0));
    assertEquals("22", output2.get(1));
    EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNodes().values().iterator().next();
    ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(ListsContainer.class));
    // There is only 1 LIA
    assertEquals(1, otn.getObjectSinkPropagator().size());
    LeftInputAdapterNode lian = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
    // There are only 2 FromNodes since R2 and R3 are sharing the second From
    LeftTupleSink[] sinks = lian.getSinkPropagator().getSinks();
    assertEquals(2, sinks.length);
    // The first from has R1 has sink
    assertEquals(1, sinks[0].getSinkPropagator().size());
    // The second from has both R2 and R3 as sinks
    assertEquals(2, sinks[1].getSinkPropagator().size());
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) KieHelper(org.kie.internal.utils.KieHelper) EntryPointNode(org.drools.core.reteoo.EntryPointNode) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 8 with EntryPointNode

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

the class BetaNodeBuilder method build.

public BetaNode build() {
    NodeFactory nFactory = buildContext.getComponentFactory().getNodeFactoryService();
    EntryPointNode epn = buildContext.getKnowledgeBase().getRete().getEntryPointNodes().values().iterator().next();
    ObjectTypeNode otn = nFactory.buildObjectTypeNode(buildContext.getNextId(), epn, new ClassObjectType(leftType), buildContext);
    LeftInputAdapterNode leftInput = nFactory.buildLeftInputAdapterNode(buildContext.getNextId(), otn, buildContext);
    ObjectSource rightInput = nFactory.buildObjectTypeNode(buildContext.getNextId(), epn, new ClassObjectType(rightType), buildContext);
    ReteTesterHelper reteTesterHelper = new ReteTesterHelper();
    Pattern pattern = new Pattern(0, new ClassObjectType(leftType));
    // BetaNodeFieldConstraint betaConstraint = null;
    BetaConstraints betaConstraints = null;
    if (constraintFieldName != null) {
        ClassFieldAccessorStore store = (ClassFieldAccessorStore) reteTesterHelper.getStore();
        InternalReadAccessor extractor = store.getReader(leftType, leftFieldName);
        Declaration declr = new Declaration(leftVariableName, extractor, pattern);
        betaConstraints = new SingleBetaConstraints(reteTesterHelper.getBoundVariableConstraint(rightType, constraintFieldName, declr, constraintOperator), buildContext.getKnowledgeBase().getConfiguration());
    } else {
        betaConstraints = new EmptyBetaConstraints();
    }
    switch(nodeType) {
        case NodeTypeEnums.JoinNode:
            return new JoinNode(0, leftInput, rightInput, betaConstraints, buildContext);
        case NodeTypeEnums.NotNode:
            return new NotNode(0, leftInput, rightInput, betaConstraints, buildContext);
        case NodeTypeEnums.ExistsNode:
            return new ExistsNode(0, leftInput, rightInput, betaConstraints, buildContext);
    }
    throw new IllegalStateException("Unable to build Node");
}
Also used : Pattern(org.drools.core.rule.Pattern) SingleBetaConstraints(org.drools.core.common.SingleBetaConstraints) NotNode(org.drools.core.reteoo.NotNode) BetaConstraints(org.drools.core.common.BetaConstraints) EmptyBetaConstraints(org.drools.core.common.EmptyBetaConstraints) SingleBetaConstraints(org.drools.core.common.SingleBetaConstraints) ClassObjectType(org.drools.core.base.ClassObjectType) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ClassFieldAccessorStore(org.drools.core.base.ClassFieldAccessorStore) ExistsNode(org.drools.core.reteoo.ExistsNode) EntryPointNode(org.drools.core.reteoo.EntryPointNode) NodeFactory(org.drools.core.reteoo.builder.NodeFactory) EmptyBetaConstraints(org.drools.core.common.EmptyBetaConstraints) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) ObjectSource(org.drools.core.reteoo.ObjectSource) Declaration(org.drools.core.rule.Declaration) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 9 with EntryPointNode

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

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

the class NodesPartitioningTest method checkDrl.

private void checkDrl(String drl) {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) new KieHelper().addContent(drl, ResourceType.DRL).build(MultithreadEvaluationOption.YES);
    Rete rete = kbase.getRete();
    for (EntryPointNode entryPointNode : rete.getEntryPointNodes().values()) {
        traverse(entryPointNode);
    }
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) Rete(org.drools.core.reteoo.Rete) KieHelper(org.kie.internal.utils.KieHelper) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Aggregations

EntryPointNode (org.drools.core.reteoo.EntryPointNode)23 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)16 ClassObjectType (org.drools.core.base.ClassObjectType)8 Test (org.junit.Test)8 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)7 KieSession (org.kie.api.runtime.KieSession)6 KieHelper (org.kie.internal.utils.KieHelper)6 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)5 ObjectSource (org.drools.core.reteoo.ObjectSource)5 KieBase (org.kie.api.KieBase)5 Rete (org.drools.core.reteoo.Rete)4 ArrayList (java.util.ArrayList)3 LeftTupleSink (org.drools.core.reteoo.LeftTupleSink)3 LeftTupleSource (org.drools.core.reteoo.LeftTupleSource)3 NodeFactory (org.drools.core.reteoo.builder.NodeFactory)3 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)2 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)2 InternalWorkingMemoryEntryPoint (org.drools.core.common.InternalWorkingMemoryEntryPoint)2 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)2 RuleBasePartitionId (org.drools.core.common.RuleBasePartitionId)2