Search in sources :

Example 1 with MapBackedClassLoader

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);
}
Also used : JarInputStream(java.util.jar.JarInputStream) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MapBackedClassLoader(org.drools.core.rule.MapBackedClassLoader) JarEntry(java.util.jar.JarEntry) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 2 with MapBackedClassLoader

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());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) JarInputStream(java.util.jar.JarInputStream) ArrayList(java.util.ArrayList) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MapBackedClassLoader(org.drools.core.rule.MapBackedClassLoader) JarEntry(java.util.jar.JarEntry) Test(org.junit.Test)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 JarEntry (java.util.jar.JarEntry)2 JarInputStream (java.util.jar.JarInputStream)2 MapBackedClassLoader (org.drools.core.rule.MapBackedClassLoader)2 Test (org.junit.Test)2 KnowledgeBuilderConfiguration (org.kie.internal.builder.KnowledgeBuilderConfiguration)2 ArrayList (java.util.ArrayList)1 KiePackage (org.kie.api.definition.KiePackage)1 EntryPoint (org.kie.api.runtime.rule.EntryPoint)1 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)1