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 Variable marshallVariable(MarshallerWriteContext context, String name, Object value) throws IOException { JBPMMessages.Variable.Builder builder = JBPMMessages.Variable.newBuilder().setName( name ); if(value != null){ ObjectMarshallingStrategy strategy = context.objectMarshallingStrategyStore.getStrategyObject( value ); Integer index = context.getStrategyIndex( strategy ); builder.setStrategyIndex( index ) .setValue( ByteString.copyFrom( strategy.marshal( context.strategyContext.get( strategy ), context, value ) ) ); } return builder.build(); }
public static Object unmarshallVariableValue(MarshallerReaderContext context, JBPMMessages.Variable _variable) throws IOException, ClassNotFoundException { if(_variable.getValue() == null || _variable.getValue().isEmpty()){ return null; } ObjectMarshallingStrategy strategy = context.usedStrategies.get( _variable.getStrategyIndex() ); Object value = strategy.unmarshal( context.strategyContexts.get( strategy ), context, _variable.getValue().toByteArray(), (context.kBase == null)?null:context.kBase.getRootClassLoader() ); return value; }
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; }
marshaller.marshall( baos, session ); byte[] b1 = baos.toByteArray(); baos.close(); StatefulKnowledgeSession session2 = (StatefulKnowledgeSession) marshaller.unmarshall( bais );
public static void writeWorkItem_v1(MarshallerWriteContext context, WorkItem workItem) throws IOException { ObjectOutputStream stream = context.stream; stream.writeLong( workItem.getId() ); stream.writeLong( workItem.getProcessInstanceId() ); stream.writeUTF( workItem.getName() ); stream.writeInt( workItem.getState() ); //Work Item Parameters Map<String, Object> parameters = workItem.getParameters(); Collection<Object> notNullValues = new ArrayList<Object>(); for ( Object value : parameters.values() ) { if ( value != null ) { notNullValues.add( value ); } } stream.writeInt( notNullValues.size() ); for ( String key : parameters.keySet() ) { Object object = parameters.get( key ); if ( object != null ) { stream.writeUTF( key ); int index = context.objectMarshallingStrategyStore.getStrategy( object ); stream.writeInt( index ); ObjectMarshallingStrategy strategy = context.objectMarshallingStrategyStore.getStrategy( index ); if ( strategy.accept( object ) ) { strategy.write( stream, object ); } } } }
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; }
int index = objectMarshallingStrategyStore.getStrategy( object ); ObjectMarshallingStrategy strategy = objectMarshallingStrategyStore.getStrategy( index ); strategy.write( stream, object ); } else {
strategy = context.resolverStrategyFactory.getStrategy( index ); String strategyClassName = context.stream.readUTF(); if ( ! StringUtils.isEmpty(strategyClassName) ) { strategy = context.resolverStrategyFactory.getStrategyObject(strategyClassName); if( strategy == null ) { throw new IllegalStateException( "No strategy of type " + strategyClassName + " available." ); value = strategy.read( stream );
stream.writeInt(useNewMarshallingStrategyAlgorithm); ObjectMarshallingStrategy strategy = context.objectMarshallingStrategyStore.getStrategyObject(object); stream.writeUTF(strategy.getClass().getName()); strategy.write(stream, object);
continue; if (strategy.accept(variable.getValue())) { logger.debug("Strategy of type {} found to handle variable '{}'", strategy, variable.getKey()); try { context.setState(ProcessMarshallerWriteContext.STATE_COMPLETED); strategy.marshal(null, context, variable.getValue()); logger.debug("Variable '{}' successfully persisted by strategy {}", variable.getKey(), strategy); break;
(InternalWorkingMemory) ((StatefulKnowledgeSessionImpl) ksession), RuleBaseNodes.getNodeMap(((InternalKnowledgeBase) kbase)), marshallingConfig.getObjectMarshallingStrategyStore(), marshallingConfig.isMarshallProcessInstances(), marshallingConfig.isMarshallWorkItems(), ksession.getEnvironment()); ((KnowledgeBaseImpl) kbase), RuleBaseNodes.getNodeMap( ((KnowledgeBaseImpl) kbase)), marshallingConfig.getObjectMarshallingStrategyStore(), ProtobufMarshaller.TIMER_READERS, marshallingConfig.isMarshallProcessInstances(), marshallingConfig.isMarshallWorkItems() , EnvironmentFactory.newEnvironment()); readerContext.wm = ((StatefulKnowledgeSessionImpl) ksession).getInternalWorkingMemory();
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 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(); }
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; }
public static Variable marshallVariablesMap(MarshallerWriteContext context, Map<String, Object> variables) throws IOException{ Map<String, Variable> marshalledVariables = new HashMap<String, Variable>(); for(String key : variables.keySet()){ JBPMMessages.Variable.Builder builder = JBPMMessages.Variable.newBuilder().setName( key ); if(variables.get(key) != null){ ObjectMarshallingStrategy strategy = context.objectMarshallingStrategyStore.getStrategyObject( variables.get(key) ); Integer index = context.getStrategyIndex( strategy ); builder.setStrategyIndex( index ) .setValue( ByteString.copyFrom( strategy.marshal( context.strategyContext.get( strategy ), context, variables.get(key) ) ) ); } marshalledVariables.put(key, builder.build()); } return marshallVariable(context, "variablesMap" ,marshalledVariables); }
@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 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; }
public static VariableContainer marshallVariablesContainer(MarshallerWriteContext context, Map<String, Object> variables) throws IOException{ JBPMMessages.VariableContainer.Builder vcbuilder = JBPMMessages.VariableContainer.newBuilder(); for(String key : variables.keySet()){ JBPMMessages.Variable.Builder builder = JBPMMessages.Variable.newBuilder().setName( key ); if(variables.get(key) != null){ ObjectMarshallingStrategy strategy = context.objectMarshallingStrategyStore.getStrategyObject( variables.get(key) ); Integer index = context.getStrategyIndex( strategy ); builder.setStrategyIndex( index ) .setValue( ByteString.copyFrom( strategy.marshal( context.strategyContext.get( strategy ), context, variables.get(key) ) ) ); } vcbuilder.addVariable(builder.build()); } return vcbuilder.build(); }