use of org.kie.api.marshalling.Marshaller in project drools by kiegroup.
the class MarshallingTest method testDroolsObjectOutputInputStream.
/**
* test that creates a new knowledge base, new stateful session, inserts new
* fact, serializes the knowledge base and session and fact using one output
* stream then deserializes and verifies that fact in the session is the
* same as fact that was deserialized,
*
* from the ObjectOutputStream API: "Multiple references to a single object
* are encoded using a reference sharing mechanism so that graphs of objects
* can be restored to the same shape as when the original was written."
*
* This is still not fixed, as mentioned in the JIRA
*
* JBRULES-2048
*
* @throws Exception
*/
@Test
@Ignore
public void testDroolsObjectOutputInputStream() throws Exception {
KieBase kbase = loadKnowledgeBase("org/drools/compiler/integrationtests/test_Serializable.drl");
KieSession session = kbase.newKieSession();
Person bob = new Person();
session.insert(bob);
assertSame("these two object references should be same", bob, session.getObjects().iterator().next());
Marshaller marshaller = createSerializableMarshaller(kbase);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new DroolsObjectOutputStream(baos);
out.writeObject(bob);
out.writeObject(kbase);
marshaller.marshall(out, session);
out.flush();
out.close();
ObjectInputStream in = new DroolsObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
Person deserializedBob = (Person) in.readObject();
kbase = (InternalKnowledgeBase) in.readObject();
marshaller = createSerializableMarshaller(kbase);
session = (StatefulKnowledgeSession) marshaller.unmarshall(in);
assertSame("these two object references should be same", deserializedBob, session.getObjects().iterator().next());
in.close();
}
use of org.kie.api.marshalling.Marshaller in project drools by kiegroup.
the class StatefulKnowledgeSessionImpl method writeExternal.
public void writeExternal(ObjectOutput out) throws IOException {
// all we do is create marshall to a byte[] and write to the stream
StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) getKnowledgeRuntime();
Marshaller marshaller = MarshallerFactory.newMarshaller(ksession.getKieBase(), new ObjectMarshallingStrategy[] { MarshallerFactory.newSerializeMarshallingStrategy() });
ByteArrayOutputStream stream = new ByteArrayOutputStream();
marshaller.marshall(stream, (StatefulKnowledgeSession) getKnowledgeRuntime());
stream.close();
byte[] bytes = stream.toByteArray();
out.writeInt(bytes.length);
out.write(bytes);
}
use of org.kie.api.marshalling.Marshaller 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());
}
use of org.kie.api.marshalling.Marshaller in project drools by kiegroup.
the class MarshallingTest method createSerializableMarshaller.
private Marshaller createSerializableMarshaller(KieBase knowledgeBase) {
ObjectMarshallingStrategyAcceptor acceptor = MarshallerFactory.newClassFilterAcceptor(new String[] { "*.*" });
ObjectMarshallingStrategy strategy = MarshallerFactory.newSerializeMarshallingStrategy(acceptor);
Marshaller marshaller = MarshallerFactory.newMarshaller(knowledgeBase, new ObjectMarshallingStrategy[] { strategy });
return marshaller;
}
use of org.kie.api.marshalling.Marshaller in project drools by kiegroup.
the class MarshallingTest method marshallAndUnmarshall.
public static KieSession marshallAndUnmarshall(KieBase kbase1, KieBase kbase2, KieSession ksession, KieSessionConfiguration sessionConfig) {
// Serialize and Deserialize
try {
KieMarshallers kieMarshallers = KieServices.Factory.get().getMarshallers();
Marshaller marshaller = kieMarshallers.newMarshaller(kbase1);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
marshaller.marshall(baos, ksession);
marshaller = kieMarshallers.newMarshaller(kbase2);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
baos.close();
ksession = marshaller.unmarshall(bais, sessionConfig, null);
bais.close();
} catch (Exception e) {
e.printStackTrace();
fail("unexpected exception :" + e.getMessage());
}
return ksession;
}
Aggregations