Search in sources :

Example 81 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class IncrementalCompilationTest method testIncrementalCompilationChangeingParentRule.

@Test
public void testIncrementalCompilationChangeingParentRule() throws Exception {
    // DROOLS-1031
    String drl1_1 = "rule R1 when\n" + "   $s : String( this == \"s1\" )\n" + "then\n" + "end\n";
    String drl1_2 = "rule R1 when\n" + "   $s : String( this == \"s2\" )\n" + "then\n" + "end\n";
    String drl2 = "rule R2 extends R1 when\n" + "   $i : Integer()\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-extends", "1.1.1");
    KieModule km = createAndDeployJar(ks, releaseId1, drl1_1 + drl2);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    ksession.insert(1);
    ksession.insert("s2");
    assertEquals(0, ksession.fireAllRules());
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-extends", "1.1.2");
    km = createAndDeployJar(ks, releaseId2, drl1_2 + drl2);
    kc.updateToVersion(releaseId2);
    assertEquals(2, ksession.fireAllRules());
}
Also used : 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 82 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class IncrementalCompilationTest method testKJarUpgradeDRLWithSpace4_usingSingleQuote.

@Test
public void testKJarUpgradeDRLWithSpace4_usingSingleQuote() throws Exception {
    // DROOLS-1399 quater (using single quote)
    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();
    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 83 with KieSession

use of org.kie.api.runtime.KieSession 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 84 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class IncrementalCompilationTest method testAddRuleWithSlidingWindows.

@Test
public void testAddRuleWithSlidingWindows() throws Exception {
    // DROOLS-2292
    String drl1 = "package org.drools.compiler\n" + "import " + List.class.getCanonicalName() + "\n" + "import " + BooleanEvent.class.getCanonicalName() + "\n" + "rule R1 when\n" + "    $e : BooleanEvent(!enabled)\n" + "    List(size >= 1) from collect ( BooleanEvent(!enabled) over window:time(1) )\n" + "    $toEdit : List() from collect( BooleanEvent(!enabled) over window:time(2) )\n" + "then\n" + "    modify( (BooleanEvent)$toEdit.get(0) ){ setEnabled( true ) }\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.0.0");
    deployJar(ks, createKJar(ks, kproj, releaseId1, null));
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "2.0.0");
    deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl1));
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieSession kieSession = kc.newKieSession();
    kieSession.insert(new BooleanEvent());
    kieSession.fireAllRules();
    kc.updateToVersion(releaseId2);
    kieSession.fireAllRules();
    KieMarshallers marshallers = ks.getMarshallers();
    Marshaller marshaller = marshallers.newMarshaller(kieSession.getKieBase());
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    marshaller.marshall(outputStream, kieSession);
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) Marshaller(org.kie.api.marshalling.Marshaller) 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) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KieSessionModel(org.kie.api.builder.model.KieSessionModel) KieContainer(org.kie.api.runtime.KieContainer) KieMarshallers(org.kie.api.marshalling.KieMarshallers) Test(org.junit.Test)

Example 85 with KieSession

use of org.kie.api.runtime.KieSession 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)

Aggregations

KieSession (org.kie.api.runtime.KieSession)5328 Test (org.junit.Test)4824 KieBase (org.kie.api.KieBase)2414 ArrayList (java.util.ArrayList)2317 List (java.util.List)1105 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)769 FactHandle (org.kie.api.runtime.rule.FactHandle)598 Person (org.drools.modelcompiler.domain.Person)519 HashMap (java.util.HashMap)416 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)415 KieServices (org.kie.api.KieServices)382 KieHelper (org.kie.internal.utils.KieHelper)355 KieContainer (org.kie.api.runtime.KieContainer)298 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)265 InternalFactHandle (org.drools.core.common.InternalFactHandle)259 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)234 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)234 ReleaseId (org.kie.api.builder.ReleaseId)232 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)229 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)207