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.");
}
}
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();
}
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);
}
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());
}
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());
}
Aggregations