Search in sources :

Example 51 with Message

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

the class IncrementalCompilationTest method testKJarUpgradeWithDSL.

@Test
public void testKJarUpgradeWithDSL() throws Exception {
    // DROOLS-718
    String dsl = "[when][]There is a Message=Message()\n" + "[when][]-with message \"{factId}\"=message==\"{factId}\"\n" + "\n" + "[then][]Print \"{message}\"=System.out.println(\"{message}\");\n";
    String drl2_1 = "package org.drools.compiler\n" + "rule \"bla\"\n" + "when\n" + "\tThere is a Message\t   \n" + "\t-with message \"Hi Universe\"\n" + "then\n" + "\tPrint \"Found a Message Hi Universe.\"\n" + "end\n";
    String drl2_2 = "package org.drools.compiler\n" + "rule \"bla\"\n" + "when\n" + "\tThere is a Message\t   \n" + "\t-with message \"Hello World\"\n" + "then\n" + "\tPrint \"Found a Message Hello World.\"\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJarWithDSL(ks, releaseId1, dsl, drl2_1);
    // Create a session and fire rules
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    ksession.insert(new Message("Hello World"));
    assertEquals(0, ksession.fireAllRules());
    ksession.dispose();
    // Create a new jar for version 1.1.0
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    km = createAndDeployJarWithDSL(ks, releaseId2, dsl, drl2_2);
    // try to update the container to version 1.1.0
    kc.updateToVersion(releaseId2);
    // create and use a new session
    ksession = kc.newKieSession();
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
}
Also used : Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 52 with Message

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

the class IncrementalCompilationTest method testKJarUpgradeSameSessionAddingGlobal.

@Test
public void testKJarUpgradeSameSessionAddingGlobal() throws Exception {
    // DROOLS-523
    String drl1 = "package org.drools.compiler\n" + "global java.lang.String foo\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    String drl2_1 = "package org.drools.compiler\n" + "rule R2_1 when\n" + "   $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
    String drl2_2 = "package org.drools.compiler\n" + "global java.lang.String foo\n" + "rule R2_2 when\n" + "   $m : Message( message == foo )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId1, drl1, drl2_1);
    // Create a session and fire rules
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    // Create a new jar for version 1.1.0
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    km = createAndDeployJar(ks, releaseId2, drl1, drl2_2);
    // try to update the container to version 1.1.0
    kc.updateToVersion(releaseId2);
    ksession.setGlobal("foo", "Hello World");
    // continue working with the session
    ksession.insert(new Message("Hello World"));
    assertEquals(2, ksession.fireAllRules());
}
Also used : Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 53 with Message

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

the class IncrementalCompilationTest method testKJarUpgradeSameSession.

@Test
public void testKJarUpgradeSameSession() throws Exception {
    String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    String drl2_1 = "package org.drools.compiler\n" + "rule R2_1 when\n" + "   $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
    String drl2_2 = "package org.drools.compiler\n" + "rule R2_2 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId1, drl1, drl2_1);
    // Create a session and fire rules
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    // Create a new jar for version 1.1.0
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    km = createAndDeployJar(ks, releaseId2, drl1, drl2_2);
    // try to update the container to version 1.1.0
    kc.updateToVersion(releaseId2);
    // continue working with the session
    ksession.insert(new Message("Hello World"));
    assertEquals(3, ksession.fireAllRules());
}
Also used : Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 54 with Message

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

the class IncrementalCompilationTest method testRuleRemovalAndEval.

@Test
public void testRuleRemovalAndEval() throws Exception {
    // DROOLS-2276
    String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + "   $m : Message()\n" + "   eval($m != null)\n" + "then\n" + "   System.out.println( \"Hello R1\" );\n" + "end\n";
    String drl2 = "package org.drools.compiler\n" + "rule R2 when\n" + "   $m : Message()\n" + "   eval($m != null)\n" + "then\n" + "   System.out.println( \"Hello R2\" );\n" + "end\n";
    String drl3 = "package org.drools.compiler\n" + "rule R3 when\n" + "   $m : Message()\n" + "   eval($m != null)\n" + "then\n" + "   System.out.println( \"Hello R3\" );\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs1 = ks.newKieFileSystem();
    kfs1.write("src/main/resources/rules/Sample1.drl", drl1);
    ReleaseId releaseId1 = ks.newReleaseId("com.sample", "my-sample-a", "1.0.0");
    kfs1.generateAndWritePomXML(releaseId1);
    ks.newKieBuilder(kfs1).buildAll();
    KieFileSystem kfs2 = ks.newKieFileSystem();
    kfs2.write("src/main/resources/rules/Sample2.drl", drl2);
    ReleaseId releaseId2 = ks.newReleaseId("com.sample", "my-sample-a", "2.0.0");
    kfs2.generateAndWritePomXML(releaseId2);
    ks.newKieBuilder(kfs2).buildAll();
    KieFileSystem kfs3 = ks.newKieFileSystem();
    kfs3.write("src/main/resources/rules/Sample3.drl", drl3);
    ReleaseId releaseId3 = ks.newReleaseId("com.sample", "my-sample-a", "3.0.0");
    kfs3.generateAndWritePomXML(releaseId3);
    ks.newKieBuilder(kfs3).buildAll();
    // Create a session and fire rules
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieSession ksession = kc.newKieSession();
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    kc.updateToVersion(releaseId2);
    kc.updateToVersion(releaseId3);
    assertEquals(1, ksession.fireAllRules());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 55 with Message

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

the class IncrementalCompilationTest method testKJarUpgradeDRLWithSpace3_usingSingleQuote.

@Test
public void testKJarUpgradeDRLWithSpace3_usingSingleQuote() throws Exception {
    // DROOLS-1399 ter (using single quote)
    String drl_1 = "package org.drools.compiler\n" + "rule Rs when $s : String() then System.out.println($s); end\n" + "rule Rx when\n" + "   $m : Message( message == 'Hello World' )\n" + "then\n" + "  System.out.println($m); \n" + "end\n";
    String drl_2 = "package org.drools.compiler\n" + // <<- notice rule changed
    "rule Rs when $s : String( this == 'x') then System.out.println($s); end\n" + "rule Rx when\n" + // <<- notice the EXTRA SPACE is the an ADDITIONAL change in this other version.
    "   $m : Message( message == 'Hello World'  )\n" + "then\n" + "  System.out.println($m); \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();
    final List<String> fired = new ArrayList<>();
    ksession.addEventListener(new DefaultAgendaEventListener() {

        @Override
        public void afterMatchFired(AfterMatchFiredEvent event) {
            fired.add(event.getMatch().getRule().getName());
        }
    });
    ksession.insert(new Message("Hello World"));
    ksession.insert("x");
    assertEquals(2, ksession.fireAllRules());
    assertTrue(fired.contains("Rs"));
    assertTrue(fired.contains("Rx"));
    fired.clear();
    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
    // rule Rs is changed and should match again, and fire again.
    assertEquals(1, ksession.fireAllRules());
    assertTrue(fired.contains("Rs"));
    assertFalse(fired.contains("Rx"));
}
Also used : Message(org.drools.compiler.Message) ArrayList(java.util.ArrayList) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

Message (org.drools.compiler.Message)68 Test (org.junit.Test)65 KieSession (org.kie.api.runtime.KieSession)59 KieServices (org.kie.api.KieServices)41 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)36 ReleaseId (org.kie.api.builder.ReleaseId)30 KieContainer (org.kie.api.runtime.KieContainer)25 KieModule (org.kie.api.builder.KieModule)24 KieBase (org.kie.api.KieBase)20 KieFileSystem (org.kie.api.builder.KieFileSystem)19 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)18 ArrayList (java.util.ArrayList)14 KieRuntimeLogger (org.kie.api.logger.KieRuntimeLogger)8 List (java.util.List)7 KieBuilder (org.kie.api.builder.KieBuilder)6 Resource (org.kie.api.io.Resource)6 File (java.io.File)5 DynamicRulesTest (org.drools.compiler.integrationtests.DynamicRulesTest)4 KieModuleModel (org.kie.api.builder.model.KieModuleModel)4 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)4