Search in sources :

Example 16 with KnowledgeBuilder

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

the class StreamsTest method testEventExpirationValue.

@Test(timeout = 10000)
public void testEventExpirationValue() throws Exception {
    String drl1 = "package org.drools.pkg1\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick\n" + "    @role(event)\n" + "end\n" + "rule X\n" + "when\n" + "    StockTick()\n" + "then\n" + "end\n";
    String drl2 = "package org.drools.pkg2\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick\n" + "    @role(event)\n" + "end\n" + "rule X\n" + "when\n" + "    StockTick()\n" + "then\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl1.getBytes()), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newByteArrayResource(drl2.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EventProcessingOption.STREAM);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kconf);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    List<ObjectTypeNode> otns = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
    ObjectType stot = new ClassObjectType(StockTick.class);
    for (ObjectTypeNode otn : otns) {
        if (otn.getObjectType().isAssignableFrom(stot)) {
            assertEquals(NEVER_EXPIRES, otn.getExpirationOffset());
        }
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 17 with KnowledgeBuilder

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

the class StreamsTest method loadKnowledgeBase.

private KieBase loadKnowledgeBase(final String fileName, KieBaseConfiguration kconf) throws IOException, DroolsParserException, Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource(fileName, getClass()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        System.out.println(kbuilder.getErrors());
        return null;
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kconf);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    return SerializationHelper.serializeObject(kbase);
// return kbase;
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 18 with KnowledgeBuilder

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

the class SegmentMemoryPrototypeTest method testSegmentMemoryPrototype.

@Test
public void testSegmentMemoryPrototype() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(DRL.getBytes()), ResourceType.DRL);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = kbase.newKieSession();
    checkKieSession(ksession);
    // Create a 2nd KieSession (that will use segment memory prototype) and check that it works as the former one
    KieSession ksession2 = kbase.newKieSession();
    checkKieSession(ksession2);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 19 with KnowledgeBuilder

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

the class SegmentMemoryPrototypeTest method testSessionCache.

@Test
public void testSessionCache() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(DRL.getBytes()), ResourceType.DRL);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kbase.newKieSession();
    checkKieSession(ksession);
    ksession.dispose();
    ksession.reset();
    checkKieSession(ksession);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 20 with KnowledgeBuilder

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

the class SerializedPackageMergeTest method testBuildAndSerializePackagesWithSamePackageName.

@Test
public void testBuildAndSerializePackagesWithSamePackageName() throws IOException, ClassNotFoundException {
    // RHBRMS-2773
    String str1 = "package com.sample\n" + "import org.drools.compiler.Person\n" + "global java.util.List list\n" + "rule R1 when\n" + "  $p : Person( name == \"John\" )\n" + "then\n" + "  list.add($p);" + "end\n";
    String str2 = "package com.sample\n" + "import org.drools.compiler.Person\n" + "global java.util.List list\n" + "rule R2 when\n" + "  $p : Person( name == \"Paul\" )\n" + "then\n" + "  list.add($p);" + "end\n";
    // Create 2 knowledgePackages separately (but these rules have the same package name)
    KnowledgeBuilder builder1 = KnowledgeBuilderFactory.newKnowledgeBuilder();
    builder1.add(ResourceFactory.newByteArrayResource(str1.getBytes()), ResourceType.DRL);
    Collection<KiePackage> knowledgePackages1 = builder1.getKnowledgePackages();
    KnowledgeBuilder builder2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
    builder2.add(ResourceFactory.newByteArrayResource(str2.getBytes()), ResourceType.DRL);
    Collection<KiePackage> knowledgePackages2 = builder2.getKnowledgePackages();
    // Combine the knowledgePackages
    InternalKnowledgeBase knowledgeBase1 = KnowledgeBaseFactory.newKnowledgeBase();
    knowledgeBase1.addPackages(knowledgePackages1);
    knowledgeBase1.addPackages(knowledgePackages2);
    Collection<KiePackage> knowledgePackagesCombined = knowledgeBase1.getKiePackages();
    // serialize
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new DroolsObjectOutputStream(baos);
    out.writeObject(knowledgePackagesCombined);
    out.flush();
    out.close();
    // deserialize
    ObjectInputStream in = new DroolsObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    Collection<KiePackage> deserializedPackages = (Collection<KiePackage>) in.readObject();
    // Use the deserialized knowledgePackages
    InternalKnowledgeBase knowledgeBase2 = KnowledgeBaseFactory.newKnowledgeBase();
    knowledgeBase2.addPackages(deserializedPackages);
    KieSession ksession = knowledgeBase2.newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.insert(new org.drools.compiler.Person("John"));
    ksession.insert(new org.drools.compiler.Person("Paul"));
    ksession.fireAllRules();
    assertEquals(2, list.size());
    ksession.dispose();
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DroolsObjectOutputStream(org.drools.core.common.DroolsObjectOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) ByteArrayInputStream(java.io.ByteArrayInputStream) Collection(java.util.Collection) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) DroolsObjectOutputStream(org.drools.core.common.DroolsObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) 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