use of org.kie.api.builder.KieBuilder in project drools by kiegroup.
the class ClassLoaderTest method testClassLoaderFromPojo.
@Test
public void testClassLoaderFromPojo() throws IOException {
// DROOLS-1108
final String source = "package org.drools.testdrl;\n" + "public class MyPojo {\n" + " public String getUrlPath() {" + " return getClass().getClassLoader().getResource(\"META-INF/foo.xml\").getPath();\n" + " }\n" + "}\n";
final String drl1 = "package org.drools.testdrl;\n" + "import org.drools.testdrl.MyPojo;\n" + "global java.util.List list;\n" + "rule R1 when\n" + "then\n" + " insert(new MyPojo());\n" + "end\n" + "rule R2 when\n" + " $m : MyPojo()\n" + "then\n" + " list.add($m.getUrlPath());\n" + "end\n";
final String foo = "<xyz/>\n";
final KieServices ks = KieServices.Factory.get();
final ReleaseId releaseId = ks.newReleaseId("org.kie", "test-cl", "1.0.0");
final KieFileSystem kfs = ks.newKieFileSystem();
final KieModuleModel kproj = ks.newKieModuleModel();
final KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("testKbase").setDefault(true).addPackage("org.drools.testdrl");
final KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("testKsession").setDefault(true);
kfs.writeKModuleXML(kproj.toXML());
kfs.write("src/main/resources/org/drools/testdrl/rules1.drl", drl1);
kfs.write("src/main/java/org/drools/testdrl/MyPojo.java", source);
kfs.write("src/main/resources/META-INF/foo.xml", foo);
kfs.generateAndWritePomXML(releaseId);
final KieBuilder kieBuilder = ks.newKieBuilder(kfs);
assertTrue(kieBuilder.buildAll().getResults().getMessages().isEmpty());
final KieContainer kieContainer = ks.newKieContainer(releaseId);
final ClassLoader classLoader = kieContainer.getClassLoader();
final URL url = classLoader.getResource("META-INF/foo.xml");
assertNotNull(url);
final KieSession ksession = kieContainer.newKieSession("testKsession");
final List<URL> list = new ArrayList<>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals(url.getPath(), list.get(0));
ksession.dispose();
}
use of org.kie.api.builder.KieBuilder in project drools by kiegroup.
the class TypeDeclarationTest method testOutOfRangePositions.
@Test
public void testOutOfRangePositions() throws InstantiationException, IllegalAccessException {
// DROOLS-559
String drl = "package org.test;\n" + "global java.util.List names;\n" + "declare Person\n" + " name : String @position(3)\n" + " age : int @position(1)\n" + "end\n" + "rule R when \n" + " $p : Person( 37, \"Mark\"; )\n" + "then\n" + " names.add( $p.getName() );\n" + "end\n";
KieBuilder kieBuilder = build(drl);
assertTrue(kieBuilder.getResults().hasMessages(Message.Level.ERROR));
}
use of org.kie.api.builder.KieBuilder in project drools by kiegroup.
the class TypeDeclarationTest method testDuplicatedPositions.
@Test
public void testDuplicatedPositions() throws InstantiationException, IllegalAccessException {
// DROOLS-559
String drl = "package org.test;\n" + "global java.util.List names;\n" + "declare Person\n" + " name : String @position(1)\n" + " age : int @position(1)\n" + "end\n" + "rule R when \n" + " $p : Person( 37, \"Mark\"; )\n" + "then\n" + " names.add( $p.getName() );\n" + "end\n";
KieBuilder kieBuilder = build(drl);
assertTrue(kieBuilder.getResults().hasMessages(Message.Level.ERROR));
}
use of org.kie.api.builder.KieBuilder in project drools by kiegroup.
the class TypeDeclarationTest method testPreventReflectionAPIsOnJavaClasses.
@Test(expected = UnsupportedOperationException.class)
public void testPreventReflectionAPIsOnJavaClasses() {
String drl = "package org.test; " + // existing java class
"declare org.drools.compiler.Person " + " @role(event) " + "end \n" + "";
KieBuilder kieBuilder = build(drl);
assertFalse(kieBuilder.getResults().hasMessages(Message.Level.ERROR));
KieBase kieBase = KieServices.Factory.get().newKieContainer(kieBuilder.getKieModule().getReleaseId()).getKieBase();
FactType type = kieBase.getFactType("org.drools.compiler", "Person");
}
use of org.kie.api.builder.KieBuilder in project drools by kiegroup.
the class DroolsAbstractPMMLTest method readKnowledgeBase.
private static KieBase readKnowledgeBase(List<InputStream> theory) {
KieServices ks = KieServices.Factory.get();
KieRepository kr = ks.getRepository();
KieFileSystem kfs = ks.newKieFileSystem();
for (int j = 0; j < theory.size(); j++) {
Resource res = ks.getResources().newInputStreamResource(theory.get(j));
kfs.write(RESOURCE_PATH + "source_" + j + ".drl", res);
}
KieModuleModel model = ks.newKieModuleModel();
KieBaseModel kbModel = model.newKieBaseModel(DEFAULT_KIEBASE).setDefault(true).addPackage(BASE_PACK).setEventProcessingMode(EventProcessingOption.STREAM);
kfs.writeKModuleXML(model.toXML());
KieBuilder kb = ks.newKieBuilder(kfs);
kb.buildAll();
if (kb.getResults().hasMessages(Message.Level.ERROR)) {
throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
}
KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
return kContainer.getKieBase();
}
Aggregations