Search in sources :

Example 16 with Message

use of org.drools.compiler.Message in project drools by kiegroup.

the class KieCompilationCacheTest 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();
    InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId);
    byte[] jar = kieModule.getBytes();
    MemoryFileSystem mfs = MemoryFileSystem.readFromJar(jar);
    File file = mfs.getFile(KieBuilderImpl.getCompilationCachePath(releaseId, "KBase1"));
    assertNotNull(file);
    file = mfs.getFile(KieBuilderImpl.getCompilationCachePath(releaseId, "KBase2"));
    assertNotNull(file);
    Resource jarRes = ks.getResources().newByteArrayResource(jar);
    KieModule km = ks.getRepository().addKieModule(jarRes);
    KieSession ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
    ksession.insert(new Message("Aloha Earth"));
    assertEquals(0, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2");
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(0, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2");
    ksession.insert(new Message("Aloha Earth"));
    assertEquals(1, ksession.fireAllRules());
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) File(org.drools.compiler.compiler.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 17 with Message

use of org.drools.compiler.Message 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 18 with Message

use of org.drools.compiler.Message 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 19 with Message

use of org.drools.compiler.Message 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 20 with Message

use of org.drools.compiler.Message in project drools by kiegroup.

the class KieHelloWorldTest method testHelloWorldOnVersionRange.

@Test
public void testHelloWorldOnVersionRange() throws Exception {
    KieServices ks = KieServices.Factory.get();
    buildVersion(ks, "Hello World", "1.0");
    buildVersion(ks, "Aloha Earth", "1.1");
    buildVersion(ks, "Hi Universe", "1.2");
    ReleaseId latestReleaseId = ks.newReleaseId("org.kie", "hello-world", "LATEST");
    KieSession ksession = ks.newKieContainer(latestReleaseId).newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));
    assertEquals(0, ksession.fireAllRules());
    ksession = ks.newKieContainer(latestReleaseId).newKieSession("KSession1");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(1, ksession.fireAllRules());
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "hello-world", "1.0");
    ksession = ks.newKieContainer(releaseId1).newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(releaseId1).newKieSession("KSession1");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(0, ksession.fireAllRules());
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "hello-world", "[1.0,1.2)");
    ksession = ks.newKieContainer(releaseId2).newKieSession("KSession1");
    ksession.insert(new Message("Aloha Earth"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(releaseId2).newKieSession("KSession1");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(0, 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)

Aggregations

Message (org.drools.compiler.Message)68 Test (org.junit.Test)65 KieSession (org.kie.api.runtime.KieSession)59 KieServices (org.kie.api.KieServices)41 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)36 ReleaseId (org.kie.api.builder.ReleaseId)30 KieContainer (org.kie.api.runtime.KieContainer)25 KieModule (org.kie.api.builder.KieModule)24 KieBase (org.kie.api.KieBase)20 KieFileSystem (org.kie.api.builder.KieFileSystem)19 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)18 ArrayList (java.util.ArrayList)14 KieRuntimeLogger (org.kie.api.logger.KieRuntimeLogger)8 List (java.util.List)7 KieBuilder (org.kie.api.builder.KieBuilder)6 Resource (org.kie.api.io.Resource)6 File (java.io.File)5 DynamicRulesTest (org.drools.compiler.integrationtests.DynamicRulesTest)4 KieModuleModel (org.kie.api.builder.model.KieModuleModel)4 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)4