Search in sources :

Example 86 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class LinkingTest method testNotNodeDoesNotUnlinksWithConstriants.

@Test
public void testNotNodeDoesNotUnlinksWithConstriants() {
    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 : 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(1));
    FactHandle fhc = wm.insert(new C());
    wm.flushPropagations();
    // All nodes are linked in
    assertEquals(7, amem.getSegmentMemory().getLinkedNodeMask());
    // NotNode does not unlink, due to variable constraint
    wm.retract(fhb);
    wm.flushPropagations();
    assertEquals(7, amem.getSegmentMemory().getLinkedNodeMask());
}
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 87 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class LinkingTest method testForallNodes.

@Test
public void testForallNodes() 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 += "   forall( 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);
    ObjectTypeNode botn = getObjectTypeNode(kbase, A.class);
    ObjectTypeNode cotn = getObjectTypeNode(kbase, A.class);
    KieSession wm = kbase.newKieSession();
    List list = new ArrayList();
    // wm.setGlobal( "list", list );
    // 
    // for ( int i = 0; i < 3; i++ ) {
    // wm.insert(  new A(i) );
    // }
    // 
    // wm.insert(  new B(2) );
    // 
    // for ( int i = 0; i < 3; i++ ) {
    // wm.insert(  new C(i) );
    // }
    // 
    // wm.fireAllRules();
    // assertEquals( 0, list.size() );
    wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    list = new ArrayList();
    wm.setGlobal("list", list);
    for (int i = 0; i < 2; i++) {
        wm.insert(new A(i));
    }
    for (int i = 0; i < 27; i++) {
        wm.insert(new B(1));
    }
    for (int i = 0; i < 2; i++) {
        wm.insert(new C(i));
    }
    wm.fireAllRules();
    assertEquals(4, list.size());
// wm.retract( fh );
// wm.fireAllRules();
// assertEquals( 9, list.size() );
}
Also used : ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 88 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class LinkingTest method testNotNodes1.

@Test
public void testNotNodes1() 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 += "   not A() \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());
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.fireAllRules();
    assertEquals(1, list.size());
    wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    list = new ArrayList();
    wm.setGlobal("list", list);
    FactHandle fh = wm.insert(new A(1));
    wm.fireAllRules();
    assertEquals(0, list.size());
    wm.retract(fh);
    wm.fireAllRules();
    assertEquals(1, list.size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 89 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class LinkingTest method testJoinNodes.

@Test
public void testJoinNodes() 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 += "   $b : B() \n";
    str += "   $c : C() \n";
    str += "then \n";
    str += "  list.add( $a.getValue() + \":\"+ $b.getValue() + \":\" + $c.getValue() ); \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);
    ObjectTypeNode botn = getObjectTypeNode(kbase, B.class);
    ObjectTypeNode cotn = getObjectTypeNode(kbase, C.class);
    InternalWorkingMemory wm = (InternalWorkingMemory) kbase.newKieSession();
    List list = new ArrayList();
    wm.setGlobal("list", list);
    for (int i = 0; i < 3; i++) {
        wm.insert(new A(i));
    }
    for (int i = 0; i < 3; i++) {
        wm.insert(new B(i));
    }
    for (int i = 0; i < 29; i++) {
        wm.insert(new C(i));
    }
    wm.flushPropagations();
    LeftInputAdapterNode aNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) aNode.getSinkPropagator().getSinks()[0];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
    LiaNodeMemory amem = (LiaNodeMemory) wm.getNodeMemory(aNode);
    BetaMemory bmem = (BetaMemory) wm.getNodeMemory(bNode);
    BetaMemory cmem = (BetaMemory) wm.getNodeMemory(cNode);
    // amem.getSegmentMemory().getStagedLeftTuples().insertSize() == 3
    assertNotNull(amem.getSegmentMemory().getStagedLeftTuples().getInsertFirst());
    assertNotNull(amem.getSegmentMemory().getStagedLeftTuples().getInsertFirst().getStagedNext());
    assertNotNull(amem.getSegmentMemory().getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
    assertNull(amem.getSegmentMemory().getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext().getStagedNext());
    // assertEquals( 3, bmem.getStagedRightTuples().insertSize() );
    assertNotNull(bmem.getStagedRightTuples().getInsertFirst());
    assertNotNull(bmem.getStagedRightTuples().getInsertFirst().getStagedNext());
    assertNotNull(bmem.getStagedRightTuples().getInsertFirst().getStagedNext().getStagedNext());
    assertNull(bmem.getStagedRightTuples().getInsertFirst().getStagedNext().getStagedNext().getStagedNext());
    wm.fireAllRules();
    assertNull(amem.getSegmentMemory().getStagedLeftTuples().getInsertFirst());
    assertNull(bmem.getStagedRightTuples().getInsertFirst());
    assertNull(cmem.getStagedRightTuples().getInsertFirst());
    assertEquals(261, list.size());
    assertTrue(list.contains("2:2:14"));
    assertTrue(list.contains("1:0:6"));
    assertTrue(list.contains("0:1:1"));
    assertTrue(list.contains("2:2:14"));
    assertTrue(list.contains("0:0:25"));
}
Also used : JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) BetaMemory(org.drools.core.reteoo.BetaMemory) 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 90 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class LinkingTest method testSubNetworkSharingMemories.

@Test
public void testSubNetworkSharingMemories() throws Exception {
    // checks the memory sharing works, and linking, uses the already checked network from testSubNetworkSharing
    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 liaNodeA = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
    ExistsNode existsNode2 = (ExistsNode) liaNodeA.getSinkPropagator().getSinks()[1];
    ExistsNode existsNode3 = (ExistsNode) liaNodeA.getSinkPropagator().getSinks()[2];
    JoinNode joinNodeB = (JoinNode) liaNodeA.getSinkPropagator().getSinks()[0];
    JoinNode joinNodeC = (JoinNode) joinNodeB.getSinkPropagator().getSinks()[0];
    JoinNode joinNodeD1 = (JoinNode) joinNodeC.getSinkPropagator().getSinks()[0];
    JoinNode joinNodeD2 = (JoinNode) existsNode2.getSinkPropagator().getSinks()[0];
    JoinNode joinNodeE = (JoinNode) existsNode3.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn1 = (RuleTerminalNode) joinNodeD1.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn2 = (RuleTerminalNode) joinNodeD2.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn3 = (RuleTerminalNode) joinNodeE.getSinkPropagator().getSinks()[0];
    FactHandle fha = wm.insert(new A());
    wm.insert(new B());
    wm.insert(new C());
    wm.insert(new D());
    wm.flushPropagations();
    BetaMemory bm = null;
    LiaNodeMemory liam = (LiaNodeMemory) wm.getNodeMemory(liaNodeA);
    BetaMemory bm1 = (BetaMemory) wm.getNodeMemory(joinNodeB);
    BetaMemory bm2 = (BetaMemory) wm.getNodeMemory(joinNodeC);
    BetaMemory bm3 = (BetaMemory) wm.getNodeMemory(joinNodeD1);
    assertEquals(1, liam.getNodePosMaskBit());
    assertEquals(1, bm1.getNodePosMaskBit());
    assertEquals(2, bm2.getNodePosMaskBit());
    assertEquals(1, bm3.getNodePosMaskBit());
    assertNotSame(liam.getSegmentMemory(), bm1.getSegmentMemory());
    assertSame(bm1.getSegmentMemory(), bm2.getSegmentMemory());
    assertNotSame(bm2.getSegmentMemory(), bm3.getSegmentMemory());
    BetaMemory bm4 = (BetaMemory) wm.getNodeMemory(existsNode2);
    BetaMemory bm5 = (BetaMemory) wm.getNodeMemory(joinNodeD2);
    assertEquals(1, bm4.getNodePosMaskBit());
    assertEquals(2, bm5.getNodePosMaskBit());
    assertSame(bm4.getSegmentMemory(), bm5.getSegmentMemory());
    PathMemory rs1 = (PathMemory) wm.getNodeMemory(rtn1);
    PathMemory rs2 = (PathMemory) wm.getNodeMemory(rtn2);
    PathMemory rs3 = (PathMemory) wm.getNodeMemory(rtn3);
    assertTrue(rs1.isRuleLinked());
    assertTrue(rs2.isRuleLinked());
    // no E yet
    assertFalse(rs3.isRuleLinked());
    wm.insert(new E());
    wm.flushPropagations();
    BetaMemory bm6 = (BetaMemory) wm.getNodeMemory(existsNode3);
    BetaMemory bm7 = (BetaMemory) wm.getNodeMemory(joinNodeE);
    assertEquals(1, bm6.getNodePosMaskBit());
    assertEquals(2, bm7.getNodePosMaskBit());
    assertSame(bm6.getSegmentMemory(), bm7.getSegmentMemory());
    assertTrue(rs1.isRuleLinked());
    assertTrue(rs2.isRuleLinked());
    assertTrue(rs3.isRuleLinked());
    wm.retract(fha);
    // need to have rules evalulated, for unlinking to happen
    wm.fireAllRules();
    assertFalse(rs1.isRuleLinked());
    assertFalse(rs2.isRuleLinked());
    assertFalse(rs3.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) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) 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

KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)376 Test (org.junit.Test)302 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)231 KieSession (org.kie.api.runtime.KieSession)149 ArrayList (java.util.ArrayList)112 List (java.util.List)65 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)52 KiePackage (org.kie.api.definition.KiePackage)39 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)38 FactHandle (org.kie.api.runtime.rule.FactHandle)36 FactType (org.kie.api.definition.type.FactType)27 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)24 Person (org.drools.compiler.Person)23 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)23 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)22 InternalFactHandle (org.drools.core.common.InternalFactHandle)19 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)17 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)17 StringReader (java.io.StringReader)15 Resource (org.kie.api.io.Resource)15