Search in sources :

Example 26 with Message

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

the class Misc2Test method testPackagingJarWithTypeDeclarations.

@Test
public void testPackagingJarWithTypeDeclarations() throws Exception {
    // BZ-1054823
    String drl1 = "package org.drools.compiler\n" + "import org.drools.compiler.Message\n" + "declare Message\n" + "   @role (event)\n" + "end\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = KieUtil.getKieModuleFromDrls(releaseId, kieBaseTestConfiguration, drl1);
    // Create a session and fire rules
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    ksession.insert(new Message("Hello World"));
    ksession.fireAllRules();
}
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) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 27 with Message

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

the class Misc2Test method testInsertModifyInteractionsWithLockOnActive.

@Test
public void testInsertModifyInteractionsWithLockOnActive() {
    String drl = "package org.drools.mvel.integrationtests;\n" + "import org.drools.mvel.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 = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, 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.mvel.compiler.Message) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) Test(org.junit.Test)

Example 28 with Message

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

the class JBRULESTest method testJBRULES3326.

@Test
public void testJBRULES3326() throws Exception {
    final StringBuilder rule = new StringBuilder();
    rule.append("package org.drools.mvel.compiler\n");
    rule.append("rule X\n");
    rule.append("when\n");
    rule.append("    Message(!!!false)\n");
    rule.append("then\n");
    rule.append("end\n");
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, rule.toString());
    KieSession ksession = kbase.newKieSession();
    ksession.insert(new Message("test"));
    final int rules = ksession.fireAllRules();
    assertEquals(1, rules);
    ksession.dispose();
}
Also used : Message(org.drools.mvel.compiler.Message) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 29 with Message

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

the class KieCompilationCacheTest method testHelloWorldWithPackagesAnd2KieBases.

@Test
public void testHelloWorldWithPackagesAnd2KieBases() throws Exception {
    String drl1 = "package org.pkg1\n" + "import " + Message.class.getCanonicalName() + "\n" + "rule R11 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule R12 when\n" + "   $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
    String drl2 = "package org.pkg2\n" + "import " + Message.class.getCanonicalName() + "\n" + "rule R21 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule R22 when\n" + "   $m : Message( message == \"Aloha Earth\" )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "hello-world", "1.0");
    KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).write("src/main/resources/KBase1/org/pkg1/r1.drl", drl1).write("src/main/resources/KBase1/org/pkg2/r2.drl", drl2).writeKModuleXML(createKieProjectWithPackagesAnd2KieBases(ks).toXML());
    final KieBuilder kieBuilder = KieUtil.getKieBuilderFromKieFileSystem(kieBaseTestConfiguration, kfs, false);
    InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId);
    byte[] jar = kieModule.getBytes();
    MemoryFileSystem mfs = MemoryFileSystem.readFromJar(jar);
    File file = mfs.getFile(KieBuilderImpl.getCompilationCachePath(releaseId, "KBase1"));
    assertNotNull(file);
    file = mfs.getFile(KieBuilderImpl.getCompilationCachePath(releaseId, "KBase2"));
    assertNotNull(file);
    Resource jarRes = ks.getResources().newByteArrayResource(jar);
    KieModule km = ks.getRepository().addKieModule(jarRes);
    KieSession ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
    ksession.insert(new Message("Aloha Earth"));
    assertEquals(0, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2");
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(0, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2");
    ksession.insert(new Message("Aloha Earth"));
    assertEquals(1, ksession.fireAllRules());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.mvel.compiler.Message) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) File(org.drools.compiler.compiler.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 30 with Message

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

the class KieHelloWorldTest method testHelloWorldWithPackages.

@Test
public void testHelloWorldWithPackages() throws Exception {
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "hello-world", "1.0");
    KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).write("src/main/resources/KBase1/org/pkg1/r1.drl", createDrl("org.pkg1", "R1")).write("src/main/resources/KBase1/org/pkg2/r2.drl", createDrl("org.pkg2", "R2")).writeKModuleXML(createKieProjectWithPackages(ks, "org.pkg1").toXML());
    KieUtil.getKieBuilderFromKieFileSystem(kieBaseTestConfiguration, kfs, false);
    KieSession ksession = ks.newKieContainer(releaseId).newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));
    int count = ksession.fireAllRules();
    assertEquals(1, count);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.mvel.compiler.Message) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) 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