Search in sources :

Example 1 with Marshaller

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

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

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

the class MarshallingTest method readWrite.

private void readWrite(KieBase knowledgeBase, KieSession ksession, KieSessionConfiguration config) {
    try {
        Marshaller marshaller = MarshallerFactory.newMarshaller(knowledgeBase);
        ByteArrayOutputStream o = new ByteArrayOutputStream();
        marshaller.marshall(o, ksession);
        ksession = marshaller.unmarshall(new ByteArrayInputStream(o.toByteArray()), config, KieServices.get().newEnvironment());
        ksession.fireAllRules();
    // scheduler = ksession.<SessionClock>getSessionClock();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 4 with Marshaller

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

the class MarshallingTest method testScheduledActivation.

@Test
@Ignore("This test is suspicious to say the least...")
public void testScheduledActivation() {
    KnowledgeBaseImpl knowledgeBase = (KnowledgeBaseImpl) KnowledgeBaseFactory.newKnowledgeBase();
    KnowledgePackageImpl impl = new KnowledgePackageImpl("test");
    BuildContext buildContext = new BuildContext(knowledgeBase);
    // simple rule that fires after 10 seconds
    final RuleImpl rule = new RuleImpl("test-rule");
    new RuleTerminalNode(1, new MockTupleSource(2), rule, rule.getLhs(), 0, buildContext);
    final List<String> fired = new ArrayList<String>();
    rule.setConsequence(new Consequence() {

        public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
            fired.add("a");
        }

        public String getName() {
            return "default";
        }
    });
    rule.setTimer(new DurationTimer(10000));
    rule.setPackage("test");
    impl.addRule(rule);
    knowledgeBase.addPackages(Collections.singleton(impl));
    SessionConfiguration config = SessionConfiguration.newInstance();
    config.setClockType(ClockType.PSEUDO_CLOCK);
    KieSession ksession = knowledgeBase.newKieSession(config, KieServices.get().newEnvironment());
    PseudoClockScheduler scheduler = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    Marshaller marshaller = MarshallerFactory.newMarshaller(knowledgeBase);
    ksession.insert("cheese");
    assertTrue(fired.isEmpty());
    // marshall, then unmarshall session
    readWrite(knowledgeBase, ksession, config);
    // the activations should fire after 10 seconds
    assertTrue(fired.isEmpty());
    scheduler.advanceTime(12, TimeUnit.SECONDS);
    assertFalse(fired.isEmpty());
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) WorkingMemory(org.drools.core.WorkingMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) Consequence(org.drools.core.spi.Consequence) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) IOException(java.io.IOException) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) DurationTimer(org.drools.core.time.impl.DurationTimer) MockTupleSource(org.drools.core.reteoo.MockTupleSource) BuildContext(org.drools.core.reteoo.builder.BuildContext) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) KieSession(org.kie.api.runtime.KieSession) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with Marshaller

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

the class EventAccessorRestoreTest method saveSession.

public void saveSession(FileOutputStream output, KieSession ksession) throws IOException {
    DroolsObjectOutputStream droolsOut = new DroolsObjectOutputStream(output);
    droolsOut.writeObject(ksession.getKieBase());
    Marshaller mas = createMarshaller(ksession.getKieBase());
    mas.marshall(droolsOut, ksession);
    droolsOut.flush();
    droolsOut.close();
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) DroolsObjectOutputStream(org.drools.core.common.DroolsObjectOutputStream)

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