Search in sources :

Example 16 with KnowledgeBuilderConfiguration

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

the class DynamicRulesTest method testClassLoaderSwitchsUsingConf.

@Test(timeout = 10000)
public void testClassLoaderSwitchsUsingConf() throws Exception {
    try {
        // Creates first class loader and use it to load fact classes
        ClassLoader loader1 = new SubvertedClassLoader(new URL[] { getClass().getResource("/") }, this.getClass().getClassLoader());
        Class cheeseClass = loader1.loadClass("org.drools.compiler.Cheese");
        KnowledgeBuilderConfiguration kbuilderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader1);
        KieBase kbase = loadKnowledgeBase(kbuilderConf, "test_Dynamic1.drl");
        KieSession wm = createKnowledgeSession(kbase);
        wm.insert(cheeseClass.newInstance());
        wm.fireAllRules();
        // Creates second class loader and use it to load fact classes
        ClassLoader loader2 = new SubvertedClassLoader(new URL[] { getClass().getResource("/") }, this.getClass().getClassLoader());
        cheeseClass = loader2.loadClass("org.drools.compiler.Cheese");
        kbuilderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader2);
        kbase = loadKnowledgeBase(kbuilderConf, "test_Dynamic1.drl");
        wm = createKnowledgeSession(kbase);
        wm.insert(cheeseClass.newInstance());
        wm.fireAllRules();
    } catch (ClassCastException cce) {
        cce.printStackTrace();
        fail("No ClassCastException should be raised.");
    }
}
Also used : KieBase(org.kie.api.KieBase) URLClassLoader(java.net.URLClassLoader) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 17 with KnowledgeBuilderConfiguration

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

the class ClassLoaderTest method testClassLoaderHits.

@Test
public void testClassLoaderHits() throws Exception {
    final KnowledgeBuilderConfiguration conf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    // conf.setOption( ClassLoaderCacheOption.DISABLED );
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(conf);
    kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("test_GeneratedBeansMVEL.drl")), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("test_GeneratedBeans.drl")), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("test_NullFieldOnCompositeSink.drl")), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
// ((CompositeClassLoader)((PackageBuilderConfiguration)conf).getClassLoader()).dumpStats();
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) Test(org.junit.Test)

Example 18 with KnowledgeBuilderConfiguration

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

the class CustomOperatorTest method testCustomOperatorCombiningConstraints.

@Test
public void testCustomOperatorCombiningConstraints() {
    // JBRULES-3517
    String str = "declare GN\n" + "   gNo : Double\n" + "end\n" + "\n" + "declare t547147\n" + "   c547148 : String\n" + "   c547149 : String\n" + "end\n" + "\n" + "declare Tra48\n" + "   gNo : Double\n" + "   postCode : String\n" + "   name : String\n" + "   cnt : String\n" + "end\n" + "\n" + "rule \"r548695.1\"\n" + "no-loop true\n" + "dialect \"mvel\"\n" + "when\n" + "   gnId : GN()\n" + "   la : t547147( )\n" + "   v1717 : Tra48( gnId.gNo == gNo, name F_str[startsWith] la.c547148 || postCode F_str[contains] la.c547149 )\n" + "then\n" + "   System.out.println(\"Rule r548695.1 fired\");\n" + "end\n";
    KnowledgeBuilderConfiguration builderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    builderConf.setOption(EvaluatorOption.get("str", new F_StrEvaluatorDefinition()));
    KieBase kbase = loadKnowledgeBaseFromString(builderConf, str);
}
Also used : KieBase(org.kie.api.KieBase) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) Test(org.junit.Test)

Example 19 with KnowledgeBuilderConfiguration

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

the class CustomOperatorTest method testCustomOperatorUsingCollections.

@Test
public void testCustomOperatorUsingCollections() {
    String str = "import org.drools.compiler.Person\n" + "import org.drools.compiler.Address\n" + "rule R when\n" + "    $alice : Person(name == \"Alice\")\n" + "    $bob : Person(name == \"Bob\", addresses supersetOf $alice.addresses)\n" + "then\n" + "end\n";
    KnowledgeBuilderConfiguration builderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    builderConf.setOption(EvaluatorOption.get("supersetOf", new SupersetOfEvaluatorDefinition()));
    KieBase kbase = loadKnowledgeBaseFromString(builderConf, str);
    KieSession ksession = kbase.newKieSession();
    Person alice = new Person("Alice", 30);
    alice.addAddress(new Address("Large Street", "BigTown", "12345"));
    Person bob = new Person("Bob", 30);
    bob.addAddress(new Address("Large Street", "BigTown", "12345"));
    bob.addAddress(new Address("Long Street", "SmallTown", "54321"));
    ksession.insert(alice);
    ksession.insert(bob);
    assertEquals(1, ksession.fireAllRules());
}
Also used : Address(org.drools.compiler.Address) KieBase(org.kie.api.KieBase) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 20 with KnowledgeBuilderConfiguration

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

the class PropertySpecificTest method testWrongUasgeOfWatchAnnotationOnNonPropertySpecificClass.

@Test
public void testWrongUasgeOfWatchAnnotationOnNonPropertySpecificClass() throws Exception {
    String rule = "package org.drools.compiler.integrationtests\n" + "dialect \"mvel\"\n" + "declare A\n" + "    s : String\n" + "end\n" + "declare B\n" + "    on : boolean\n" + "    s : String\n" + "end\n" + "rule R1\n" + "when\n" + "    A($s : s)\n" + "    $b : B(s != $s) @watch( !s, on )\n" + "then\n" + "    modify($b) { setS($s) }\n" + "end\n" + "rule R2\n" + "when\n" + "    $b : B(on == false)\n" + "then\n" + "    modify($b) { setOn(true) }\n" + "end\n";
    // The compilation of the above ^ would turn error under the assumption Property Reactivity is NOT enabled by default.
    KnowledgeBuilderConfiguration conf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    conf.setOption(PropertySpecificOption.ALLOWED);
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(conf);
    kbuilder.add(ResourceFactory.newByteArrayResource(rule.getBytes()), ResourceType.DRL);
    assertTrue(kbuilder.hasErrors());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) 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