I'm working with drools 6.0.1.Final using maven to create an android app.
To reduce the workload of the mobile i've decided to compile the rules on my destop/server and then package the compiled rules in the mobile app. My problem is that i'm unable to serialize the KieBase without hard writting links to the current CLASSPATH.
here is my generating test code :
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
File rule = new File(getClass().getClassLoader()
.getResource("HelloWorld.drl").getPath());
kfs.write( "src/main/resources/HelloWorld.drl",
ks.getResources().newInputStreamResource( rule.toURI().toURL().openStream() ) );
KieBuilder kieBuilder = ks.newKieBuilder( kfs ).buildAll();
Results results = kieBuilder.getResults();
if( results.hasMessages( Message.Level.ERROR ) ){
for (Message message : results.getMessages()) {
log.error( message.getText() );
}
throw new IllegalStateException( "### errors ###" );
}
KieContainer kieContainer =
ks.newKieContainer( ks.getRepository().getDefaultReleaseId() );
// CEP - get the KIE related configuration container and set the EventProcessing (from default cloud) to Stream
KieBaseConfiguration config = ks.newKieBaseConfiguration();
config.setOption( EventProcessingOption.STREAM );
KieBase kieBase = kieContainer.newKieBase( config );
File compiledRuleFile = new File(getClass().getClassLoader()
.getResource("compiledRules").getPath());
log.debug(compiledRuleFile.getCanonicalPath());
//----- Serialization to file "compiledRuleFile" --------------
Serialization.serialize(kieBase, compiledRuleFile);
//----- Deserialization to another object on another computer --------------
kieBase = Serialization.deserialize(KieBase.class, compiledRuleFile);
KieSession kieSession = kieBase.newKieSession();
then the serializer :
public static <T> void serialize(T o, File outputFile)
throws FileNotFoundException, IOException {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
outputFile));
out.writeObject(o);
out.close();
}
public static <T> T deserialize(Class<T> klass, File inputFile)
throws StreamCorruptedException, FileNotFoundException,
IOException, ClassNotFoundException {
T t = null;
ObjectInputStream in = new ObjectInputStream(new FileInputStream(
inputFile));
t = klass.cast(in.readObject());
in.close();
return t;
}
to get the serialized file "compiledRuleFile" on pastebin:
<iframe src="http://pastebin.com/embed_iframe.php?i=Lx3cbcaS" style="border:none;width:100%"></iframe>
the problematic section of the output file:
\sources\sandbox\trunk\gs-maven-android-master\initial\target\test-classes;G:\sources\sandbox\trunk\gs-maven-android-master\initial\target\classes;C:\Users\AccountName\.m2\repository\com\google\android\android\4.1.1.4\android-4.1.1.4.jar;C:\Users\AccountName\.m2\repository\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;C:\Users\AccountName\.m2\repository\org\apache\httpcomponents\httpclient\4.0.1\httpclient-4.0.1.jar;C:\Users\AccountName\.m2\repository\org\apache\httpcomponents\httpcore\4.0.1\httpcore-4.0.1.jar;C:\Users\AccountName\.m2\repository\commons-codec\commons-codec\1.3\commons-codec-1.3.jar;C:\Users\AccountName\.m2\repository\org\khronos\opengl-api\gl1.1-android-2.1_r1\opengl-api-gl1.1-android-2.1_r1.jar;C:\Users\AccountName\.m2\repository\xerces\xmlParserAPIs\2.6.2\xmlParserAPIs-2.6.2.jar;C:\Users\AccountName\.m2\repository\xpp3\xpp3\1.1.4c\xpp3-1.1.4c.jar;C:\Users\AccountName\.m2\repository\org\json\json\20080701\json-20080701.jar;C:\Users\AccountName\.m2\repository\org\projectlombok\lombok\1.14.8\lombok-1.14.8.jar;C:\Users\AccountName\.m2\repository\junit\junit\4.11\junit-4.11.jar;C:\Users\AccountName\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\AccountName\.m2\repository\org\assertj\assertj-core\1.7.0\assertj-core-1.7.0.jar;C:\Users\AccountName\.m2\repository\org\drools\drools-compiler\6.0.1.Final\drools-compiler-6.0.1.Final.jar;C:\Users\AccountName\.m2\repository\org\drools\drools-core\6.0.1.Final\drools-core-6.0.1.Final.jar;C:\Users\AccountName\.m2\repository\org\kie\kie-internal\6.0.1.Final\kie-internal-6.0.1.Final.jar;C:\Users\AccountName\.m2\repository\org\antlr\antlr-runtime\3.5\antlr-runtime-3.5.jar;C:\Users\AccountName\.m2\repository\org\eclipse\jdt\core\compiler\ecj\3.7.2\ecj-3.7.2.jar;C:\Users\AccountName\.m2\repository\com\thoughtworks\xstream\xstream\1.4.3\xstream-1.4.3.jar;C:\Users\AccountName\.m2\repository\xmlpull\xmlpull\1.1.3.1\xmlpull-1.1.3.1.jar;C:\Users\AccountName\.m2\repository\xpp3\xpp3_min\1.1.4c\xpp3_min-1.1.4c.jar;C:\Users\AccountName\.m2\repository\com\google\protobuf\protobuf-java\2.5.0\protobuf-java-2.5.0.jar;C:\Users\AccountName\.m2\repository\ch\qos\logback\logback-classic\1.1.2\logback-classic-1.1.2.jar;C:\Users\AccountName\.m2\repository\ch\qos\logback\logback-core\1.1.2\logback-core-1.1.2.jar;C:\Users\AccountName\.m2\repository\org\kie\kie-internal-android\6.0.1.Final\kie-internal-android-6.0.1.Final.jar;C:\Users\AccountName\.m2\repository\org\kie\kie-api\6.0.1.Final\kie-api-6.0.1.Final.jar;C:\Users\AccountName\.m2\repository\org\slf4j\slf4j-api\1.7.2\slf4j-api-1.7.2.jar;C:\Users\AccountName\.m2\repository\org\drools\drools-core-android\6.0.1.Final\drools-core-android-6.0.1.Final.jar;C:\Users\AccountName\.m2\repository\com\android\dex\1.7\dex-1.7.jar;C:\Users\AccountName\.m2\repository\android\android\4.4.2_r3\android-4.4.2_r3.jar;C:\Users\AccountName\.m2\repository\org\mvel\mvel2\2.1.8.Final\mvel2-2.1.8.Final.jar;C:\Users\AccountName\.m2\repository\com\google\openbeans\1.0\openbeans-1.0.jar;C:\Users\AccountName\.m2\repository\org\slf4j\slf4j-android\1.7.10\slf4j-android-1.7.10.jar;C:\Users\AccountName\.m2\repository\junit\junit\3.8.1\junit-3.8.1.jar;C:\Users\AccountName\.m2\repository\log4j\log4j\1.2.12\log4j-1.2.12.jar;C:\Users\AccountName\.m2\repository\logkit\logkit\1.0.1\logkit-1.0.1.jar;C:\Users\AccountName\.m2\repository\avalon-framework\avalon-framework\4.1.3\avalon-framework-4.1.3.jar;C:\Users\AccountName\.m2\repository\javax\servlet\servlet-api\2.3\servlet-api-2.3.jar;C:\Users\AccountName\.m2\repository\junit\junit\3.8.2\junit-3.8.2.jar;G:\sources\sandbox\trunk\gs-maven-android-master\initial\bin\classes;/G:/eclipse - juno/configuration/org.eclipse.osgi/205/0/.cp/;/G:/eclipse - juno/configuration/org.eclipse.osgi/204/0/.cp/t java.vm.specification.versiont 1.8t sun.arch.data.modelt 64t java.homet G:\jdk1.8.0_25\jret sun.java.commandt g.eclipse.jdt.internal.junit.runner.RemoteTestRunner -version 3 -port 57613 -testLoaderClass org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader -loaderpluginname org.eclipse.jdt.junit4.runtime -classNames org.hello.SHouldDoSmtht java.specification.vendort Oracle Corporationt user.languaget frt awt.toolkitt sun.awt.windows.WToolkitt java.vm.infot mixed modet java.versiont 1.8.0_25t java.ext.dirst 6G:\jdk1.8.0_25\jre\lib\ext;C:\windows\Sun\Java\lib\extt sun.boot.class.pathtG:\jdk1.8.0_25\jre\lib\resources.jar;G:\jdk1.8.0_25\jre\lib\rt.jar;G:\jdk1.8.0_25\jre\lib\sunrsasign.jar;G:\jdk1.8.0_25\jre\lib\jsse.jar;G:\jdk1.8.0_25\jre\lib\jce.jar;G:\jdk1.8.0_25\jre\lib\charsets.jar;G:\jdk1.8.0_25\jre\lib\jfr.jar;G:\jdk1.8.0_25\jre\classest java.vendort Oracle Corporationt file.separatort \t java.vendor.url.bugt #http://bugreport.sun.com/bugreport/t sun.cpu.endiant littlet sun.io.unicode.encodingt
how could i get a "compiledRuleFile" that can be deserialized everywhere ?
try to run the createrule.java test from the github repo : https://github.com/cyrillemortier/drools-serialize open the file prompted in the console output.
Thank's Cyrille