Search in sources :

Example 21 with KieModule

use of org.kie.api.builder.KieModule in project drools by kiegroup.

the class IncrementalCompilationTest method testKJarUpgradeDRLWithSpace3.

@Test
public void testKJarUpgradeDRLWithSpace3() throws Exception {
    // DROOLS-1399 ter
    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)

Example 22 with KieModule

use of org.kie.api.builder.KieModule in project drools by kiegroup.

the class IncrementalCompilationTest method testUpdateWithNewDrlAndChangeInOldOne.

@Test
public void testUpdateWithNewDrlAndChangeInOldOne() throws Exception {
    // BZ-1275378
    String drl1 = "package org.kie.test\n" + "global java.util.List list\n" + "rule rule1\n" + "when\n" + "then\n" + "list.add( drools.getRule().getName() );\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.1");
    KieModule km = createAndDeployJar(ks, releaseId1, drl1);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertTrue(list.contains("rule1"));
    drl1 = "package org.kie.test\n" + "global java.util.List list\n" + "rule rule1\n" + "when\n" + "Object()\n" + "then\n" + "list.add( drools.getRule().getName() );\n" + "end\n";
    String drl2 = "package org.kie.test\n" + "global java.util.List list\n" + "rule rule2\n" + "when\n" + "then\n" + "list.add( drools.getRule().getName() );\n" + "end\n";
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.2");
    km = createAndDeployJar(ks, releaseId2, drl1, drl2);
    kc.updateToVersion(releaseId2);
    list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertTrue(list.contains("rule2"));
}
Also used : ArrayList(java.util.ArrayList) 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 23 with KieModule

use of org.kie.api.builder.KieModule in project drools by kiegroup.

the class IncrementalCompilationTest method testKJarUpgradeDRLWithSpace2.

@Test
public void testKJarUpgradeDRLWithSpace2() throws Exception {
    // DROOLS-1399 bis
    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 = "package org.drools.compiler\n" + "rule Rx when\n" + // <<- notice the EXTRA SPACE is the only change in this other version.
    "   $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());
}
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 24 with KieModule

use of org.kie.api.builder.KieModule in project drools by kiegroup.

the class IncrementalCompilationTest method testKJarUpgradeDRLWithSpace5.

@Test
public void testKJarUpgradeDRLWithSpace5() throws Exception {
    // DROOLS-1399 quinquies
    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 = "package org.drools.compiler\n" + "rule Rx when\n" + // <<- notice the EXTRA SPACE typo was removed
    "   $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();
    // <<- notice the ' character
    ksession.insert(new Message("Hello' World"));
    assertEquals(0, 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(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 25 with KieModule

use of org.kie.api.builder.KieModule in project drools by kiegroup.

the class IncrementalCompilationTest method testDrlRenamingWithEvents.

@Test
public void testDrlRenamingWithEvents() throws Exception {
    // DROOLS-965
    String drl1 = "import " + SimpleEvent.class.getCanonicalName() + ";\n" + "\n" + "global java.util.concurrent.atomic.AtomicInteger counter1;\n" + "global java.util.concurrent.atomic.AtomicInteger counter2;\n" + "\n" + "declare SimpleEvent\n" + "    @role( event )\n" + "    @timestamp( timestamp )\n" + "    @expires( 2d )\n" + "end\n" + "\n" + "rule R1 when\n" + "    $s:SimpleEvent(code==\"MY_CODE\")\n" + "then\n" + "    counter1.incrementAndGet();\n" + "end\n" + "\n" + "rule R2 when\n" + "    $s:SimpleEvent(code==\"MY_CODE\")\n" + "    not SimpleEvent(this != $s, this after [0,10s] $s)\n" + "then\n" + "    counter2.incrementAndGet();\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("KSession1").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.1");
    KieModule km = deployJar(ks, createKJar(ks, kproj, releaseId1, null, drl1));
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    PseudoClockScheduler clock = ksession.getSessionClock();
    AtomicInteger counter1 = new AtomicInteger(0);
    AtomicInteger counter2 = new AtomicInteger(0);
    ksession.setGlobal("counter1", counter1);
    ksession.setGlobal("counter2", counter2);
    ksession.insert(new SimpleEvent("1", "MY_CODE", 0));
    ksession.fireAllRules();
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.insert(new SimpleEvent("2", "MY_CODE", 5));
    ksession.fireAllRules();
    assertEquals(2, counter1.get());
    assertEquals(0, counter2.get());
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.2");
    // the null drl placeholder is used to have the same drl with a different file name
    // this causes the removal and readdition of both rules
    km = deployJar(ks, createKJar(ks, kproj, releaseId2, null, (String) null, drl1));
    kc.updateToVersion(releaseId2);
    clock = ksession.getSessionClock();
    clock.advanceTime(16, TimeUnit.SECONDS);
    ksession.insert(new SimpleEvent("3", "MY_CODE", 21));
    ksession.fireAllRules();
    assertEquals(5, counter1.get());
    assertEquals(1, counter2.get());
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieModuleModel(org.kie.api.builder.model.KieModuleModel) 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) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) KieSessionModel(org.kie.api.builder.model.KieSessionModel) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

KieModule (org.kie.api.builder.KieModule)124 KieServices (org.kie.api.KieServices)104 Test (org.junit.Test)94 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)84 ReleaseId (org.kie.api.builder.ReleaseId)84 KieContainer (org.kie.api.runtime.KieContainer)75 KieSession (org.kie.api.runtime.KieSession)64 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)53 Message (org.drools.compiler.Message)25 KieFileSystem (org.kie.api.builder.KieFileSystem)23 Resource (org.kie.api.io.Resource)20 ArrayList (java.util.ArrayList)15 KieModuleModel (org.kie.api.builder.model.KieModuleModel)15 KieBuilder (org.kie.api.builder.KieBuilder)13 KieRepository (org.kie.api.builder.KieRepository)10 MemoryFileSystem (org.drools.compiler.compiler.io.memory.MemoryFileSystem)7 KieBaseModel (org.kie.api.builder.model.KieBaseModel)7 KieBase (org.kie.api.KieBase)6 List (java.util.List)5 Results (org.kie.api.builder.Results)5