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