Search in sources :

Example 71 with ReleaseId

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

the class KieContainerTest method testGetDefaultKieSessionModelEmptyKmodule.

@Test
public void testGetDefaultKieSessionModelEmptyKmodule() {
    KieServices kieServices = KieServices.Factory.get();
    String drl = "package org.drools.test\n" + "rule R1 when\n" + "   $m : Object()\n" + "then\n" + "end\n";
    Resource resource = kieServices.getResources().newReaderResource(new StringReader(drl), "UTF-8");
    resource.setTargetPath("org/drools/test/rules.drl");
    String kmodule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "</kmodule>";
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId = kieServices.newReleaseId("org.kie", "test-testGetDefaultKieSessionModelEmptyKmodule", "1.0.0");
    createAndDeployJar(kieServices, kmodule, releaseId, resource);
    KieContainer kieContainer = kieServices.newKieContainer(releaseId);
    KieSessionModel sessionModel = kieContainer.getKieSessionModel(null);
    assertNotNull(sessionModel);
}
Also used : Resource(org.kie.api.io.Resource) StringReader(java.io.StringReader) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieContainer(org.drools.core.impl.InternalKieContainer) KieContainer(org.kie.api.runtime.KieContainer) KieSessionModel(org.kie.api.builder.model.KieSessionModel) Test(org.junit.Test)

Example 72 with ReleaseId

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

the class KieContainerTest method testSharedTypeDeclarationsUsingFactTypes.

@Test
public void testSharedTypeDeclarationsUsingFactTypes() throws Exception {
    String type = "package org.drools.test\n" + "declare Message\n" + "   message : String\n" + "end\n";
    String drl1 = "package org.drools.test\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "   if ($m.getClass() != new Message(\"Test\").getClass()) {\n" + "       throw new RuntimeException();\n" + "   }\n" + "end\n";
    String drl2 = "package org.drools.test\n" + "rule R2_2 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "   if ($m.getClass() != new Message(\"Test\").getClass()) {\n" + "       throw new RuntimeException();\n" + "   }\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-delete", "1.0.0");
    createAndDeployJar(ks, releaseId1, type, drl1, drl2);
    KieContainer kieContainer = ks.newKieContainer(releaseId1);
    KieContainer kieContainer2 = ks.newKieContainer(releaseId1);
    KieSession ksession = kieContainer.newKieSession();
    KieSession ksession2 = kieContainer2.newKieSession();
    insertMessageFromTypeDeclaration(ksession);
    assertEquals(2, ksession.fireAllRules());
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-delete", "1.0.1");
    createAndDeployJar(ks, releaseId2, type, null, drl2);
    kieContainer.updateToVersion(releaseId2);
    // test with the old ksession ...
    ksession = kieContainer.newKieSession();
    insertMessageFromTypeDeclaration(ksession);
    assertEquals(1, ksession.fireAllRules());
    // ... and with a brand new one
    ksession = kieContainer.newKieSession();
    insertMessageFromTypeDeclaration(ksession);
    assertEquals(1, ksession.fireAllRules());
    // check that the second kieContainer hasn't been affected by the update of the first one
    insertMessageFromTypeDeclaration(ksession2);
    assertEquals(2, ksession2.fireAllRules());
    ksession2 = kieContainer2.newKieSession();
    insertMessageFromTypeDeclaration(ksession2);
    assertEquals(2, ksession2.fireAllRules());
}
Also used : KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieContainer(org.drools.core.impl.InternalKieContainer) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 73 with ReleaseId

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

the class KieHelloWorldTest method testGetDefaultKieSessionWithNullName.

@Test
public void testGetDefaultKieSessionWithNullName() throws Exception {
    // DROOLS-1276
    KieServices ks = KieServices.Factory.get();
    buildVersion(ks, "Hello World", "1.0");
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "hello-world", "1.0");
    KieSession ksession = ks.newKieContainer(releaseId1).newKieSession((String) null);
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
}
Also used : 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 74 with ReleaseId

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

the class KieHelloWorldTest method testHelloWorldWithPackagesAnd2KieBases.

@Test
public void testHelloWorldWithPackagesAnd2KieBases() throws Exception {
    String drl1 = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getCanonicalName() + "\n" + "rule R11 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule R12 when\n" + "   $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
    String drl2 = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getCanonicalName() + "\n" + "rule R21 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule R22 when\n" + "   $m : Message( message == \"Aloha Earth\" )\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.drl", drl1).write("src/main/resources/KBase1/org/pkg2/r2.drl", drl2).writeKModuleXML(createKieProjectWithPackagesAnd2KieBases(ks).toXML());
    ks.newKieBuilder(kfs).buildAll();
    KieSession ksession = ks.newKieContainer(releaseId).newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(releaseId).newKieSession("KSession1");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(releaseId).newKieSession("KSession1");
    ksession.insert(new Message("Aloha Earth"));
    assertEquals(0, ksession.fireAllRules());
    ksession = ks.newKieContainer(releaseId).newKieSession("KSession2");
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(releaseId).newKieSession("KSession2");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(0, ksession.fireAllRules());
    ksession = ks.newKieContainer(releaseId).newKieSession("KSession2");
    ksession.insert(new Message("Aloha Earth"));
    assertEquals(1, 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 75 with ReleaseId

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

Aggregations

ReleaseId (org.kie.api.builder.ReleaseId)273 Test (org.junit.Test)219 KieServices (org.kie.api.KieServices)214 KieContainer (org.kie.api.runtime.KieContainer)176 KieSession (org.kie.api.runtime.KieSession)137 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)117 KieModule (org.kie.api.builder.KieModule)84 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)73 KieFileSystem (org.kie.api.builder.KieFileSystem)64 KieModuleModel (org.kie.api.builder.model.KieModuleModel)40 KieBuilder (org.kie.api.builder.KieBuilder)34 ArrayList (java.util.ArrayList)32 Message (org.drools.compiler.Message)32 KieMavenRepository.getKieMavenRepository (org.kie.scanner.KieMavenRepository.getKieMavenRepository)27 Resource (org.kie.api.io.Resource)23 Results (org.kie.api.builder.Results)20 KieBaseModel (org.kie.api.builder.model.KieBaseModel)20 InternalKieScanner (org.drools.compiler.kie.builder.impl.InternalKieScanner)18 KieScanner (org.kie.api.builder.KieScanner)18 IncrementalResults (org.kie.internal.builder.IncrementalResults)18