Search in sources :

Example 31 with Message

use of org.drools.testcoverage.common.model.Message in project drools by kiegroup.

the class IncrementalCompilationTest method testKJarUpgrade.

@Test
public void testKJarUpgrade() {
    final String drl1 = "package org.drools.compiler\n" + "import " + Message.class.getCanonicalName() + ";\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    final String drl2_1 = "package org.drools.compiler\n" + "import " + Message.class.getCanonicalName() + ";\n" + "rule R2_1 when\n" + "   $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
    final String drl2_2 = "package org.drools.compiler\n" + "import " + Message.class.getCanonicalName() + ";\n" + "rule R2_2 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
    final KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieUtil.getKieModuleFromDrls(releaseId1, kieBaseTestConfiguration, drl1, drl2_1);
    // Create a session and fire rules
    final KieContainer kc = ks.newKieContainer(releaseId1);
    KieSession ksession = kc.newKieSession();
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    ksession.dispose();
    // Create a new jar for version 1.1.0
    final ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    KieUtil.getKieModuleFromDrls(releaseId2, kieBaseTestConfiguration, drl1, drl2_2);
    // try to update the container to version 1.1.0
    kc.updateToVersion(releaseId2);
    // create and use a new session
    ksession = kc.newKieSession();
    ksession.insert(new Message("Hello World"));
    assertEquals(2, ksession.fireAllRules());
}
Also used : Message(org.drools.testcoverage.common.model.Message) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 32 with Message

use of org.drools.testcoverage.common.model.Message in project drools by kiegroup.

the class IncrementalCompilationTest method testMultipleIncrementalCompilationsWithFireUntilHalt.

@Test(timeout = 20000L)
public void testMultipleIncrementalCompilationsWithFireUntilHalt() throws Exception {
    // DROOLS-1406
    final KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-fireUntilHalt", "1.0");
    KieUtil.getKieModuleFromDrls(releaseId1, kieBaseTestConfiguration, getTestRuleForFireUntilHalt(0));
    // Create a session and fire rules
    final KieContainer kc = ks.newKieContainer(releaseId1);
    final KieSession kieSession = kc.newKieSession();
    final DebugList<String> list = new DebugList<>();
    kieSession.setGlobal("list", list);
    kieSession.insert(new Message("X"));
    CountDownLatch done = new CountDownLatch(1);
    list.done = done;
    try {
        new Thread(kieSession::fireUntilHalt).start();
        done.await();
        assertEquals(1, list.size());
        assertEquals("0 - X", list.get(0));
        list.clear();
        for (int i = 1; i < 10; i++) {
            done = new CountDownLatch(1);
            list.done = done;
            final ReleaseId releaseIdI = ks.newReleaseId("org.kie", "test-fireUntilHalt", "1." + i);
            KieUtil.getKieModuleFromDrls(releaseIdI, kieBaseTestConfiguration, getTestRuleForFireUntilHalt(i));
            kc.updateToVersion(releaseIdI);
            done.await();
            assertEquals(1, list.size());
            assertEquals(i + " - X", list.get(0));
            list.clear();
        }
    } finally {
        kieSession.halt();
    }
}
Also used : Message(org.drools.testcoverage.common.model.Message) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) CountDownLatch(java.util.concurrent.CountDownLatch) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 33 with Message

use of org.drools.testcoverage.common.model.Message in project drools by kiegroup.

the class PatternTest method testHelloWorld.

@Test
public void testHelloWorld() {
    final String drl = "package org.drools.compiler.integrationtests.drl;\n" + " \n" + "//we don't use the import, as class is fully qualified below\n" + "\n" + "global java.util.List list;\n" + "\n" + "rule \"Hello World\"\n" + "    when\n" + "        $m : " + Message.class.getCanonicalName() + "(list contains \"hello\",\n" + "                                text:message, message == \"hola\",\n" + "                                fired == false,\n" + "                                number > 40,\n" + "                                birthday > \"10-Jul-1974\",\n" + "                                message matches \".*ho.*\",\n" + "                                list excludes \"wax\")\n" + "    then\n" + "        // putting in a complex consequence, to make sure it picks up the variabels correctly\n" + "        if (1==1)  {\n" + "            int a = 0;\n" + "        }\n" + "        try {\n" + "            //System.out.println(\"hello world with collections \" + $m.getMessage());\n" + "        } catch  ( Exception e ) {\n" + "\n" + "        } finally {\n" + "            list.add( $m );\n" + "        }\n" + "        modify($m) { setFired(true) }\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("pattern-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final List list = new ArrayList();
        ksession.setGlobal("list", list);
        final Message message = new Message("hola");
        message.addToList("hello");
        message.setNumber(42);
        ksession.insert(message);
        ksession.insert("boo");
        ksession.fireAllRules();
        assertTrue(message.isFired());
        assertEquals(message, ((List) ksession.getGlobal("list")).get(0));
    } finally {
        ksession.dispose();
    }
}
Also used : Message(org.drools.testcoverage.common.model.Message) 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)

Example 34 with Message

use of org.drools.testcoverage.common.model.Message in project drools by kiegroup.

the class OOPathCepTest method populateAndVerifyEventCase.

private void populateAndVerifyEventCase(final EntryPoint entryPoint) {
    final Message helloMessage = new Message("Hello");
    final MessageEvent helloEvent = new MessageEvent(MessageEvent.Type.sent, helloMessage);
    entryPoint.insert(helloEvent);
    final MessageEvent anotherEvent = new MessageEvent(MessageEvent.Type.sent, new Message("Not a hello"));
    entryPoint.insert(anotherEvent);
    this.kieSession.fireAllRules();
    Assertions.assertThat(this.messages).containsExactlyInAnyOrder(helloMessage);
}
Also used : Message(org.drools.testcoverage.common.model.Message) MessageEvent(org.drools.testcoverage.common.model.MessageEvent)

Example 35 with Message

use of org.drools.testcoverage.common.model.Message in project drools by kiegroup.

the class OOPathCepTest method testTemporalOperatorCoincidesWithOOPath.

@Test
public void testTemporalOperatorCoincidesWithOOPath() {
    final String drl = "import org.drools.testcoverage.common.model.Message;\n" + "import org.drools.testcoverage.common.model.MessageEvent;\n" + "global java.util.List events\n" + "global java.util.List messages\n" + "\n" + "declare org.drools.testcoverage.common.model.MessageEvent\n" + "  @role( event )\n" + "end\n" + "rule R when\n" + "  ev1: MessageEvent( /msg[ message == 'Ping' ] )\n" + "  ev2: MessageEvent( $message: /msg[ message == 'Pong' ], this coincides[1s] ev1 )\n" + "then\n" + "  messages.add( $message );\n" + "end\n";
    final KieBase kieBase = KieBaseUtil.getKieBaseFromKieModuleFromDrl(MODULE_GROUP_ID, kieBaseTestConfiguration, drl);
    final SessionPseudoClock clock = this.initKieSessionWithPseudoClock(kieBase);
    this.insertEventAndAdvanceClock(new MessageEvent(MessageEvent.Type.sent, new Message("Pong")), clock, 2);
    this.insertEvent(new MessageEvent(MessageEvent.Type.sent, new Message("Ping")));
    this.kieSession.fireAllRules();
    Assertions.assertThat(this.messages).as("The first sequence of events should NOT make the rule fire").isEmpty();
    final Message pongMessage = this.insertEvent(new MessageEvent(MessageEvent.Type.sent, new Message("Pong")));
    this.kieSession.fireAllRules();
    Assertions.assertThat(this.messages).as("The last event should make the rule fire").containsExactlyInAnyOrder(pongMessage);
}
Also used : Message(org.drools.testcoverage.common.model.Message) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) MessageEvent(org.drools.testcoverage.common.model.MessageEvent) Test(org.junit.Test)

Aggregations

Message (org.drools.testcoverage.common.model.Message)39 Test (org.junit.Test)32 KieBase (org.kie.api.KieBase)21 KieSession (org.kie.api.runtime.KieSession)20 MessageEvent (org.drools.testcoverage.common.model.MessageEvent)18 KieServices (org.kie.api.KieServices)14 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)14 KieContainer (org.kie.api.runtime.KieContainer)13 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)13 ReleaseId (org.kie.api.builder.ReleaseId)11 ArrayList (java.util.ArrayList)4 KieFileSystem (org.kie.api.builder.KieFileSystem)4 KieBuilder (org.kie.api.builder.KieBuilder)3 Resource (org.kie.api.io.Resource)3 IncrementalResults (org.kie.internal.builder.IncrementalResults)3 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)3 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Cheese (org.drools.testcoverage.common.model.Cheese)2 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)2