Search in sources :

Example 61 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class LinkingTest method testNestedSubNetwork.

@Test
public void testNestedSubNetwork() 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 += "   B() \n";
    str += "   exists( C() and D() and exists( E() and F() ) ) \n";
    str += "   G() \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());
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
    assertEquals(2, bNode.getSinkPropagator().size());
    ExistsNode exists1n = (ExistsNode) bNode.getSinkPropagator().getSinks()[1];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
    JoinNode dNode = (JoinNode) cNode.getSinkPropagator().getSinks()[0];
    assertEquals(2, dNode.getSinkPropagator().size());
    ExistsNode exists2n = (ExistsNode) dNode.getSinkPropagator().getSinks()[1];
    JoinNode eNode = (JoinNode) dNode.getSinkPropagator().getSinks()[0];
    JoinNode fNode = (JoinNode) eNode.getSinkPropagator().getSinks()[0];
    RightInputAdapterNode riaNode2 = (RightInputAdapterNode) fNode.getSinkPropagator().getSinks()[0];
    assertEquals(exists2n, riaNode2.getObjectSinkPropagator().getSinks()[0]);
    RightInputAdapterNode riaNode1 = (RightInputAdapterNode) exists2n.getSinkPropagator().getSinks()[0];
    assertEquals(exists1n, riaNode1.getObjectSinkPropagator().getSinks()[0]);
    JoinNode gNode = (JoinNode) exists1n.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn = (RuleTerminalNode) gNode.getSinkPropagator().getSinks()[0];
    wm.insert(new A());
    wm.insert(new B());
    wm.insert(new C());
    wm.insert(new D());
    wm.insert(new F());
    wm.insert(new G());
    PathMemory rs = (PathMemory) wm.getNodeMemory(rtn);
    assertFalse(rs.isRuleLinked());
    FactHandle fhE1 = wm.insert(new E());
    FactHandle fhE2 = wm.insert(new E());
    wm.flushPropagations();
    assertTrue(rs.isRuleLinked());
    wm.retract(fhE1);
    wm.flushPropagations();
    assertTrue(rs.isRuleLinked());
    wm.retract(fhE2);
    wm.flushPropagations();
    assertFalse(rs.isRuleLinked());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) 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 62 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class LinkingTest method testSubNetworkRiaLinking.

@Test
public void testSubNetworkRiaLinking() 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 += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   A() \n";
    str += "   B() \n";
    str += "   exists( C() and D() ) \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(1, liaNode.getSinkPropagator().size());
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
    assertEquals(2, bNode.getSinkPropagator().size());
    ExistsNode exists1n = (ExistsNode) bNode.getSinkPropagator().getSinks()[1];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
    JoinNode dNode = (JoinNode) cNode.getSinkPropagator().getSinks()[0];
    assertEquals(1, dNode.getSinkPropagator().size());
    RightInputAdapterNode riaNode1 = (RightInputAdapterNode) dNode.getSinkPropagator().getSinks()[0];
    JoinNode eNode = (JoinNode) exists1n.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn = (RuleTerminalNode) eNode.getSinkPropagator().getSinks()[0];
    SegmentUtilities.createSegmentMemory(exists1n, wm);
    BetaMemory existsBm = (BetaMemory) wm.getNodeMemory(exists1n);
    assertEquals(0, existsBm.getSegmentMemory().getLinkedNodeMask());
    FactHandle fhc = wm.insert(new C());
    FactHandle fhd = wm.insert(new D());
    wm.flushPropagations();
    // exists is start of new segment
    assertEquals(1, existsBm.getSegmentMemory().getLinkedNodeMask());
    wm.retract(fhd);
    wm.flushPropagations();
    assertEquals(0, existsBm.getSegmentMemory().getLinkedNodeMask());
    PathMemory rs = (PathMemory) wm.getNodeMemory(rtn);
    assertFalse(rs.isRuleLinked());
    wm.insert(new A());
    wm.flushPropagations();
    assertFalse(rs.isRuleLinked());
    wm.insert(new B());
    wm.flushPropagations();
    assertFalse(rs.isRuleLinked());
    wm.insert(new E());
    wm.flushPropagations();
    assertFalse(rs.isRuleLinked());
    wm.insert(new D());
    wm.flushPropagations();
    assertTrue(rs.isRuleLinked());
    wm.retract(fhc);
    wm.flushPropagations();
    assertFalse(rs.isRuleLinked());
    wm.insert(new C());
    wm.flushPropagations();
    assertTrue(rs.isRuleLinked());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) 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) 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 63 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class LinkingTest method testSubNetworkSharing.

@Test
public void testSubNetworkSharing() throws Exception {
    // Checks the network is correctly formed, with sharing
    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 += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   A() \n";
    str += "   B() \n";
    str += "   C() \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 += "   D() \n";
    str += "then \n";
    str += "end \n";
    str += "rule rule3 when \n";
    str += "   A() \n";
    str += "   exists( B() and C() and D() ) \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(3, liaNode.getSinkPropagator().size());
    ExistsNode existsNode2 = (ExistsNode) liaNode.getSinkPropagator().getSinks()[1];
    ExistsNode existsNode3 = (ExistsNode) liaNode.getSinkPropagator().getSinks()[2];
    JoinNode joinNodeB = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
    assertSame(joinNodeB.getRightInput(), getObjectTypeNode(kbase, B.class));
    JoinNode joinNodeC = (JoinNode) joinNodeB.getSinkPropagator().getSinks()[0];
    assertSame(joinNodeC.getRightInput(), getObjectTypeNode(kbase, C.class));
    assertEquals(2, joinNodeC.getSinkPropagator().size());
    JoinNode joinNodeD = (JoinNode) joinNodeC.getSinkPropagator().getSinks()[0];
    assertSame(joinNodeD.getRightInput(), getObjectTypeNode(kbase, D.class));
    assertEquals(2, joinNodeD.getSinkPropagator().size());
    assertSame(existsNode2, ((RightInputAdapterNode) joinNodeC.getSinkPropagator().getSinks()[1]).getObjectSinkPropagator().getSinks()[0]);
    assertSame(existsNode3, ((RightInputAdapterNode) joinNodeD.getSinkPropagator().getSinks()[1]).getObjectSinkPropagator().getSinks()[0]);
}
Also used : JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) 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) Test(org.junit.Test)

Example 64 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory 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 65 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class ActivationIteratorTest method testFilteredEagerEvaluation.

@Test(timeout = 10000)
public void testFilteredEagerEvaluation() throws Exception {
    String str = "package org.simple \n" + "rule xxx @Propagation(EAGER) \n" + "when \n" + "  $s : String()\n" + "then \n" + "end  \n" + "rule yyy @Propagation(EAGER) \n" + "when \n" + "  $s : String()\n" + "then \n" + "end  \n";
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(new ForceEagerActivationOption.FILTERED(new ForceEagerActivationFilter() {

        @Override
        public boolean accept(Rule rule) {
            return rule.getName().equals("xxx");
        }
    }));
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = createKnowledgeSession(kbase, conf);
    final List list = new ArrayList();
    AgendaEventListener agendaEventListener = new DefaultAgendaEventListener() {

        public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
            list.add("activated");
        }
    };
    ksession.addEventListener(agendaEventListener);
    ksession.insert("test");
    ((InternalWorkingMemory) ksession).flushPropagations();
    assertEquals(1, list.size());
}
Also used : ForceEagerActivationOption(org.kie.internal.runtime.conf.ForceEagerActivationOption) ArrayList(java.util.ArrayList) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ForceEagerActivationFilter(org.kie.internal.runtime.conf.ForceEagerActivationFilter) KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Rule(org.kie.api.definition.rule.Rule) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Aggregations

InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)285 Test (org.junit.Test)217 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)140 KieBase (org.kie.api.KieBase)129 ArrayList (java.util.ArrayList)96 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)91 List (java.util.List)76 SegmentMemory (org.drools.core.reteoo.SegmentMemory)74 KieSession (org.kie.api.runtime.KieSession)71 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)69 JoinNode (org.drools.core.reteoo.JoinNode)62 BetaMemory (org.drools.core.reteoo.BetaMemory)57 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)48 PathMemory (org.drools.core.reteoo.PathMemory)48 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)45 BetaNode (org.drools.core.reteoo.BetaNode)44 FactHandle (org.kie.api.runtime.rule.FactHandle)44 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)35 AlphaNode (org.drools.core.reteoo.AlphaNode)35 ClassObjectType (org.drools.core.base.ClassObjectType)30