[rules-users] Drools and serialize

Swindells, Thomas TSwindells at nds.com
Tue Jul 12 09:05:46 EDT 2011


You don't need the object output stream below - and it's very bad code to give access to the same output stream to two parents (the marshaller and the OOS in this case) as you can end up just writing garbage if they interleave their writes - you don't know the buffering policy that each parent is using.

Thomas

> -----Original Message-----
> From: rules-users-bounces at lists.jboss.org [mailto:rules-users-
> bounces at lists.jboss.org] On Behalf Of clement.pernot
> Sent: 12 July 2011 13:53
> To: rules-users at lists.jboss.org
> Subject: Re: [rules-users] Drools and serialize
>
> Thank you for all your tips.
>
> I think I have found the solution. (the project run) Now for test that, I try to
> make a project who can un-serialize this.
>
> There it is:
>
> package com.sample;
>
> import java.io.FileOutputStream;
> import java.io.ObjectOutputStream;
> import java.io.OutputStream;
> 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;
>
>
> /**
>  * This is a sample class to launch a rule.
>  */
> 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 {
>                       // load up the knowledge base
>                       KnowledgeBase kbase = readKnowledgeBase();
>                       StatefulKnowledgeSession ksession =
> kbase.newStatefulKnowledgeSession();
>
>                       // go !
>                       Message message = new Message();
>                       message.setMessage("Hello World");
>                       message.setStatus(Message.HELLO);
>
>                       ksession.insert(message);
>
>                       ObjectMarshallingStrategy oms =
> MarshallerFactory.newIdentityMarshallingStrategy();
>               Marshaller marshaller = MarshallerFactory.newMarshaller(
> ksession.getKnowledgeBase(), new ObjectMarshallingStrategy[]{ oms } );
>
>                   OutputStream os = new FileOutputStream( rbPath );
>                   ObjectOutputStream oos = new ObjectOutputStream( os );
>                       marshaller.marshall( os, ksession );
>
>               oos.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;
>               }
>
>       }
>
> }
>
> --
> View this message in context: http://drools.46999.n3.nabble.com/Drools-
> and-serialize-tp3161882p3162316.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


**************************************************************************************
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