Search in sources :

Example 46 with LeftInputAdapterNode

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

the class LinkingTest method testNotNodeMasksWithoutConstraints.

@Test
public void testNotNodeMasksWithoutConstraints() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "import " + F.class.getCanonicalName() + "\n";
    str += "import " + G.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   A() \n";
    str += "   not( B( ) ) \n";
    str += "   C() \n";
    str += "then \n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
    assertEquals(1, liaNode.getSinkPropagator().size());
    wm.insert(new A());
    wm.flushPropagations();
    NotNode notNode = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
    JoinNode cNode = (JoinNode) notNode.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
    PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn);
    assertEquals(1, pmem.getSegmentMemories().length);
    assertEquals(1, pmem.getAllLinkedMaskTest());
    SegmentMemory sm = pmem.getSegmentMemories()[0];
    assertEquals(7, sm.getAllLinkedMaskTest());
    assertEquals(3, sm.getLinkedNodeMask());
    assertFalse(sm.isSegmentLinked());
    assertFalse(pmem.isRuleLinked());
    wm.insert(new C());
    wm.flushPropagations();
    assertEquals(7, sm.getLinkedNodeMask());
    assertTrue(sm.isSegmentLinked());
    assertTrue(pmem.isRuleLinked());
}
Also used : NotNode(org.drools.core.reteoo.NotNode) SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Example 47 with LeftInputAdapterNode

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

the class LinkingTest method testNotNodeMasksWithConstraints.

@Test
public void testNotNodeMasksWithConstraints() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "import " + F.class.getCanonicalName() + "\n";
    str += "import " + G.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   $a : A() \n";
    str += "   not( B( value == $a.value ) ) \n";
    str += "   C() \n";
    str += "then \n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
    assertEquals(1, liaNode.getSinkPropagator().size());
    wm.insert(new A());
    wm.flushPropagations();
    NotNode notNode = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
    JoinNode cNode = (JoinNode) notNode.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
    PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn);
    assertEquals(1, pmem.getSegmentMemories().length);
    assertEquals(1, pmem.getAllLinkedMaskTest());
    SegmentMemory sm = pmem.getSegmentMemories()[0];
    assertEquals(5, sm.getAllLinkedMaskTest());
    assertEquals(3, sm.getLinkedNodeMask());
    assertFalse(sm.isSegmentLinked());
    assertFalse(pmem.isRuleLinked());
    wm.insert(new C());
    wm.flushPropagations();
    // only 5 is needed to link, the 'not' turns on but it has no unfleunce either way
    assertEquals(7, sm.getLinkedNodeMask());
    assertTrue(sm.isSegmentLinked());
    assertTrue(pmem.isRuleLinked());
}
Also used : NotNode(org.drools.core.reteoo.NotNode) SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Example 48 with LeftInputAdapterNode

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

the class LinkingTest method testNonReactiveSubNetworkOwnSegmentMasks.

@Test
public void testNonReactiveSubNetworkOwnSegmentMasks() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "import " + F.class.getCanonicalName() + "\n";
    str += "import " + G.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   A() \n";
    str += "   exists( B() and C() ) \n";
    str += "   exists( eval(1==1) ) \n";
    str += "   D() \n";
    str += "then \n";
    str += "end \n";
    str += "rule rule2 when \n";
    str += "   A() \n";
    str += "   exists( B() and C() ) \n";
    str += "   exists( eval(1==1) ) \n";
    str += "   E() \n";
    str += "then \n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
    assertEquals(2, liaNode.getSinkPropagator().size());
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
    ExistsNode exists1n = (ExistsNode) liaNode.getSinkPropagator().getSinks()[1];
    EvalConditionNode evalNode = (EvalConditionNode) exists1n.getSinkPropagator().getSinks()[0];
    ExistsNode exists2n = (ExistsNode) exists1n.getSinkPropagator().getSinks()[1];
    JoinNode dNode = (JoinNode) exists2n.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn = (RuleTerminalNode) dNode.getSinkPropagator().getSinks()[0];
    wm.insert(new A());
    PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn);
    assertEquals(4, pmem.getSegmentMemories().length);
    // the exists eval segment does not need to be linked in
    assertEquals(11, pmem.getAllLinkedMaskTest());
    RightInputAdapterNode.RiaNodeMemory riaMem = (RightInputAdapterNode.RiaNodeMemory) wm.getNodeMemory((MemoryFactory) exists1n.getRightInput());
    // second segment must be linked in
    assertEquals(2, riaMem.getRiaPathMemory().getAllLinkedMaskTest());
    wm.insert(new B());
    wm.insert(new C());
    assertEquals(2, riaMem.getRiaPathMemory().getSegmentMemories().length);
    riaMem = (RightInputAdapterNode.RiaNodeMemory) wm.getNodeMemory((MemoryFactory) exists2n.getRightInput());
    // no segments to be linked in
    assertEquals(0, riaMem.getRiaPathMemory().getAllLinkedMaskTest());
}
Also used : EvalConditionNode(org.drools.core.reteoo.EvalConditionNode) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) MemoryFactory(org.drools.core.common.MemoryFactory) ExistsNode(org.drools.core.reteoo.ExistsNode) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Example 49 with LeftInputAdapterNode

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

the class LinkingTest method testNotNodeUnlinksWithNoConstriants.

@Test
public void testNotNodeUnlinksWithNoConstriants() {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "import " + F.class.getCanonicalName() + "\n";
    str += "import " + G.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   $a : A() \n";
    str += "   not B() \n";
    str += "   $c : C() \n";
    str += "then \n";
    str += "  list.add( 'x' ); \n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ObjectTypeNode aotn = getObjectTypeNode(kbase, A.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    LeftInputAdapterNode aNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    NotNode bNode = (NotNode) aNode.getSinkPropagator().getSinks()[0];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
    SegmentUtilities.createSegmentMemory(cNode, wm);
    LiaNodeMemory amem = (LiaNodeMemory) wm.getNodeMemory(aNode);
    // Only NotNode is linked in
    assertEquals(2, amem.getSegmentMemory().getLinkedNodeMask());
    FactHandle fha = wm.insert(new A());
    FactHandle fhb = wm.insert(new B());
    FactHandle fhc = wm.insert(new C());
    wm.fireAllRules();
    assertEquals(0, list.size());
    // NotNode unlinks, which is allowed because it has no variable constraints
    assertEquals(5, amem.getSegmentMemory().getLinkedNodeMask());
    // NotNode links back in again, which is allowed because it has no variable constraints
    wm.retract(fhb);
    wm.flushPropagations();
    assertEquals(7, amem.getSegmentMemory().getLinkedNodeMask());
    wm.fireAllRules();
    assertEquals(1, list.size());
    // Now try with lots of facthandles on NotNode
    list.clear();
    List<FactHandle> handles = new ArrayList<FactHandle>();
    for (int i = 0; i < 5; i++) {
        handles.add(wm.insert(new B()));
    }
    wm.fireAllRules();
    assertEquals(0, list.size());
    assertEquals(5, amem.getSegmentMemory().getLinkedNodeMask());
    for (FactHandle fh : handles) {
        wm.retract(fh);
    }
    wm.flushPropagations();
    assertEquals(7, amem.getSegmentMemory().getLinkedNodeMask());
    wm.fireAllRules();
    assertEquals(1, list.size());
}
Also used : NotNode(org.drools.core.reteoo.NotNode) FactHandle(org.kie.api.runtime.rule.FactHandle) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 50 with LeftInputAdapterNode

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

the class LinkingTest method testNonReactiveSubNetworkInShareMasks.

@Test
public void testNonReactiveSubNetworkInShareMasks() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "import " + F.class.getCanonicalName() + "\n";
    str += "import " + G.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   A() \n";
    str += "   exists( B() and C() ) \n";
    str += "   exists( eval(1==1) ) \n";
    str += "   D() \n";
    str += "then \n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
    assertEquals(2, liaNode.getSinkPropagator().size());
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
    ExistsNode exists1n = (ExistsNode) liaNode.getSinkPropagator().getSinks()[1];
    EvalConditionNode evalNode = (EvalConditionNode) exists1n.getSinkPropagator().getSinks()[0];
    ExistsNode exists2n = (ExistsNode) exists1n.getSinkPropagator().getSinks()[1];
    JoinNode dNode = (JoinNode) exists2n.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn = (RuleTerminalNode) dNode.getSinkPropagator().getSinks()[0];
    wm.insert(new A());
    PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn);
    assertEquals(3, pmem.getSegmentMemories().length);
    // D is in the exists segment
    assertEquals(7, pmem.getAllLinkedMaskTest());
    BetaMemory bm = (BetaMemory) wm.getNodeMemory(dNode);
    // check lazy initialization
    assertNull(bm.getSegmentMemory());
    wm.insert(new D());
    wm.flushPropagations();
    // only D can be linked in
    assertEquals(2, bm.getSegmentMemory().getAllLinkedMaskTest());
}
Also used : EvalConditionNode(org.drools.core.reteoo.EvalConditionNode) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) ExistsNode(org.drools.core.reteoo.ExistsNode) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Aggregations

LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)72 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)60 Test (org.junit.Test)56 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)48 KieBase (org.kie.api.KieBase)35 JoinNode (org.drools.core.reteoo.JoinNode)28 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)28 SegmentMemory (org.drools.core.reteoo.SegmentMemory)28 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)27 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)25 BetaMemory (org.drools.core.reteoo.BetaMemory)25 ArrayList (java.util.ArrayList)17 PathMemory (org.drools.core.reteoo.PathMemory)15 List (java.util.List)12 FactHandle (org.kie.api.runtime.rule.FactHandle)12 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)12 ClassObjectType (org.drools.core.base.ClassObjectType)11 AlphaNode (org.drools.core.reteoo.AlphaNode)11 NotNode (org.drools.core.reteoo.NotNode)11 ExistsNode (org.drools.core.reteoo.ExistsNode)10