There are several different ways of doing it, but here is a snippet to
help creating an in-memory kjar with default kbases and ksessions:
public static byte[] createKJar(KieServices ks,
ReleaseId releaseId,
String pom,
String... drls) {
KieFileSystem kfs = ks.newKieFileSystem();
if( pom != null ) {
kfs.write("pom.xml", pom);
} else {
kfs.generateAndWritePomXML(releaseId);
}
KieResources kr = KieServices.getResources();
for (int i = 0; i < drls.length; i++) {
if (drls[i] != null) {
kfs.write( kr.newByteArrayResource( drls[i].getBytes()
).setSourcePath("my/pkg/drl"+i+".drl") );
}
}
KieBuilder kb = ks.newKieBuilder(kfs).buildAll();
if( kb.getResults().hasMessages(
org.kie.api.builder.Message.Level.ERROR ) ) {
for( org.kie.api.builder.Message result :
kb.getResults().getMessages() ) {
System.out.println(result.getText());
}
return null;
}
InternalKieModule kieModule = (InternalKieModule) ks.getRepository()
.getKieModule(releaseId);
byte[] jar = kieModule.getBytes();
return jar;
}
Please note that you usually don't need the byte[] back, so you can
ignore everything after the last 3 lines of the code. Also, the error check
in the snippet is just printing to sysout. You should handle this
accordingly in your application.
Hope it helps.
Edson
On Tue, Dec 3, 2013 at 3:25 AM, pmander <paul.s.mander(a)gmail.com> wrote:
I head previously read that but didn't spot the bit that mentions
creating
from a drl defined as a String. I take it this can be done from
KieFileSystem.
String rules = ...
KieFileSystem kfs = kieServices.newKieFileSystem();
kfs.write(kieServices.getResources().newReaderResource(new
StringReader(rules)));
This throws an exception complaining that the resource doesn't have a
source
or target path set.
Is there a way to push a dynamically created drl into this without writing
it to disk first?
--
View this message in context:
http://drools.46999.n3.nabble.com/drools-6-equivalent-of-addKnowledgePack...
Sent from the Drools: User forum mailing list archive at
Nabble.com.
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users
--
Edson Tirelli
Principal Software Engineer
Red Hat Business Systems and Intelligence Group