Search in sources :

Example 31 with Message

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

the class JBRULESTest method testJBRULES3326.

@Test
public void testJBRULES3326() throws Exception {
    final StringBuilder rule = new StringBuilder();
    rule.append("package org.drools.compiler\n");
    rule.append("rule X\n");
    rule.append("when\n");
    rule.append("    Message(!!!false)\n");
    rule.append("then\n");
    rule.append("end\n");
    // building stuff
    final KieBase kbase = loadKnowledgeBaseFromString(rule.toString());
    final KieSession ksession = createKnowledgeSession(kbase);
    ksession.insert(new Message("test"));
    final int rules = ksession.fireAllRules();
    assertEquals(1, rules);
    ksession.dispose();
}
Also used : Message(org.drools.compiler.Message) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 32 with Message

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

the class KieBuilderTest method testResourceInclusion.

@Test
public void testResourceInclusion() {
    final String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    final String drl2 = "package org.drools.compiler\n" + "rule R2 when\n" + "   $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
    final String drl3 = "package org.drools.compiler\n" + "rule R3 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
    final String drl4 = "package org.drools.compiler\n" + "rule R4 when\n" + "   $m : Message( message == \"Hello Earth\" )\n" + "then\n" + "end\n";
    final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + "         xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "  <kbase name=\"kbase1\" default=\"true\" eventProcessingMode=\"stream\" equalsBehavior=\"identity\" scope=\"javax.enterprise.context.ApplicationScoped\">\n" + "    <ksession name=\"ksession1\" type=\"stateful\" default=\"true\" clockType=\"realtime\" scope=\"javax.enterprise.context.ApplicationScoped\"/>\n" + "  </kbase>\n" + "</kmodule>";
    final KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
    final Resource r1 = ResourceFactory.newByteArrayResource(drl1.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
    final Resource r2 = ResourceFactory.newByteArrayResource(drl2.getBytes()).setResourceType(ResourceType.GDRL).setSourcePath("kbase1/drl2.gdrl");
    final Resource r3 = ResourceFactory.newByteArrayResource(drl3.getBytes()).setResourceType(ResourceType.RDRL).setSourcePath("kbase1/drl3.rdrl");
    final Resource r4 = ResourceFactory.newByteArrayResource(drl4.getBytes()).setResourceType(ResourceType.TDRL).setSourcePath("kbase1/drl4.tdrl");
    final KieModule km = createAndDeployJar(ks, kmodule, releaseId1, r1, r2, r3, r4);
    final InternalKieModule ikm = (InternalKieModule) km;
    assertNotNull(ikm.getResource(r1.getSourcePath()));
    assertNotNull(ikm.getResource(r2.getSourcePath()));
    assertNotNull(ikm.getResource(r3.getSourcePath()));
    assertNotNull(ikm.getResource(r4.getSourcePath()));
    // Create a session and fire rules
    final KieContainer kc = ks.newKieContainer(km.getReleaseId());
    final KieSession ksession = kc.newKieSession();
    ksession.insert(new Message("Hello World"));
    assertEquals(2, ksession.fireAllRules());
    ksession.dispose();
}
Also used : Message(org.drools.compiler.Message) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 33 with Message

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

the class KieHelloWorldTest method testHelloWorldWithEmptyFile.

@Test
public void testHelloWorldWithEmptyFile() throws Exception {
    String drl = createDrl("R1");
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl).write("src/main/resources/empty.drl", ks.getResources().newInputStreamResource(new ByteArrayInputStream(new byte[0])));
    ks.newKieBuilder(kfs).buildAll();
    KieSession ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    ksession.insert(new Message("Hello World"));
    int count = ksession.fireAllRules();
    assertEquals(1, count);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 34 with Message

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

the class KieHelloWorldTest method testHelloWorld.

@Test
public void testHelloWorld() throws Exception {
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", createDrl("R1"));
    ks.newKieBuilder(kfs).buildAll();
    KieSession ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    ksession.insert(new Message("Hello World"));
    int count = ksession.fireAllRules();
    assertEquals(1, count);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 35 with Message

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

the class KieHelloWorldTest method testHelloWorldWithWildcardPackages.

@Test
public void testHelloWorldWithWildcardPackages() throws Exception {
    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/org/pkg1/test/r1.drl", createDrlWithGlobal("R1")).write("src/main/resources/org/pkg2/test/r2.drl", createDrlWithGlobal("R2")).writeKModuleXML(createKieProjectWithPackages(ks, "org.pkg1.*").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(1, ksession.fireAllRules());
    assertEquals(1, list.size());
    assertEquals("R1", list.get(0));
}
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)

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