public static StatefulKnowledgeSession deserializeKnowledgeSession(Marshaller marshaller, byte [] serializedKsession) throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream( serializedKsession ); StatefulKnowledgeSession deserializedKsession = (StatefulKnowledgeSession) marshaller.unmarshall( bais, SessionConfiguration.newInstance(), EnvironmentFactory.newEnvironment() ); bais.close(); return deserializedKsession; }
StatefulKnowledgeSession session2 = (StatefulKnowledgeSession) marshaller.unmarshall( bais );
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 ); } }
public static KieSession marshallAndUnmarshall(KieServices ks, KieBase kbase, KieSession ksession, KieSessionConfiguration sessionConfig) { // Serialize and Deserialize try { Marshaller marshaller = ks.getMarshallers().newMarshaller(kbase); ByteArrayOutputStream baos = new ByteArrayOutputStream(); marshaller.marshall(baos, ksession); marshaller = MarshallerFactory.newMarshaller(kbase); 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; }
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; }
@Test public void testMarshallWithCollects() throws Exception { // BZ-1193600 String str = "import java.util.Collection\n" + "rule R1 when\n" + " Collection(empty==false) from collect( Integer() )\n" + " Collection() from collect( String() )\n" + "then\n" + "end\n" + "rule R2 when then end\n"; KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build(); KieSession ksession = kbase.newKieSession(); try { Marshaller marshaller = MarshallerFactory.newMarshaller(kbase); ByteArrayOutputStream baos = new ByteArrayOutputStream(); marshaller.marshall(baos, ksession); marshaller = MarshallerFactory.newMarshaller(kbase); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); baos.close(); ksession = marshaller.unmarshall(bais); bais.close(); } catch (Exception e) { e.printStackTrace(); fail("unexpected exception :" + e.getMessage()); } }
public KieSession loadSession( FileInputStream input ) throws IOException, ClassNotFoundException { KieSession ksession = null; DroolsObjectInputStream droolsIn = new DroolsObjectInputStream( input, this.getClass().getClassLoader() ); try { KieBase kbase = (KieBase) droolsIn.readObject(); Marshaller mas = createMarshaller( kbase ); ksession = mas.unmarshall(droolsIn); } catch ( EOFException e ) { e.printStackTrace(); fail( e.getMessage() ); } finally { droolsIn.close(); } return ksession; }
kbase = (InternalKnowledgeBase) in.readObject(); marshaller = createSerializableMarshaller( kbase ); session = (StatefulKnowledgeSession) marshaller.unmarshall( in );
ksession = MarshallerFactory.newMarshaller( knowledgeBase ).unmarshall( bais ); } catch ( Throwable t ) { t.printStackTrace();
private KieSession marsallStatefulKnowledgeSession(KieSession ksession) throws IOException, ClassNotFoundException { Globals globals = ksession.getGlobals(); KieBase kbase = ksession.getKieBase(); ByteArrayOutputStream out = new ByteArrayOutputStream(); MarshallerFactory.newMarshaller( kbase ).marshall( out, ksession ); KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); ksconf.setOption( TimerJobFactoryOption.get("trackable") ); ksconf.setOption( ClockTypeOption.get( "pseudo" ) ); Environment env = EnvironmentFactory.newEnvironment(); env.set( EnvironmentName.GLOBALS, globals ); ksession = MarshallerFactory.newMarshaller( kbase ).unmarshall( new ByteArrayInputStream( out.toByteArray() ), ksconf, env ); return ksession; }
kbase = (InternalKnowledgeBase) in.readObject(); marshaller = createSerializableMarshaller( kbase ); ksession = (StatefulKnowledgeSession) marshaller.unmarshall( in ); in.close();
ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() ); baos.close(); ksession = (StatefulKnowledgeSession) marshaller.unmarshall( bais ); bais.close(); } catch (Exception e) {
@Test public void testMarshallWithTimer() throws Exception { // DROOLS-2210 String drl = "declare String @role(event) end\n" + "\n" + "rule R1 when\n" + " $s : String( ) over window:time( 5s )\n" + " then\n" + " delete( $s );\n" + "end\n"; KieBase kBase = initializeKnowledgeBase( drl ); KieSession ksession = kBase.newKieSession(); ksession.insert( "test" ); assertEquals( 1, ksession.fireAllRules() ); Marshaller marshaller = KieServices.get().getMarshallers().newMarshaller(kBase); ByteArrayOutputStream baos = new ByteArrayOutputStream(); marshaller.marshall( baos, ksession ); ksession.dispose(); ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() ); ksession = marshaller.unmarshall( bais ); assertEquals( 0, ksession.fireAllRules() ); } }
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); } }
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); } }
/** * 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); } }
private void readWrite(KnowledgeBase 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, KnowledgeBaseFactory.newEnvironment() ); ksession.fireAllRules(); //scheduler = ksession.<SessionClock>getSessionClock(); } catch ( Exception e ) { throw new RuntimeException( e ); } }
private KieSession marsallStatefulKnowledgeSession(KieSession ksession) throws IOException, ClassNotFoundException { Globals globals = ksession.getGlobals(); KieBase kbase = ksession.getKieBase(); ByteArrayOutputStream out = new ByteArrayOutputStream(); MarshallerFactory.newMarshaller( kbase ).marshall( out, ksession ); KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(); ksconf.setOption( TimerJobFactoryOption.get("trackable") ); ksconf.setOption( ClockTypeOption.get( "pseudo" ) ); Environment env = EnvironmentFactory.newEnvironment(); env.set( EnvironmentName.GLOBALS, globals ); ksession = MarshallerFactory.newMarshaller( kbase ).unmarshall( new ByteArrayInputStream( out.toByteArray() ), ksconf, env ); return ksession; }
kbase = (KnowledgeBase) in.readObject(); marshaller = createSerializableMarshaller( kbase ); ksession = (StatefulKnowledgeSession) marshaller.unmarshall( in ); in.close();
@Test public void testKieMarshaller() throws Exception { final KieServices ks = KieServices.Factory.get(); final ReleaseId v100 = ks.newReleaseId("org.kie", "dmn-test", "1.0.0"); KieHelper.createAndDeployJar(ks, v100, wrapWithDroolsModelResource(ks, ks.getResources().newClassPathResource("0001-input-data-string.dmn", this.getClass()))); KieContainer kieContainer = ks.newKieContainer(v100); KieSession kieSession = kieContainer.newKieSession(); KieBase kieBase = kieSession.getKieBase(); DMNRuntime runtime = kieSession.getKieRuntime(DMNRuntime.class); Assert.assertNotNull(runtime); assertThat(runtime.getModels(), hasSize(1)); check0001_input_data_string(runtime); KieMarshallers kieMarshallers = ks.getMarshallers(); Marshaller marshaller = kieMarshallers.newMarshaller(kieBase); ByteArrayOutputStream baos = new ByteArrayOutputStream(); marshaller.marshall(baos, kieSession); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); baos.close(); kieSession = marshaller.unmarshall(bais); bais.close(); runtime = kieSession.getKieRuntime(DMNRuntime.class); Assert.assertNotNull(runtime); assertThat(runtime.getModels(), hasSize(1)); check0001_input_data_string(runtime); }