Search in sources :

Example 11 with Message

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

the class SequentialTest method testSequentialWithNoLoop.

@Test(timeout = 10000L)
public void testSequentialWithNoLoop() throws Exception {
    // BZ-1228098
    String str = "package org.drools.compiler.test\n" + "import \n" + Message.class.getCanonicalName() + ";" + "rule R1 no-loop when\n" + "    $s : String( )" + "    $m : Message( )\n" + "then\n" + "    modify($m) { setMessage($s) };\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write("src/main/resources/r0.drl", str);
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    Results results = kieBuilder.getResults();
    if (results.hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
        throw new RuntimeException(results.getMessages().toString());
    }
    KieContainer kieContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    KieBaseConfiguration kieBaseConf = ks.newKieBaseConfiguration();
    kieBaseConf.setOption(SequentialOption.YES);
    StatelessKieSession sequentialKsession = kieContainer.newKieBase(kieBaseConf).newStatelessKieSession();
    List result = (List) sequentialKsession.execute(CommandFactory.newInsertElements(Arrays.asList("test", new Message())));
    assertEquals(2, result.size());
    StatelessKieSession ksession = kieContainer.getKieBase().newStatelessKieSession();
    result = (List) ksession.execute(CommandFactory.newInsertElements(Arrays.asList("test", new Message())));
    assertEquals(2, result.size());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) Results(org.kie.api.builder.Results) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieServices(org.kie.api.KieServices) ArrayList(java.util.ArrayList) List(java.util.List) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer) DynamicRulesTest(org.drools.compiler.integrationtests.DynamicRulesTest) Test(org.junit.Test)

Example 12 with Message

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

the class StatefulSessionTest method testDispose.

@Test
public void testDispose() 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()\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();
    try {
        // the following should raise an IllegalStateException as the session was already disposed
        ksession.fireAllRules();
        fail("An IllegallStateException should have been raised as the session was disposed before the method call.");
    } catch (final IllegalStateException ise) {
    // success
    }
}
Also used : Message(org.drools.compiler.Message) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 13 with Message

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

the class Misc2Test method testNoLoopWithNamedConsequences.

@Test
public void testNoLoopWithNamedConsequences() {
    // DROOLS-327
    String drl = "import org.drools.compiler.Message\n" + "rule \"Hello World\" no-loop\n" + "    when\n" + "        m : Message( myMessage : message )\n" + "        if (status == 0) do[sayHello]\n" + "    then\n" + "        System.out.println( myMessage );\n" + "        m.setMessage( \"Goodbye cruel world\" );\n" + "        m.setStatus( Message.GOODBYE );\n" + "        update( m );\n" + "    then[sayHello]\n" + "        System.out.println(\"Hello, I'm here!\");\n" + "end\n";
    KnowledgeBuilderConfiguration kbConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    KieBase kbase = loadKnowledgeBaseFromString(kbConf, drl);
    KieSession ksession = kbase.newKieSession();
    ksession.insert(new Message("Hello World"));
    ksession.fireAllRules();
}
Also used : Message(org.drools.compiler.Message) KieBase(org.kie.api.KieBase) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Test(org.junit.Test)

Example 14 with Message

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

the class Misc2Test method testNonSerializableInEvaluatorWrapper.

@Test
public void testNonSerializableInEvaluatorWrapper() throws Exception {
    // BZ-1315143
    String str = "package org.drools.compiler\n" + "rule B\n" + "  when\n" + "    $m1 : Message( $message1 : message, $date1 : birthday )\n" + "    $m2 : Message( this != $m1, message != $message1, birthday after $date1 )\n" + "  then\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write(ResourceFactory.newByteArrayResource(str.getBytes()).setTargetPath("org/drools/compiler/rules.drl"));
    KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
    kbuilder.buildAll();
    assertEquals(0, kbuilder.getResults().getMessages().size());
    KieSession ksession1 = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieSession();
    Message message1 = new Message();
    message1.setMessage("Hello World");
    message1.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2015-12-15"));
    Message message2 = new Message();
    message2.setMessage("Goodbye World");
    message2.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2015-12-16"));
    ksession1.insert(message1);
    ksession1.insert(message2);
    int fired1 = ksession1.fireAllRules();
    assertEquals(1, fired1);
    ksession1.dispose();
    // Force deepClone
    KieSession ksession2 = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieSession();
    Message message3 = new Message();
    message3.setMessage("Hello World");
    message3.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2015-12-15"));
    Message message4 = new Message();
    message4.setMessage("Goodbye World");
    message4.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2015-12-16"));
    ksession2.insert(message3);
    ksession2.insert(message4);
    int fired2 = ksession2.fireAllRules();
    assertEquals(1, fired2);
    ksession2.dispose();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieBuilder(org.kie.api.builder.KieBuilder) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 15 with Message

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

the class NullTest method testNullFieldOnCompositeSink.

@Test
public void testNullFieldOnCompositeSink() throws Exception {
    final KieBase kbase = loadKnowledgeBase("test_NullFieldOnCompositeSink.drl");
    KieSession ksession = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(new Attribute());
    ksession.insert(new Message());
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(1, ((List) ksession.getGlobal("list")).size());
    assertEquals("X", ((List) ksession.getGlobal("list")).get(0));
}
Also used : Message(org.drools.compiler.Message) Attribute(org.drools.compiler.Attribute) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) 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