Search in sources :

Example 1 with KieMarshallers

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

the class DroolsNorthbounder method unmarshallStateFromDisk.

/**
 * Unmarshall state from disk.
 *
 * @param serialize the serialize
 */
private void unmarshallStateFromDisk(boolean serialize) {
    final File stateFile = getPathToState().toFile();
    LOG.debug("Restoring state for engine {} from {} ...", getName(), stateFile);
    if (!stateFile.exists())
        return;
    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 {}. There are {} elements on the working memory.", getName(), stateFile, m_kieSession.getObjects().size());
    } catch (IOException | ClassNotFoundException e) {
        LOG.error("Failed to restore state for engine {} from {}.", getName(), 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 2 with KieMarshallers

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

the class IncrementalCompilationTest method testAddRuleWithSlidingWindows.

@Test
public void testAddRuleWithSlidingWindows() throws Exception {
    // DROOLS-2292
    String drl1 = "package org.drools.compiler\n" + "import " + List.class.getCanonicalName() + "\n" + "import " + BooleanEvent.class.getCanonicalName() + "\n" + "rule R1 when\n" + "    $e : BooleanEvent(!enabled)\n" + "    List(size >= 1) from collect ( BooleanEvent(!enabled) over window:time(1) )\n" + "    $toEdit : List() from collect( BooleanEvent(!enabled) over window:time(2) )\n" + "then\n" + "    modify( (BooleanEvent)$toEdit.get(0) ){ setEnabled( true ) }\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("KSession1").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    deployJar(ks, createKJar(ks, kproj, releaseId1, null));
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "2.0.0");
    deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl1));
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieSession kieSession = kc.newKieSession();
    kieSession.insert(new BooleanEvent());
    kieSession.fireAllRules();
    kc.updateToVersion(releaseId2);
    kieSession.fireAllRules();
    KieMarshallers marshallers = ks.getMarshallers();
    Marshaller marshaller = marshallers.newMarshaller(kieSession.getKieBase());
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    marshaller.marshall(outputStream, kieSession);
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) Marshaller(org.kie.api.marshalling.Marshaller) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KieSessionModel(org.kie.api.builder.model.KieSessionModel) KieContainer(org.kie.api.runtime.KieContainer) KieMarshallers(org.kie.api.marshalling.KieMarshallers) Test(org.junit.Test)

Example 3 with KieMarshallers

use of org.kie.api.marshalling.KieMarshallers 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 4 with KieMarshallers

use of org.kie.api.marshalling.KieMarshallers 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 5 with KieMarshallers

use of org.kie.api.marshalling.KieMarshallers 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

KieMarshallers (org.kie.api.marshalling.KieMarshallers)6 Marshaller (org.kie.api.marshalling.Marshaller)6 IOException (java.io.IOException)5 File (java.io.File)4 ObjectMarshallingStrategy (org.kie.api.marshalling.ObjectMarshallingStrategy)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Test (org.junit.Test)1 KieServices (org.kie.api.KieServices)1 ReleaseId (org.kie.api.builder.ReleaseId)1 KieBaseModel (org.kie.api.builder.model.KieBaseModel)1 KieModuleModel (org.kie.api.builder.model.KieModuleModel)1 KieSessionModel (org.kie.api.builder.model.KieSessionModel)1 KieContainer (org.kie.api.runtime.KieContainer)1 KieSession (org.kie.api.runtime.KieSession)1 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)1