Search in sources :

Example 21 with KieBuilder

use of org.kie.api.builder.KieBuilder in project drools by kiegroup.

the class KieBuilderTest method testMultipleKBaseWithDrlError.

@Test
public void testMultipleKBaseWithDrlError() {
    // RHBRMS-2651
    final String drl = "package org.drools.compiler;\n" + "rule \"test\"\n" + "  when\n" + "    Smurf\n" + "  then\n" + "end";
    final KieServices ks = KieServices.Factory.get();
    final KieModuleModel kproj = ks.newKieModuleModel();
    kproj.newKieBaseModel("kbase1").newKieSessionModel("ksession1").setDefault(true);
    kproj.newKieBaseModel("kbase2").newKieSessionModel("ksession2");
    final ReleaseId releaseId = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
    final KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).writeKModuleXML(kproj.toXML());
    final Resource drlResource = ResourceFactory.newByteArrayResource(drl.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
    kfs.write("src/main/resources/org/drools/compiler/drl1.drl", drlResource);
    final KieBuilder kb = ks.newKieBuilder(kfs).buildAll();
    final List<org.kie.api.builder.Message> messages = kb.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR);
    assertEquals(4, messages.size());
    assertTrue(messages.get(0).toString().contains("kbase1"));
    assertTrue(messages.get(1).toString().contains("kbase1"));
    assertTrue(messages.get(2).toString().contains("kbase2"));
    assertTrue(messages.get(3).toString().contains("kbase2"));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) KieModuleModel(org.kie.api.builder.model.KieModuleModel) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 22 with KieBuilder

use of org.kie.api.builder.KieBuilder in project drools by kiegroup.

the class KieDefaultPackageTest method testModelInDefaultPackage2.

@Test
public void testModelInDefaultPackage2() throws Exception {
    // This Model will be in the "default package"
    String model_drl = "" + "declare Smurf\n" + "Field1 : String\n" + "end\n";
    // This DRL is in a named package, but imports the model (trying with defaultPkg prefix)
    String drl = "" + "package org.smurf\n" + "import defaultpkg.Smurf\n" + "rule \"test\"\n" + "when\n" + "Smurf()\n" + "then\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write("src/main/resources/model.drl", model_drl);
    kfs.write("src/main/resources/drl.drl", drl);
    KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(0, builder.getResults().getMessages().size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 23 with KieBuilder

use of org.kie.api.builder.KieBuilder in project drools by kiegroup.

the class KieHelloWorldTest method testImport.

@Test
public void testImport() throws Exception {
    // DROOLS-859
    String drl1 = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "rule R1 when\n" + "    $m : Message( message == \"Hello World\" )\n" + "then\n" + "    list.add(\"ok\");\n" + "end\n";
    String drl2 = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "rule R2 when\n" + "   $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "   list.add(\"ko\");\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "test-import", "1.0-SNAPSHOT");
    KieModuleModel kproj = ks.newKieModuleModel();
    kproj.newKieBaseModel("kbase").addPackage("rules").newKieSessionModel("ksession").setDefault(true);
    KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).write("src/main/resources/rules/r1.drl", drl1).write("src/main/resources/myrules/r2.drl", drl2).writeKModuleXML(kproj.toXML());
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    KieSession ksession = ks.newKieContainer(releaseId).newKieSession();
    List<String> results = new ArrayList<String>();
    ksession.setGlobal("list", results);
    ksession.insert(new Message("Hello World"));
    ksession.insert(new Message("Hi Universe"));
    ksession.fireAllRules();
    assertEquals(1, results.size());
    assertEquals("ok", results.get(0));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) KieModuleModel(org.kie.api.builder.model.KieModuleModel) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 24 with KieBuilder

use of org.kie.api.builder.KieBuilder in project drools by kiegroup.

the class KieHelloWorldTest method testErrorReportingWithWrongKmodule.

@Test
public void testErrorReportingWithWrongKmodule() throws Exception {
    // RHDM-69
    String kmodule = "<kmodule xmlns=\"http://jboss.org/kie/6.0.0/kmodule\">\n" + "  <kbase name=\"ABC\" default=\"false\" eventProcessingMode=\"stream\" equalsBehavior=\"identity\"/>\n" + "  <kbase name=\"ABC\" default=\"false\" eventProcessingMode=\"stream\" equalsBehavior=\"identity\"/>\n" + "</kmodule>\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", createDrl("R1"));
    kfs.writeKModuleXML(kmodule);
    KieBuilder kb = ks.newKieBuilder(kfs).buildAll();
    assertEquals(1, kb.getResults().getMessages().size());
    assertTrue(kb.getResults().getMessages().get(0).toString().contains("ABC"));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 25 with KieBuilder

use of org.kie.api.builder.KieBuilder in project drools by kiegroup.

the class KieLoggersTest method populateKieFileSystem.

private KieServices populateKieFileSystem(Resource dt) {
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write(dt);
    KieBuilder kb = ks.newKieBuilder(kfs).buildAll();
    assertTrue(kb.getResults().getMessages().isEmpty());
    return ks;
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder)

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