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