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());
}
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());
}
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));
}
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();
}
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();
}
Aggregations