Search in sources :

Example 6 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class MarshallingTest method testSerializeAdd.

@Test
public void testSerializeAdd() throws Exception {
    Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Dynamic1.drl");
    kpkgs = SerializationHelper.serializeObject(kpkgs);
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
    kBase.addPackages(kpkgs);
    kBase = SerializationHelper.serializeObject(kBase);
    KieSession session = kBase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    InternalFactHandle stilton = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
    InternalFactHandle brie = (InternalFactHandle) session.insert(new Cheese("brie", 10));
    session.fireAllRules();
    assertEquals(list.size(), 1);
    assertEquals("stilton", list.get(0));
    // now recreate the rulebase, deserialize the session and test it
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    list = (List) session.getGlobal("list");
    assertNotNull(list);
    assertEquals(list.size(), 1);
    assertEquals("stilton", list.get(0));
    kpkgs = loadKnowledgePackages("../test_Dynamic3.drl");
    kpkgs = SerializationHelper.serializeObject(kpkgs);
    kBase.addPackages(kpkgs);
    InternalFactHandle stilton2 = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
    InternalFactHandle brie2 = (InternalFactHandle) session.insert(new Cheese("brie", 10));
    InternalFactHandle bob = (InternalFactHandle) session.insert(new Person("bob", 30));
    session.fireAllRules();
    assertEquals(list.size(), 3);
    assertEquals(bob.getObject(), list.get(2));
    assertEquals("stilton", list.get(1));
    session.dispose();
}
Also used : KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) Cheese(org.drools.compiler.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 7 with KiePackage

use of org.kie.api.definition.KiePackage 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 8 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class StatelessSessionTest method getSession2.

private StatelessKieSession getSession2(final Resource resource) throws Exception {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(resource, ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        System.out.println(kbuilder.getErrors());
    }
    assertFalse(kbuilder.hasErrors());
    final Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(pkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    final StatelessKieSession session = kbase.newStatelessKieSession();
    session.setGlobal("list", this.list);
    session.setGlobal("cheesery", this.cheesery);
    return session;
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 9 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class MarshallingTest method testSerializabilityWithJarFacts.

/**
 * In this case we are dealing with facts which are not on the systems classpath.
 */
@Test
public void testSerializabilityWithJarFacts() throws Exception {
    MapBackedClassLoader loader = new MapBackedClassLoader(this.getClass().getClassLoader());
    JarInputStream jis = new JarInputStream(this.getClass().getResourceAsStream("/billasurf.jar"));
    JarEntry entry = null;
    byte[] buf = new byte[1024];
    int len = 0;
    while ((entry = jis.getNextJarEntry()) != null) {
        if (!entry.isDirectory()) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            while ((len = jis.read(buf)) >= 0) {
                out.write(buf, 0, len);
            }
            loader.addResource(entry.getName(), out.toByteArray());
        }
    }
    String drl = "package foo.bar \n" + "import com.billasurf.Board\n" + "rule 'MyGoodRule' \n dialect 'mvel' \n when " + "   Board() " + "then \n" + " System.err.println(42); \n" + "end\n";
    KnowledgeBuilderConfiguration kbuilderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader);
    Collection<KiePackage> kpkgs = loadKnowledgePackagesFromString(kbuilderConf, drl);
    kpkgs = SerializationHelper.serializeObject(kpkgs, loader);
}
Also used : JarInputStream(java.util.jar.JarInputStream) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MapBackedClassLoader(org.drools.core.rule.MapBackedClassLoader) JarEntry(java.util.jar.JarEntry) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 10 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class MarshallingTest method testSignedSerialization4.

/*
     *  A client environment configured to use signed serialization
     *  should refuse any non-signed serialized rulebase
     */
@Test
public void testSignedSerialization4() throws Exception {
    Collection<KiePackage> kpkgs = loadKnowledgePackages(null, false, "../test_Dynamic1_0.drl");
    kpkgs = SerializationHelper.serializeObject(kpkgs);
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
    kBase.addPackages(kpkgs);
    kBase = SerializationHelper.serializeObject(kBase);
    try {
        // set only the deserialisation properties, but not the serialization
        setPublicKeyProperties();
        kBase = SerializationHelper.serializeObject(kBase);
        fail("Should not deserialize an unsigned rulebase on an environment configured to work with signed rulebases.");
    } catch (Exception e) {
    // success
    } finally {
        unsetPublicKeyProperties();
    }
}
Also used : KiePackage(org.kie.api.definition.KiePackage) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

KiePackage (org.kie.api.definition.KiePackage)124 Test (org.junit.Test)93 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)80 KieSession (org.kie.api.runtime.KieSession)70 ArrayList (java.util.ArrayList)44 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)39 List (java.util.List)25 Cheese (org.drools.compiler.Cheese)22 Person (org.drools.compiler.Person)18 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)17 InternalFactHandle (org.drools.core.common.InternalFactHandle)15 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)11 EntryPoint (org.kie.api.runtime.rule.EntryPoint)10 Resource (org.kie.api.io.Resource)9 StringReader (java.io.StringReader)8 HashMap (java.util.HashMap)8 FactHandle (org.kie.api.runtime.rule.FactHandle)8 Map (java.util.Map)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)7 KieBase (org.kie.api.KieBase)7