[rules-users] Drools and serialize

Swindells, Thomas TSwindells at nds.com
Tue Jul 12 07:23:37 EDT 2011


This looks wrong to me
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
Marshaller marshaller = MarshallerFactory.newMarshaller( ksession.getKnowledgeBase(), new ObjectMarshallingStrategy[]{ oms } );

                        marshaller.marshall( baos, ksession );

                        ObjectOutputStream out = new ObjectOutputStream( baos );
                    out.writeObject( marshaller );
                    out.close();

Presumably the marshaller is writing out the knowledge session to the output stream, then you are trying to use an object output stream to output the marshaller you just used.
I think in this case you need to use one or the other - either just write out the knowledgebase through the objectOutputStream, or write out the knowledge base with a custom marshaling strategy with the Marshaller, not both!

Thomas
> -----Original Message-----
> From: rules-users-bounces at lists.jboss.org [mailto:rules-users-
> bounces at lists.jboss.org] On Behalf Of Abhay B. Chaware
> Sent: 12 July 2011 11:46
> To: Rules Users List
> Subject: Re: [rules-users] Drools and serialize
>
> Haven't used this before, but looks like DefaultMarshaller is not serializable.
>
> What I've done in past is directly serializing and deserializing knowledgebase
> without using Marshaller. That worked.
>
> -abhay
>
> -----Original Message-----
> From: rules-users-bounces at lists.jboss.org [mailto:rules-users-
> bounces at lists.jboss.org] On Behalf Of clement.pernot
> Sent: Tuesday, July 12, 2011 4:07 PM
> To: rules-users at lists.jboss.org
> Subject: Re: [rules-users] Drools and serialize
>
> I have try what you say. This didn't work.
> here is my full code:
>
> package com.sample;
>
> import java.io.ByteArrayOutputStream;
> import java.io.ObjectOutputStream;
> import java.io.Serializable;
>
> import org.drools.KnowledgeBase;
> import org.drools.KnowledgeBaseFactory;
> import org.drools.builder.KnowledgeBuilder;
> import org.drools.builder.KnowledgeBuilderError;
> import org.drools.builder.KnowledgeBuilderErrors;
> import org.drools.builder.KnowledgeBuilderFactory;
> import org.drools.builder.ResourceType;
> import org.drools.io.ResourceFactory;
> import org.drools.marshalling.Marshaller;
> import org.drools.marshalling.MarshallerFactory;
> import org.drools.marshalling.ObjectMarshallingStrategy;
> import org.drools.runtime.StatefulKnowledgeSession;
>
> public class test{
>
>       private static void compileRuleBase2( String drlPath, String rbPath )
> throws Exception {
>               KnowledgeBuilder kBuilder =
> KnowledgeBuilderFactory.newKnowledgeBuilder();
>               kBuilder.add( ResourceFactory.newFileResource( drlPath ),
> ResourceType.DRL );
>               if( kBuilder.hasErrors() ){
>                   for( KnowledgeBuilderError err: kBuilder.getErrors() ){
>                       System.err.println( err.toString() );
>                   }
>                   throw new IllegalStateException( "DRL errors" );
>               }
>               KnowledgeBase kBase =
> KnowledgeBaseFactory.newKnowledgeBase();
>               kBase.addKnowledgePackages(
> kBuilder.getKnowledgePackages() );
>
>               try {
>                       KnowledgeBase kbase = readKnowledgeBase();
>                       StatefulKnowledgeSession ksession =
> kbase.newStatefulKnowledgeSession();
>
>                       Message message = new Message();
>                       message.setMessage("Hello World");
>                       message.setStatus(Message.HELLO);
>
>                       ksession.insert(message);
>
>                       ObjectMarshallingStrategy oms =
> MarshallerFactory.newIdentityMarshallingStrategy();
>               ByteArrayOutputStream baos = new ByteArrayOutputStream();
>                       Marshaller marshaller =
> MarshallerFactory.newMarshaller( ksession.getKnowledgeBase(), new
> ObjectMarshallingStrategy[]{ oms } );
>
>                       marshaller.marshall( baos, ksession );
>
>                       ObjectOutputStream out = new
> ObjectOutputStream( baos );
>                   out.writeObject( marshaller );
>                   out.close();
>
>                       baos.close();
>                       ksession.fireAllRules();
>
>               } catch (Throwable t) {
>                       t.printStackTrace();
>               }
>           }
>
>       public static final void main(String[] args) {
>               try {
>                       compileRuleBase2("Sample.drl",
> "drools_compiled_file_with_fact" );
>               } catch (Exception e) {
>                       e.printStackTrace();
>               }
>       }
>
>       private static KnowledgeBase readKnowledgeBase() throws
> Exception {
>               KnowledgeBuilder kbuilder =
> KnowledgeBuilderFactory.newKnowledgeBuilder();
>
>       kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"),
> ResourceType.DRL);
>               KnowledgeBuilderErrors errors = kbuilder.getErrors();
>               if (errors.size() > 0) {
>                       for (KnowledgeBuilderError error: errors) {
>                               System.err.println(error);
>                       }
>                       throw new IllegalArgumentException("Could not
> parse knowledge.");
>               }
>               KnowledgeBase kbase =
> KnowledgeBaseFactory.newKnowledgeBase();
>
>       kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
>               return kbase;
>       }
>
>       public static class Message implements Serializable{
>
>               public static final int HELLO = 0;
>               public static final int GOODBYE = 1;
>               private String message;
>               private int status;
>
>               public String getMessage() {
>                       return this.message;
>               }
>               public void setMessage(String message) {
>                       this.message = message;
>               }
>               public int getStatus() {
>                       return this.status;
>               }
>               public void setStatus(int status) {
>                       this.status = status;
>               }
>       }
> }
>
>
>
> here is my error:
>
> java.io.NotSerializableException:
> org.drools.marshalling.impl.DefaultMarshaller
>       at
> java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
>       at
> java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
>       at com.sample.test.compileRuleBase2(test.java:59)
>       at com.sample.test.main(test.java:73)
>
>
> Thank you for your reply, I think I am near the solution
>
> --
> View this message in context: http://drools.46999.n3.nabble.com/Drools-
> and-serialize-tp3161882p3162038.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users


**************************************************************************************
This message is confidential and intended only for the addressee. If you have received this message in error, please immediately notify the postmaster at nds.com and delete it from your system as well as any copies. The content of e-mails as well as traffic data may be monitored by NDS for employment and security purposes. To protect the environment please do not print this e-mail unless necessary.

NDS Limited. Registered Office: One London Road, Staines, Middlesex, TW18 4EX, United Kingdom. A company registered in England and Wales. Registered no. 3080780. VAT no. GB 603 8808 40-00
**************************************************************************************




More information about the rules-users mailing list