use of org.drools.core.rule.MapBackedClassLoader in project drools by kiegroup.
the class MarshallingTest method testSerializabilityWithJarFacts.
/**
* In this case we are dealing with facts which are not on the systems classpath.
*/
@Test
public void testSerializabilityWithJarFacts() throws Exception {
MapBackedClassLoader loader = new MapBackedClassLoader(this.getClass().getClassLoader());
JarInputStream jis = new JarInputStream(this.getClass().getResourceAsStream("/billasurf.jar"));
JarEntry entry = null;
byte[] buf = new byte[1024];
int len = 0;
while ((entry = jis.getNextJarEntry()) != null) {
if (!entry.isDirectory()) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((len = jis.read(buf)) >= 0) {
out.write(buf, 0, len);
}
loader.addResource(entry.getName(), out.toByteArray());
}
}
String drl = "package foo.bar \n" + "import com.billasurf.Board\n" + "rule 'MyGoodRule' \n dialect 'mvel' \n when " + " Board() " + "then \n" + " System.err.println(42); \n" + "end\n";
KnowledgeBuilderConfiguration kbuilderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader);
Collection<KiePackage> kpkgs = loadKnowledgePackagesFromString(kbuilderConf, drl);
kpkgs = SerializationHelper.serializeObject(kpkgs, loader);
}
use of org.drools.core.rule.MapBackedClassLoader in project drools by kiegroup.
the class JBRULESTest method testGUVNOR578_2.
@Test
public void testGUVNOR578_2() throws Exception {
final MapBackedClassLoader loader = new MapBackedClassLoader(this.getClass().getClassLoader());
final JarInputStream jis = new JarInputStream(this.getClass().getResourceAsStream("/primespoc.jar"));
JarEntry entry = null;
final byte[] buf = new byte[1024];
int len = 0;
while ((entry = jis.getNextJarEntry()) != null) {
if (!entry.isDirectory()) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((len = jis.read(buf)) >= 0) {
out.write(buf, 0, len);
}
loader.addResource(entry.getName(), out.toByteArray());
}
}
final List<JarInputStream> jarInputStreams = new ArrayList<JarInputStream>();
jarInputStreams.add(jis);
final KnowledgeBuilderConfiguration conf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader);
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(conf);
final String header = "import fr.gouv.agriculture.dag.agorha.business.primes.SousPeriodePrimeAgent\n";
kbuilder.add(ResourceFactory.newByteArrayResource(header.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.hasErrors());
final String passingRule = "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "SousPeriodePrimeAgent( echelle == \"abc\" )" + "then\n" + "end\n";
final String failingRule = "rule \"rule2\"\n" + "dialect \"mvel\"\n" + "when\n" + "SousPeriodePrimeAgent( quotiteRemuneration == 123 , echelle == \"abc\" )" + "then\n" + "end\n";
kbuilder.add(ResourceFactory.newByteArrayResource(passingRule.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.hasErrors());
kbuilder.add(ResourceFactory.newByteArrayResource(failingRule.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.hasErrors());
}
Aggregations