Search in sources :

Example 96 with KnowledgeBuilder

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

the class LinkingTest method testExistsNodes1.

@Test
public void testExistsNodes1() 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 += "   exists 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());
    KieSession wm = kbase.newKieSession();
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.fireAllRules();
    assertEquals(0, list.size());
    wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    list = new ArrayList();
    wm.setGlobal("list", list);
    FactHandle fh = wm.insert(new A(1));
    wm.fireAllRules();
    assertEquals(1, list.size());
    wm.retract(fh);
    wm.fireAllRules();
    assertEquals(1, 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)

Example 97 with KnowledgeBuilder

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

the class LinkingTest method testAccumulateNodes2.

@Test
public void testAccumulateNodes2() 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 += "   accumulate( $a : B(); $l : collectList( $a ) ) \n";
    str += "   C() \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(0, list.size());
    wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    list = new ArrayList();
    wm.setGlobal("list", list);
    FactHandle fh1 = wm.insert(new B(1));
    FactHandle fh2 = wm.insert(new B(2));
    FactHandle fh3 = wm.insert(new B(3));
    FactHandle fh4 = wm.insert(new B(4));
    FactHandle fha = wm.insert(new A(1));
    FactHandle fhc = wm.insert(new C(1));
    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) 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 98 with KnowledgeBuilder

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

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

the class MVELTest method testNestedEnum.

@Test
public void testNestedEnum() {
    final String str = "" + "package org.drools.compiler.test \n" + "import " + Triangle.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when  \n" + "    $t: Triangle(t == Triangle.Type.ACUTE) \n" + "then \n" + "    list.add($t.getT()); \n" + "end \n";
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    final KieSession ksession = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Triangle t = new Triangle(Triangle.Type.ACUTE);
    ksession.insert(t);
    ksession.fireAllRules();
    assertEquals(Triangle.Type.ACUTE, list.get(0));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) 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 100 with KnowledgeBuilder

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

the class MVELTest method testSizeCheckInObject.

@Test
public void testSizeCheckInObject() {
    final String str = "" + "package org.drools.compiler.test \n" + "import " + Triangle.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when  \n" + "    $m : Triangle( deliveries.size == 0) \n" + "then \n" + "    list.add('r1'); \n" + "end \n";
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) 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