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; }
marshaller.marshall( baos, session ); byte[] b1 = baos.toByteArray(); baos.close();
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 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(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()); } }
out.writeObject( bob ); out.writeObject( kbase ); marshaller.marshall( out, session ); out.flush();
@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 ); }
MarshallerFactory.newMarshaller( knowledgeBase ).marshall( baos, ksession );
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; }
ObjectOutputStream out = new DroolsObjectOutputStream( baos ); out.writeObject( kbase ); marshaller.marshall( out, ksession ); out.flush();
Marshaller marshaller = MarshallerFactory.newMarshaller( kbase ); ByteArrayOutputStream baos = new ByteArrayOutputStream(); marshaller.marshall( baos, ksession ); marshaller = MarshallerFactory.newMarshaller( kbase ); ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() );
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(); }
@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 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 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); } }
/** * 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 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 ); } }
@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 ); }
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; }