Search in sources :

Example 16 with Marshaller

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();
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) Marshaller(org.kie.api.marshalling.Marshaller) ByteArrayInputStream(java.io.ByteArrayInputStream) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DroolsObjectOutputStream(org.drools.core.common.DroolsObjectOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Person(org.drools.compiler.Person) DroolsObjectOutputStream(org.drools.core.common.DroolsObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 17 with Marshaller

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);
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 18 with Marshaller

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

Example 19 with Marshaller

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;
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) ClassObjectMarshallingStrategyAcceptor(org.drools.core.marshalling.impl.ClassObjectMarshallingStrategyAcceptor) ObjectMarshallingStrategyAcceptor(org.kie.api.marshalling.ObjectMarshallingStrategyAcceptor)

Example 20 with 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;
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) KieMarshallers(org.kie.api.marshalling.KieMarshallers)

Aggregations

Marshaller (org.kie.api.marshalling.Marshaller)21 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 IOException (java.io.IOException)9 Test (org.junit.Test)9 KieSession (org.kie.api.runtime.KieSession)9 KieBase (org.kie.api.KieBase)6 KieMarshallers (org.kie.api.marshalling.KieMarshallers)6 ObjectMarshallingStrategy (org.kie.api.marshalling.ObjectMarshallingStrategy)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 DroolsObjectInputStream (org.drools.core.common.DroolsObjectInputStream)3 DroolsObjectOutputStream (org.drools.core.common.DroolsObjectOutputStream)3 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Ignore (org.junit.Ignore)2 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)2 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)2