我正在使用 drools 6.0.1.Final 使用 maven 创建一个 android 应用程序。
为了减少移动设备的工作量,我决定在我的桌面/服务器上编译规则,然后将编译后的规则打包到移动应用程序中。我的问题是,如果没有指向当前 CLASSPATH 的硬链接,我无法序列化 KieBase。
这是我生成的测试代码:
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();
然后是序列化器:
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;
}
在 pastebin 上获取序列化文件“compiledRuleFile”:
<iframe src="http://pastebin.com/embed_iframe.php?i=Lx3cbcaS" style="border:none;width:100%"></iframe>
输出文件有问题的部分:
\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
我怎样才能得到一个可以在任何地方反序列化的“compiledRuleFile”?
尝试从 github repo 运行 createrule.java 测试: https ://github.com/cyrillemortier/drools-serialize 打开控制台输出中提示的文件。
谢谢西里尔