Search in sources :

Example 61 with Message

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

the class PatternTest method testHelloWorld.

@Test
public void testHelloWorld() throws Exception {
    final KieBase kbase = loadKnowledgeBase("HelloWorld.drl");
    final KieSession ksession = createKnowledgeSession(kbase);
    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));
}
Also used : Message(org.drools.compiler.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 62 with Message

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

the class IncrementalCompilationTest method testKJarUpgradeWithSpace.

@Test
public void testKJarUpgradeWithSpace() throws Exception {
    // DROOLS-1399
    String drl_1 = "package org.drools.compiler\n" + "rule Rx when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
    String drl_2 = // <<- notice the EXTRA SPACE is the only change in this other version.
    "package org.drools.compiler\n " + "rule Rx when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId1, drl_1);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    km = createAndDeployJar(ks, releaseId2, drl_2);
    kc.updateToVersion(releaseId2);
    // rule Rx is UNchanged and should NOT fire again
    assertEquals(0, ksession.fireAllRules());
}
Also used : Message(org.drools.compiler.Message) 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) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 63 with Message

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

the class Misc2Test method testInsertModifyInteractionsWithLockOnActive.

@Test
public void testInsertModifyInteractionsWithLockOnActive() {
    String drl = "package org.drools.compiler.integrationtests;\n" + "import org.drools.compiler.Message;\n" + "global Message m2;\n" + "rule r1 lock-on-active\n" + "    when\n" + "        m: Message()\n" + "    then\n" + "        modify( m ){ setMessage2( 'msg2' ) };\n" + "        m2 =  new Message( 'msg1' );\n" + "        kcontext.getKnowledgeRuntime().setGlobal( 'm2', m2 ); \n" + "        insert( m2 );\n" + "end\n" + "rule r2 lock-on-active salience 1000\n" + "    when\n" + "        m : Message()\n" + "    then\n" + "        modify( m ){ setMessage3( 'msg3' ) };\n" + "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(drl);
    KieSession ksession = kbase.newKieSession();
    Message m1 = new Message("msg1");
    ksession.insert(m1);
    assertEquals(2, ksession.fireAllRules());
    Message m2 = (Message) ksession.getGlobal("m2");
    assertEquals("msg1", m1.getMessage());
    assertEquals("msg2", m1.getMessage2());
    assertEquals("msg3", m1.getMessage3());
    assertEquals("msg1", m2.getMessage());
    // r1 does not fire for m2
    assertEquals("Two", m2.getMessage2());
    assertEquals("Three", m2.getMessage3());
}
Also used : Message(org.drools.compiler.Message) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Test(org.junit.Test)

Example 64 with Message

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

the class MarshallingTest method testAccumulate.

@Test
public void testAccumulate() throws Exception {
    String rule = "package org.drools\n" + "import org.drools.compiler.Message\n" + "global java.util.List results\n" + "rule MyRule\n" + "  when\n" + "    $n : Number( intValue >= 2 ) from accumulate ( m: Message( ), count( m ) )\n" + "  then\n" + "    results.add($n);\n" + "end";
    KieBase kBase = loadKnowledgeBaseFromString(rule);
    KieSession ksession = getSerialisedStatefulKnowledgeSession(kBase.newKieSession(), true);
    kBase = SerializationHelper.serializeObject(kBase);
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.setGlobal("results", new ArrayList());
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.insert(new Message());
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    List results = (List) ksession.getGlobal("results");
    ksession.insert(new Message());
    ksession.insert(new Message());
    ksession.fireAllRules();
    assertEquals(3, ((Number) results.get(0)).intValue());
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.insert(new Message());
    ksession.insert(new Message());
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    assertEquals(1, ((InternalAgenda) ksession.getAgenda()).agendaSize());
    ksession.fireAllRules();
    assertEquals(5, ((Number) results.get(1)).intValue());
}
Also used : Message(org.drools.compiler.Message) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) Test(org.junit.Test)

Example 65 with Message

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

the class AndTest method testExplicitAnd.

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