Search in sources :

Example 11 with KnowledgeBuilderConfiguration

use of org.kie.internal.builder.KnowledgeBuilderConfiguration in project drools by kiegroup.

the class Misc2Test method testDynamicSalienceUpdate.

@Test
public void testDynamicSalienceUpdate() {
    String drl = "package org.drools.compiler.tests; \n" + "" + "import java.util.*; \n" + "" + "global List list; \n" + "" + "declare Foo value : long end \n" + "" + "rule Nop \n" + " salience( $l ) \n" + "when\n" + "  Foo( $l : value ) \n" + "then\n" + "  System.out.println( \"Never Foo \" + $l ); " + "  list.add( $l ); \n" + "end\n" + "" + "rule Insert \n" + " salience 100 \n" + "when \n" + "  $l : Long() \n" + "then \n" + "  System.out.println( \"Insert Foo \" + $l ); " + "  insertLogical( new Foo( $l ) ); \n" + "end \n" + "" + "rule Clean \n" + " salience 50 \n" + "when \n" + "  $s : String() \n" + "  $l : Long() \n" + "then \n" + "  System.out.println( \"delete \" + $l ); " + "  delete( $l ); \n" + "end \n";
    KnowledgeBuilderConfiguration kbConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    KieBase kbase = loadKnowledgeBaseFromString(kbConf, drl);
    KieSession ksession = kbase.newKieSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(1L);
    ksession.insert(2L);
    ksession.insert("go");
    ksession.fireAllRules();
    assertTrue(list.isEmpty());
    ksession.dispose();
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 12 with KnowledgeBuilderConfiguration

use of org.kie.internal.builder.KnowledgeBuilderConfiguration in project drools by kiegroup.

the class VerifierKnowledgeBaseBuilder method newVerifierKnowledgeBase.

public KieBase newVerifierKnowledgeBase(VerifierConfiguration configuration) {
    InternalKnowledgeBase verifierKnowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
    KnowledgeBuilderConfiguration kbuilderConfiguration = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    kbuilderConfiguration.setProperty("drools.dialect.java.compiler", "JANINO");
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kbuilderConfiguration);
    if (configuration.getVerifyingResources() != null) {
        for (Resource resource : configuration.getVerifyingResources().keySet()) {
            kbuilder.add(resource, configuration.getVerifyingResources().get(resource));
        }
    }
    if (kbuilder.hasErrors()) {
        Iterator<KnowledgeBuilderError> errors = kbuilder.getErrors().iterator();
        while (errors.hasNext()) {
            this.errors.add(new VerifierError("Error compiling verifier rules: " + errors.next().getMessage()));
        }
    }
    verifierKnowledgeBase.addPackages(kbuilder.getKnowledgePackages());
    return verifierKnowledgeBase;
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) VerifierError(org.drools.verifier.VerifierError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Resource(org.kie.api.io.Resource) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 13 with KnowledgeBuilderConfiguration

use of org.kie.internal.builder.KnowledgeBuilderConfiguration 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)

Example 14 with KnowledgeBuilderConfiguration

use of org.kie.internal.builder.KnowledgeBuilderConfiguration in project drools by kiegroup.

the class FailureOnRemovalTest method createKnowledgeBuilderConfiguration.

private KnowledgeBuilderConfiguration createKnowledgeBuilderConfiguration() {
    Properties properties = new Properties();
    properties.setProperty("drools.dialect.java.compiler", "JANINO");
    KnowledgeBuilderConfiguration kconf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(properties, getClass().getClassLoader());
    kconf.setOption(DefaultDialectOption.get("java"));
    return kconf;
}
Also used : KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) Properties(java.util.Properties)

Example 15 with KnowledgeBuilderConfiguration

use of org.kie.internal.builder.KnowledgeBuilderConfiguration in project drools by kiegroup.

the class DynamicRulesTest method testIsolatedClassLoaderWithEnumsPkgBuilder.

@Test(timeout = 10000)
public void testIsolatedClassLoaderWithEnumsPkgBuilder() throws Exception {
    try {
        // Creates first class loader and use it to load fact classes
        ClassLoader loader1 = new SubvertedClassLoader(new URL[] { getClass().getResource("/testEnum.jar") }, this.getClass().getClassLoader());
        loader1.loadClass("org.drools.Primitives");
        loader1.loadClass("org.drools.TestEnum");
        // create a builder with the given classloader
        KnowledgeBuilderConfiguration conf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader1);
        Collection<KiePackage> kpkgs = loadKnowledgePackages(conf, "test_EnumSerialization.drl");
        // serialize out
        byte[] out = DroolsStreamUtils.streamOut(kpkgs);
        // adding original packages to a kbase just to make sure they are fine
        KieBaseConfiguration kbaseConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(null, loader1);
        InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase(kbaseConf);
        kbase.addPackages(kpkgs);
        KieSession ksession = createKnowledgeSession(kbase);
        List list = new ArrayList();
        ksession.setGlobal("list", list);
        assertEquals(1, ksession.fireAllRules());
        assertEquals(1, list.size());
        // now, create another classloader and make sure it has access to the classes
        ClassLoader loader2 = new SubvertedClassLoader(new URL[] { getClass().getResource("/testEnum.jar") }, this.getClass().getClassLoader());
        loader2.loadClass("org.drools.Primitives");
        loader2.loadClass("org.drools.TestEnum");
        ClassLoader ccl = Thread.currentThread().getContextClassLoader();
        Collection<KiePackage> kpkgs2 = null;
        try {
            Thread.currentThread().setContextClassLoader(loader2);
            kpkgs2 = (Collection<KiePackage>) DroolsStreamUtils.streamIn(out);
        } finally {
            Thread.currentThread().setContextClassLoader(ccl);
        }
        // create another kbase
        KieBaseConfiguration kbaseConf2 = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(null, loader2);
        InternalKnowledgeBase kbase2 = (InternalKnowledgeBase) getKnowledgeBase(kbaseConf2);
        kbase2.addPackages(kpkgs2);
        ksession = createKnowledgeSession(kbase2);
        list = new ArrayList();
        ksession.setGlobal("list", list);
        assertEquals(1, ksession.fireAllRules());
        assertEquals(1, list.size());
    } catch (ClassCastException cce) {
        cce.printStackTrace();
        fail("No ClassCastException should be raised.");
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) URLClassLoader(java.net.URLClassLoader) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

KnowledgeBuilderConfiguration (org.kie.internal.builder.KnowledgeBuilderConfiguration)25 Test (org.junit.Test)21 KieSession (org.kie.api.runtime.KieSession)16 KieBase (org.kie.api.KieBase)15 ArrayList (java.util.ArrayList)10 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)7 List (java.util.List)6 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)6 Arrays.asList (java.util.Arrays.asList)4 URLClassLoader (java.net.URLClassLoader)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 KiePackage (org.kie.api.definition.KiePackage)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 JarEntry (java.util.jar.JarEntry)2 JarInputStream (java.util.jar.JarInputStream)2 MapBackedClassLoader (org.drools.core.rule.MapBackedClassLoader)2 FactType (org.kie.api.definition.type.FactType)2 Resource (org.kie.api.io.Resource)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1