Search in sources :

Example 46 with KieFileSystem

use of org.kie.api.builder.KieFileSystem 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 47 with KieFileSystem

use of org.kie.api.builder.KieFileSystem 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 48 with KieFileSystem

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

the class KieHelloWorldTest method testHelloWorldWithWildcardPackagesComplex.

@Test
public void testHelloWorldWithWildcardPackagesComplex() throws Exception {
    // BZ-1174563
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "hello-world", "1.0-SNAPSHOT");
    KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).write("src/main/resources/rules/rules.drl", createDrlWithGlobal("R1")).write("src/main/resources/rules/tests/tests.drl", createDrlWithGlobal("R2")).write("src/main/resources/aaarules/aaarules.drl", createDrlWithGlobal("R3")).write("src/main/resources/sample/brms601_1310778/rules/rules.drl", createDrlWithGlobal("R4")).write("src/main/resources/sample/brms601_1310778/tests/tests.drl", createDrlWithGlobal("R5")).write("src/main/resources/tests/tests.drl", createDrlWithGlobal("R6")).write("src/main/resources/rules2/rules2.drl", createDrlWithGlobal("R7")).writeKModuleXML(createKieProjectWithPackages(ks, "rules.*").toXML());
    ks.newKieBuilder(kfs).buildAll();
    KieSession ksession = ks.newKieContainer(releaseId).newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    assertEquals(2, ksession.fireAllRules());
    assertTrue(list.contains("R1"));
    assertTrue(list.contains("R2"));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) Test(org.junit.Test)

Example 49 with KieFileSystem

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

the class KieHelloWorldTest method testHelloWorldUsingPackages.

@Test
public void testHelloWorldUsingPackages() throws Exception {
    String drlDef = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getCanonicalName() + "\n" + "rule R_def when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "hello-world", "1.0-SNAPSHOT");
    KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).write("src/main/resources/KBase1/org/pkg1/r1_1.drl", drlDef).write("src/main/resources/KBase1/org/pkg1/r1_2.drl", createDrl("R1")).write("src/main/resources/KBase1/org/pkg2/r2.drl", createDrl("R2")).writeKModuleXML(createKieProjectWithPackages(ks, "org.pkg1").toXML());
    ks.newKieBuilder(kfs).buildAll();
    KieSession ksession = ks.newKieContainer(releaseId).newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));
    assertEquals(2, ksession.fireAllRules());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) Test(org.junit.Test)

Example 50 with KieFileSystem

use of org.kie.api.builder.KieFileSystem 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

KieFileSystem (org.kie.api.builder.KieFileSystem)297 KieServices (org.kie.api.KieServices)241 Test (org.junit.Test)211 KieBuilder (org.kie.api.builder.KieBuilder)179 KieSession (org.kie.api.runtime.KieSession)101 KieModuleModel (org.kie.api.builder.model.KieModuleModel)79 KieContainer (org.kie.api.runtime.KieContainer)74 ReleaseId (org.kie.api.builder.ReleaseId)64 Results (org.kie.api.builder.Results)60 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)53 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)39 KieBase (org.kie.api.KieBase)38 IncrementalResults (org.kie.internal.builder.IncrementalResults)36 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)32 KieBaseModel (org.kie.api.builder.model.KieBaseModel)31 ArrayList (java.util.ArrayList)30 Message (org.kie.api.builder.Message)26 KieModule (org.kie.api.builder.KieModule)22 Message (org.drools.compiler.Message)20 Resource (org.kie.api.io.Resource)18