Search in sources :

Example 1 with Rete

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

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

use of org.drools.core.reteoo.Rete 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)

Example 4 with Rete

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

the class MemoryLeakTest method testStagedTupleLeak.

@Test
public void testStagedTupleLeak() throws Exception {
    // BZ-1056599
    String str = "rule R1 when\n" + "    $i : Integer()\n" + "then\n" + "    insertLogical( $i.toString() );\n" + "end\n" + "\n" + "rule R2 when\n" + "    $i : Integer()\n" + "then\n" + "    delete( $i );\n" + "end\n" + "\n" + "rule R3 when\n" + "    $l : Long()\n" + "    $s : String( this == $l.toString() )\n" + "then\n" + "end\n";
    KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();
    for (int i = 0; i < 10; i++) {
        ksession.insert(i);
        ksession.fireAllRules();
    }
    Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
    JoinNode joinNode = null;
    for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
        if (String.class == otn.getObjectType().getValueType().getClassType()) {
            joinNode = (JoinNode) otn.getObjectSinkPropagator().getSinks()[0];
            break;
        }
    }
    assertNotNull(joinNode);
    InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
    BetaMemory memory = (BetaMemory) wm.getNodeMemory(joinNode);
    TupleSets<RightTuple> stagedRightTuples = memory.getStagedRightTuples();
    assertNull(stagedRightTuples.getDeleteFirst());
    assertNull(stagedRightTuples.getInsertFirst());
}
Also used : Rete(org.drools.core.reteoo.Rete) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieHelper(org.kie.internal.utils.KieHelper) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTuple(org.drools.core.reteoo.RightTuple) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 5 with Rete

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

the class KnowledgeBaseImpl method setupRete.

protected void setupRete() {
    this.rete = new Rete(this);
    this.reteooBuilder = new ReteooBuilder(this);
    NodeFactory nodeFactory = CoreComponentFactory.get().getNodeFactoryService();
    // always add the default entry point
    EntryPointNode epn = nodeFactory.buildEntryPointNode(this.reteooBuilder.getNodeIdsGenerator().getNextId(), RuleBasePartitionId.MAIN_PARTITION, this.getConfiguration().isMultithreadEvaluation(), this.rete, EntryPointId.DEFAULT);
    epn.attach();
    BuildContext context = new BuildContext(this, Collections.emptyList());
    context.setCurrentEntryPoint(epn.getEntryPoint());
    context.setTupleMemoryEnabled(true);
    context.setObjectTypeNodeMemoryEnabled(true);
    context.setPartitionId(RuleBasePartitionId.MAIN_PARTITION);
    ObjectTypeNode otn = nodeFactory.buildObjectTypeNode(this.reteooBuilder.getNodeIdsGenerator().getNextId(), epn, ClassObjectType.InitialFact_ObjectType, context);
    otn.attach(context);
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) Rete(org.drools.core.reteoo.Rete) ReteooBuilder(org.drools.core.reteoo.ReteooBuilder) NodeFactory(org.drools.core.reteoo.builder.NodeFactory) BuildContext(org.drools.core.reteoo.builder.BuildContext) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode)

Aggregations

Rete (org.drools.core.reteoo.Rete)26 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)20 Test (org.junit.Test)20 ClassObjectType (org.drools.core.base.ClassObjectType)11 KieBase (org.kie.api.KieBase)10 EntryPointNode (org.drools.core.reteoo.EntryPointNode)8 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)8 KieSession (org.kie.api.runtime.KieSession)8 ArrayList (java.util.ArrayList)7 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)7 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)6 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)6 MockObjectSink (org.drools.core.reteoo.MockObjectSink)6 FactHandle (org.kie.api.runtime.rule.FactHandle)6 KieHelper (org.kie.internal.utils.KieHelper)6 List (java.util.List)5 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)5 RuleBase (org.drools.core.impl.RuleBase)5 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)5 LeftTupleSink (org.drools.core.reteoo.LeftTupleSink)4