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