Search in sources :

Example 46 with KieBuilder

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();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) URL(java.net.URL) KieSessionModel(org.kie.api.builder.model.KieSessionModel) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 47 with KieBuilder

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));
}
Also used : KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 48 with KieBuilder

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));
}
Also used : KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 49 with KieBuilder

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");
}
Also used : KieBase(org.kie.api.KieBase) KieBuilder(org.kie.api.builder.KieBuilder) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 50 with KieBuilder

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();
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieRepository(org.kie.api.builder.KieRepository) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer)

Aggregations

KieBuilder (org.kie.api.builder.KieBuilder)211 KieFileSystem (org.kie.api.builder.KieFileSystem)179 KieServices (org.kie.api.KieServices)145 Test (org.junit.Test)134 KieSession (org.kie.api.runtime.KieSession)59 KieModuleModel (org.kie.api.builder.model.KieModuleModel)57 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)54 KieContainer (org.kie.api.runtime.KieContainer)48 IncrementalResults (org.kie.internal.builder.IncrementalResults)35 ReleaseId (org.kie.api.builder.ReleaseId)34 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)33 Results (org.kie.api.builder.Results)32 Message (org.kie.api.builder.Message)30 KieBase (org.kie.api.KieBase)29 ArrayList (java.util.ArrayList)22 KieBaseModel (org.kie.api.builder.model.KieBaseModel)22 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)21 Resource (org.kie.api.io.Resource)19 FactType (org.kie.api.definition.type.FactType)16 KieModule (org.kie.api.builder.KieModule)12