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());
}
Aggregations