Search in sources :

Example 1 with DroolsObjectInputStream

use of org.drools.core.common.DroolsObjectInputStream in project drools by kiegroup.

the class PersisterHelper method loadStrategiesIndex.

private static void loadStrategiesIndex(MarshallerReaderContext context, ProtobufMessages.Header _header) throws IOException, ClassNotFoundException {
    for (ProtobufMessages.Header.StrategyIndex _entry : _header.getStrategyList()) {
        ObjectMarshallingStrategy strategyObject = context.resolverStrategyFactory.getStrategyObject(_entry.getName());
        if (strategyObject == null) {
            throw new IllegalStateException("No strategy of type " + _entry.getName() + " available.");
        }
        context.usedStrategies.put(_entry.getId(), strategyObject);
        Context ctx = strategyObject.createContext();
        context.strategyContexts.put(strategyObject, ctx);
        if (_entry.hasData() && ctx != null) {
            ClassLoader classLoader = null;
            if (context.classLoader != null) {
                classLoader = context.classLoader;
            } else if (context.kBase != null) {
                classLoader = context.kBase.getRootClassLoader();
            }
            if (classLoader instanceof ProjectClassLoader) {
                readRuntimeDefinedClasses(_header, (ProjectClassLoader) classLoader);
            }
            ctx.read(new DroolsObjectInputStream(_entry.getData().newInput(), classLoader));
        }
    }
}
Also used : Context(org.kie.api.marshalling.ObjectMarshallingStrategy.Context) ProjectClassLoader(org.drools.core.common.ProjectClassLoader) DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) Header(org.drools.core.marshalling.impl.ProtobufMessages.Header) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) ProjectClassLoader(org.drools.core.common.ProjectClassLoader)

Example 2 with DroolsObjectInputStream

use of org.drools.core.common.DroolsObjectInputStream in project drools by kiegroup.

the class MarshallingIssuesTest method testJBRULES_1946_2.

@Test
public void testJBRULES_1946_2() {
    KieBase kbase = loadKnowledgeBase("../Sample.drl");
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DroolsObjectOutputStream oos = new DroolsObjectOutputStream(baos);
        oos.writeObject(kbase);
        oos.flush();
        oos.close();
        baos.flush();
        baos.close();
        byte[] serializedKb = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(serializedKb);
        DroolsObjectInputStream ois = new DroolsObjectInputStream(bais);
        KieBase kb2 = (KieBase) ois.readObject();
    } catch (OptionalDataException ode) {
        ode.printStackTrace();
        fail("EOF? " + ode.eof);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Unexpected exception: " + e.getMessage());
    }
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) KieBase(org.kie.api.KieBase) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OptionalDataException(java.io.OptionalDataException) DroolsObjectOutputStream(org.drools.core.common.DroolsObjectOutputStream) OptionalDataException(java.io.OptionalDataException) Test(org.junit.Test)

Example 3 with DroolsObjectInputStream

use of org.drools.core.common.DroolsObjectInputStream 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 4 with DroolsObjectInputStream

use of org.drools.core.common.DroolsObjectInputStream in project drools by kiegroup.

the class EventAccessorRestoreTest method loadSession.

public KieSession loadSession(FileInputStream input) throws IOException, ClassNotFoundException {
    KieSession ksession = null;
    DroolsObjectInputStream droolsIn = new DroolsObjectInputStream(input, this.getClass().getClassLoader());
    try {
        KieBase kbase = (KieBase) droolsIn.readObject();
        Marshaller mas = createMarshaller(kbase);
        ksession = mas.unmarshall(droolsIn);
    } catch (EOFException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        droolsIn.close();
    }
    return ksession;
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) Marshaller(org.kie.api.marshalling.Marshaller) KieBase(org.kie.api.KieBase) EOFException(java.io.EOFException) KieSession(org.kie.api.runtime.KieSession)

Example 5 with DroolsObjectInputStream

use of org.drools.core.common.DroolsObjectInputStream in project drools by kiegroup.

the class JPAPlaceholderResolverStrategy method unmarshal.

public Object unmarshal(Context context, ObjectInputStream ois, byte[] object, ClassLoader classloader) throws IOException, ClassNotFoundException {
    ClassLoader clToUse = classloader;
    if (this.classLoader != null) {
        clToUse = this.classLoader;
    }
    DroolsObjectInputStream is = new DroolsObjectInputStream(new ByteArrayInputStream(object), clToUse);
    String canonicalName = is.readUTF();
    Object id = is.readObject();
    EntityManager em = getEntityManager();
    return em.find(Class.forName(canonicalName, true, (clToUse == null ? this.getClass().getClassLoader() : clToUse)), id);
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) EntityManager(javax.persistence.EntityManager) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

DroolsObjectInputStream (org.drools.core.common.DroolsObjectInputStream)16 ByteArrayInputStream (java.io.ByteArrayInputStream)12 DroolsObjectOutputStream (org.drools.core.common.DroolsObjectOutputStream)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 Test (org.junit.Test)6 KieBase (org.kie.api.KieBase)6 ObjectInputStream (java.io.ObjectInputStream)5 KieSession (org.kie.api.runtime.KieSession)5 ObjectOutputStream (java.io.ObjectOutputStream)4 ArrayList (java.util.ArrayList)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 KiePackage (org.kie.api.definition.KiePackage)3 Marshaller (org.kie.api.marshalling.Marshaller)3 IOException (java.io.IOException)2 Collection (java.util.Collection)2 EOFException (java.io.EOFException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 ObjectInput (java.io.ObjectInput)1