Search in sources :

Example 11 with Marshaller

use of org.kie.api.marshalling.Marshaller in project drools by kiegroup.

the class EventDeserializationInPastTest method marshallAndUnmarshall.

private KieSession marshallAndUnmarshall(final KieServices ks, final KieBase kbase, KieSession ksession, final KieSessionConfiguration sessionConfig) {
    // Serialize and Deserialize
    Marshaller marshaller = ks.getMarshallers().newMarshaller(kbase);
    try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        marshaller.marshall(baos, ksession);
        try (final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) {
            marshaller = MarshallerFactory.newMarshaller(kbase);
            ksession = marshaller.unmarshall(bais, sessionConfig, null);
        }
    } catch (Exception e) {
        Assertions.fail("Unexpected exception: ", e);
    }
    return ksession;
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 12 with Marshaller

use of org.kie.api.marshalling.Marshaller in project drools by kiegroup.

the class LogicalInsertionsSerializationTest method testSerializeAndDeserializeSession.

@Test
public void testSerializeAndDeserializeSession() throws Exception {
    KieSession ksession = session.getStateful();
    File tempFile = File.createTempFile(name.getMethodName(), null);
    ksession.fireAllRules();
    try (OutputStream fos = new FileOutputStream(tempFile)) {
        Marshaller marshaller = getServices().getMarshallers().newMarshaller(getKbase());
        marshaller.marshall(fos, ksession);
    }
    try (InputStream fis = new FileInputStream(tempFile)) {
        Marshaller marshaller = getServices().getMarshallers().newMarshaller(getKbase());
        marshaller.unmarshall(fis, ksession);
    }
    ksession.insert(new Promotion("Claire", "Scientist"));
    int firedRules = ksession.fireAllRules();
    Assertions.assertThat(firedRules).isEqualTo(1);
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) KieSession(org.kie.api.runtime.KieSession) Promotion(org.drools.testcoverage.common.model.Promotion) KieSessionTest(org.drools.testcoverage.common.KieSessionTest) Test(org.junit.Test)

Example 13 with Marshaller

use of org.kie.api.marshalling.Marshaller in project opennms by OpenNMS.

the class DroolsCorrelationEngine method marshallStateToDisk.

private void marshallStateToDisk(boolean serialize) {
    final File stateFile = getPathToState().toFile();
    LOG.debug("Saving state for engine {} in {} ...", m_name, stateFile);
    final KieMarshallers kMarshallers = KieServices.Factory.get().getMarshallers();
    final ObjectMarshallingStrategy oms = serialize ? kMarshallers.newSerializeMarshallingStrategy() : kMarshallers.newIdentityMarshallingStrategy();
    final Marshaller marshaller = kMarshallers.newMarshaller(m_kieBase, new ObjectMarshallingStrategy[] { oms });
    try (FileOutputStream fos = new FileOutputStream(stateFile)) {
        m_kieSession.halt();
        marshaller.marshall(fos, m_kieSession);
        m_kieSession.dispose();
        m_kieSession.destroy();
        LOG.info("Sucessfully save state for engine {} in {}.", m_name, stateFile);
    } catch (IOException e) {
        LOG.error("Failed to save state for engine {} in {}.", m_name, stateFile, e);
    }
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) KieMarshallers(org.kie.api.marshalling.KieMarshallers)

Example 14 with Marshaller

use of org.kie.api.marshalling.Marshaller in project opennms by OpenNMS.

the class DroolsCorrelationEngine method unmarshallStateFromDisk.

private void unmarshallStateFromDisk(boolean serialize) {
    final File stateFile = getPathToState().toFile();
    if (!stateFile.exists()) {
        LOG.error("Can't restore state from {} because the file doesn't exist", stateFile);
        return;
    }
    LOG.debug("Restoring state for engine {} from {} ...", m_name, stateFile);
    final KieMarshallers kMarshallers = KieServices.Factory.get().getMarshallers();
    final ObjectMarshallingStrategy oms = serialize ? kMarshallers.newSerializeMarshallingStrategy() : kMarshallers.newIdentityMarshallingStrategy();
    final Marshaller marshaller = kMarshallers.newMarshaller(m_kieBase, new ObjectMarshallingStrategy[] { oms });
    try (FileInputStream fin = new FileInputStream(stateFile)) {
        marshaller.unmarshall(fin, m_kieSession);
        stateFile.delete();
        LOG.info("Sucessfully restored state for engine {} from {}.", m_name, stateFile);
    } catch (IOException | ClassNotFoundException e) {
        LOG.error("Failed to restore state for engine {} from {}.", m_name, stateFile, e);
    }
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) KieMarshallers(org.kie.api.marshalling.KieMarshallers)

Example 15 with Marshaller

use of org.kie.api.marshalling.Marshaller in project opennms by OpenNMS.

the class DroolsNorthbounder method marshallStateToDisk.

/**
 * Marshall state to disk.
 *
 * @param serialize the serialize
 */
private void marshallStateToDisk(boolean serialize) {
    final File stateFile = getPathToState().toFile();
    LOG.debug("Saving state for engine {} in {} ...", getName(), stateFile);
    final KieMarshallers kMarshallers = KieServices.Factory.get().getMarshallers();
    final ObjectMarshallingStrategy oms = serialize ? kMarshallers.newSerializeMarshallingStrategy() : kMarshallers.newIdentityMarshallingStrategy();
    final Marshaller marshaller = kMarshallers.newMarshaller(m_kieBase, new ObjectMarshallingStrategy[] { oms });
    try (FileOutputStream fos = new FileOutputStream(stateFile)) {
        m_kieSession.halt();
        marshaller.marshall(fos, m_kieSession);
        m_kieSession.dispose();
        m_kieSession.destroy();
        LOG.info("Sucessfully save state for engine {} in {}. There are {} elements on the working memory.", getName(), stateFile, m_kieSession.getObjects().size());
    } catch (IOException e) {
        LOG.error("Failed to save state for engine {} in {}.", getName(), stateFile, e);
    }
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) 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