Search in sources :

Example 11 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase 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 12 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase 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 13 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase 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 14 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase 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)

Example 15 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class StrEvaluatorTest method readKnowledgeBase.

private KieBase readKnowledgeBase() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("strevaluator_test.drl")), ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors.size() > 0) {
        for (KnowledgeBuilderError error : errors) {
            System.err.println(error);
        }
        throw new IllegalArgumentException("Could not parse knowledge." + errors.toArray());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    return kbase;
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Aggregations

InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)368 Test (org.junit.Test)307 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)203 KieSession (org.kie.api.runtime.KieSession)193 ArrayList (java.util.ArrayList)163 List (java.util.List)99 KiePackage (org.kie.api.definition.KiePackage)79 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)45 FactHandle (org.kie.api.runtime.rule.FactHandle)39 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)38 InternalFactHandle (org.drools.core.common.InternalFactHandle)37 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)37 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)37 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)37 Cheese (org.drools.compiler.Cheese)36 Person (org.drools.compiler.Person)36 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)34 FactType (org.kie.api.definition.type.FactType)26 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)24 HashMap (java.util.HashMap)23