marshaller.marshall( baos, session ); byte[] b1 = baos.toByteArray(); baos.close(); StatefulKnowledgeSession session2 = (StatefulKnowledgeSession) marshaller.unmarshall( bais );
public static byte [] serializeKnowledgeSession(Marshaller marshaller, StatefulKnowledgeSession ksession) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); marshaller.marshall( bos, ksession ); byte[] ksessionBytes = bos.toByteArray(); bos.close(); return ksessionBytes; }
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; }
env ); MarshallingConfigurationImpl config = (MarshallingConfigurationImpl) this.marshallingHelper.getMarshaller().getMarshallingConfiguration(); config.setMarshallProcessInstances( false ); config.setMarshallWorkItems( false );
@Test public void testMarshallWithNot() throws Exception { String str = "import " + getClass().getCanonicalName() + ".*\n" + "rule one\n" + "when\n" + " A()\n" + " not(B())\n" + "then\n" + "System.out.println(\"a\");\n" + "end\n" + "\n" + "rule two\n" + "when\n" + " A()\n" + "then\n" + "System.out.println(\"b\");\n" + "end\n"; KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); config.setOption( EventProcessingOption.STREAM ); KieBase kBase = loadKnowledgeBaseFromString(config, str); KieSession ksession = kBase.newKieSession(); ksession.insert( new A() ); MarshallerFactory.newMarshaller( kBase ).marshall( new ByteArrayOutputStream(), ksession ); }
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; }
protected void initNewKnowledgeSession(KieBase kbase, KieSessionConfiguration conf) { this.sessionInfo = new SessionInfo(); // create session but bypass command service this.ksession = kbase.newKieSession( conf, this.env ); initKieSessionMBeans(this.ksession); this.marshallingHelper = new SessionMarshallingHelper( this.ksession, conf ); MarshallingConfigurationImpl config = (MarshallingConfigurationImpl) this.marshallingHelper.getMarshaller().getMarshallingConfiguration(); config.setMarshallProcessInstances( false ); config.setMarshallWorkItems( false ); this.sessionInfo.setJPASessionMashallingHelper( this.marshallingHelper ); ((InternalKnowledgeRuntime) this.ksession).setEndOperationListener( new EndOperationListenerImpl(this.txm, this.sessionInfo ) ); this.runner = new TransactionInterceptor(); TimerJobFactoryManager timerJobFactoryManager = ((InternalKnowledgeRuntime) ksession ).getTimerService().getTimerJobFactoryManager(); if (timerJobFactoryManager instanceof CommandServiceTimerJobFactoryManager) { ( (CommandServiceTimerJobFactoryManager) timerJobFactoryManager ).setRunner( this ); } }
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 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(); }
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); } }
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; }
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); } }
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); } }
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; }
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); } }
/** * 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); } }
@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()); } }
/** * 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); } }
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; }
@Test public void testMarshallWithNot() throws Exception { String str = "import " + getClass().getCanonicalName() + ".*\n" + "rule one\n" + "when\n" + " A()\n" + " not(B())\n" + "then\n" + "System.out.println(\"a\");\n" + "end\n" + "\n" + "rule two\n" + "when\n" + " A()\n" + "then\n" + "System.out.println(\"b\");\n" + "end\n"; KieBaseConfiguration config = KnowledgeBaseFactory .newKnowledgeBaseConfiguration(); config.setOption( EventProcessingOption.STREAM ); KnowledgeBase kBase = loadKnowledgeBaseFromString(config, str); StatefulKnowledgeSession ksession = kBase.newStatefulKnowledgeSession(); ksession.insert( new A() ); MarshallerFactory.newMarshaller( kBase ).marshall( new ByteArrayOutputStream(), ksession ); }