Search in sources :

Example 16 with DroolsObjectInputStream

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

the class MarshallingTest method testAccumulateSerialization.

@Test
public void testAccumulateSerialization() throws Exception {
    KieBase kbase = loadKnowledgeBase("org/drools/compiler/integrationtests/marshalling/test_SerializableAccumulate.drl");
    KieSession ksession = kbase.newKieSession();
    ksession.setGlobal("results", new ArrayList());
    Cheese t1 = new Cheese("brie", 10);
    Cheese t2 = new Cheese("brie", 15);
    Cheese t3 = new Cheese("stilton", 20);
    Cheese t4 = new Cheese("brie", 30);
    ksession.insert(t1);
    ksession.insert(t2);
    ksession.insert(t3);
    ksession.insert(t4);
    // ksession.fireAllRules();
    Marshaller marshaller = createSerializableMarshaller(kbase);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new DroolsObjectOutputStream(baos);
    out.writeObject(kbase);
    marshaller.marshall(out, ksession);
    out.flush();
    out.close();
    ObjectInputStream in = new DroolsObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    kbase = (InternalKnowledgeBase) in.readObject();
    marshaller = createSerializableMarshaller(kbase);
    ksession = (StatefulKnowledgeSession) marshaller.unmarshall(in);
    in.close();
    // setting the global again, since it is not serialized with the session
    List<List> results = (List<List>) new ArrayList<List>();
    ksession.setGlobal("results", results);
    assertNotNull(results);
    ksession.fireAllRules();
    ksession.dispose();
    assertEquals(1, results.size());
    assertEquals(3, results.get(0).size());
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) Marshaller(org.kie.api.marshalling.Marshaller) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DroolsObjectOutputStream(org.drools.core.common.DroolsObjectOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) DroolsObjectOutputStream(org.drools.core.common.DroolsObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) Test(org.junit.Test)

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