Search in sources :

Example 21 with Message

use of org.drools.mvel.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 " + Message.class.getCanonicalName() + "\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);
    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()).getAgendaGroupsManager().agendaSize());
    ksession.fireAllRules();
    assertEquals(5, ((Number) results.get(1)).intValue());
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) Message(org.drools.mvel.compiler.Message) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) IteratorToList(org.drools.mvel.integrationtests.IteratorToList) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 22 with Message

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

the class MarshallingTest method testAccumulate2.

@Test
public void testAccumulate2() throws Exception {
    String str = "package org.drools\n" + "\n" + "import " + Message.class.getCanonicalName() + "\n" + "rule MyRule\n" + "  when\n" + "    Number( intValue >= 5 ) from accumulate ( m: Message( ), count( m ) )\n" + "  then\n" + "    System.out.println(\"Found messages\");\n" + "end\n";
    KieBase kBase = loadKnowledgeBaseFromString(str);
    KieSession ksession = kBase.newKieSession();
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.insert(new Message());
    ksession.insert(new Message());
    ksession.insert(new Message());
    ksession.insert(new Message());
    ksession.insert(new Message());
    ((InternalWorkingMemory) ksession).flushPropagations();
    assertEquals(1, ((InternalAgenda) ksession.getAgenda()).getAgendaGroupsManager().agendaSize());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) InternalAgenda(org.drools.core.common.InternalAgenda) Message(org.drools.mvel.compiler.Message) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 23 with Message

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

the class NullTest method testNullFieldOnCompositeSink.

@Test
public void testNullFieldOnCompositeSink() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_NullFieldOnCompositeSink.drl");
    KieSession ksession = kbase.newKieSession();
    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.mvel.compiler.Message) Attribute(org.drools.mvel.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)

Example 24 with Message

use of org.drools.mvel.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.mvel.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();
    KieBuilder kbuilder = KieUtil.getKieBuilderFromDrls(kieBaseTestConfiguration, false, str);
    List<org.kie.api.builder.Message> errors = kbuilder.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR);
    assertTrue(errors.toString(), errors.isEmpty());
    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 : Message(org.drools.mvel.compiler.Message) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) KieBuilder(org.kie.api.builder.KieBuilder) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 25 with Message

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

the class Misc2Test method testNPEOnMutableGlobal.

@Test
public void testNPEOnMutableGlobal() throws Exception {
    // BZ-1019473
    String str = "package org.drools.mvel.compiler\n" + "global java.util.List context\n" + "rule B\n" + "  when\n" + "    Message( message == \"b\" )\n" + "    $s : String() from context\n" + "  then\n" + "    System.out.println($s);\n" + "end";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    List<String> context = new ArrayList<>();
    ksession.setGlobal("context", context);
    FactHandle b = ksession.insert(new Message("b"));
    ksession.delete(b);
    int fired = ksession.fireAllRules(1);
    assertEquals(0, fired);
    ksession.dispose();
}
Also used : Message(org.drools.mvel.compiler.Message) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) Test(org.junit.Test)

Aggregations

Message (org.drools.mvel.compiler.Message)45 Test (org.junit.Test)42 KieSession (org.kie.api.runtime.KieSession)35 KieServices (org.kie.api.KieServices)22 KieBase (org.kie.api.KieBase)18 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)17 KieFileSystem (org.kie.api.builder.KieFileSystem)14 ReleaseId (org.kie.api.builder.ReleaseId)14 KieModule (org.kie.api.builder.KieModule)12 ArrayList (java.util.ArrayList)10 KieRuntimeLogger (org.kie.api.logger.KieRuntimeLogger)8 FactWithString (org.drools.mvel.integrationtests.facts.FactWithString)6 KieBuilder (org.kie.api.builder.KieBuilder)6 Resource (org.kie.api.io.Resource)6 KieContainer (org.kie.api.runtime.KieContainer)6 File (java.io.File)5 List (java.util.List)5 DynamicRulesTest (org.drools.mvel.integrationtests.DynamicRulesTest)4 KieModuleModel (org.kie.api.builder.model.KieModuleModel)4 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)2