Search in sources :

Example 91 with KnowledgeBuilder

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

the class LinkingTest method testAccumulateNodes1.

@Test
public void testAccumulateNodes1() 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 += "   accumulate( $a : A(); $l : collectList( $a ) ) \n";
    str += "then \n";
    str += "  list.add( $l.size() ); \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());
    KieSession wm = kbase.newKieSession();
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.fireAllRules();
    assertEquals(1, list.size());
    wm = kbase.newKieSession();
    list = new ArrayList();
    wm.setGlobal("list", list);
    FactHandle fh1 = wm.insert(new A(1));
    FactHandle fh2 = wm.insert(new A(2));
    FactHandle fh3 = wm.insert(new A(3));
    FactHandle fh4 = wm.insert(new A(4));
    wm.fireAllRules();
    assertEquals(4, list.get(0));
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) 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 92 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder 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 : A() \n";
    str += "   exists ( B() and exists( C() and D() ) and E() ) \n";
    str += "   $f : F() \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);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new A());
    wm.insert(new B());
    for (int i = 0; i < 28; i++) {
        wm.insert(new C());
    }
    for (int i = 0; i < 29; i++) {
        wm.insert(new D());
    }
    wm.insert(new E());
    wm.insert(new F());
    // DefaultAgenda agenda = ( DefaultAgenda ) wm.getAgenda();
    // InternalAgendaGroup group = (InternalAgendaGroup) agenda.getNextFocus();
    // AgendaItem item = (AgendaItem) group.remove();
    // int count = ((RuleAgendaItem)item).evaluateNetworkAndFire( wm );
    // //assertEquals(7, count ); // proves we correctly track nested sub network staged propagations
    // 
    // agenda.addActivation( item, true );
    // agenda = ( DefaultAgenda ) wm.getAgenda();
    // group = (InternalAgendaGroup) agenda.getNextFocus();
    // item = (AgendaItem) group.remove();
    // 
    // agenda.fireActivation( item );
    // assertEquals( 1, list.size() );
    // 
    // agenda = ( DefaultAgenda ) wm.getAgenda();
    // group = (InternalAgendaGroup) agenda.getNextFocus();
    // item = (AgendaItem) group.remove();
    // count = ((RuleAgendaItem)item).evaluateNetworkAndFire( wm );
    // //assertEquals(0, count );
    wm.fireAllRules();
    assertEquals(1, list.size());
    wm.fireAllRules();
    assertEquals(1, list.size());
}
Also used : ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) 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) Test(org.junit.Test)

Example 93 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder 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 94 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder 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 95 with KnowledgeBuilder

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

the class LinkingTest method testExistsNodes2.

@Test
public void testExistsNodes2() 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 += "   exists 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());
    KieSession wm = 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 C(i));
    }
    wm.fireAllRules();
    assertEquals(0, list.size());
    wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    list = new ArrayList();
    wm.setGlobal("list", list);
    for (int i = 0; i < 3; i++) {
        wm.insert(new A(i));
    }
    FactHandle fh = wm.insert(new B(1));
    for (int i = 0; i < 3; i++) {
        wm.insert(new C(i));
    }
    wm.fireAllRules();
    assertEquals(9, list.size());
    wm.retract(fh);
    wm.fireAllRules();
    assertEquals(9, list.size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) 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)

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