Search in sources :

Example 76 with KieSession

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

the class IncrementalCompilationTest method testEventDeclarationInSeparatedDRL.

@Test
public void testEventDeclarationInSeparatedDRL() throws Exception {
    // DROOLS-1241
    String drl1 = "import " + SimpleEvent.class.getCanonicalName() + ";\n" + "declare SimpleEvent\n" + "    @role( event )\n" + "    @timestamp( timestamp )\n" + "    @expires( 2d )\n" + "end\n";
    String drl2 = "import " + SimpleEvent.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R1 when\n" + "    $s:SimpleEvent(code==\"MY_CODE\") over window:time( 1s )\n" + "then\n" + "    list.add(\"MY_CODE\");\n" + "end\n";
    String drl3 = "import " + SimpleEvent.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R2 when\n" + "    $s:SimpleEvent(code==\"YOUR_CODE\") over window:time( 1s )\n" + "then\n" + "    list.add(\"YOUR_CODE\");\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-cep-upgrade", "1.1.1");
    KieModule km = deployJar(ks, createKJar(ks, kproj, releaseId1, null, drl1, drl2));
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.insert(new SimpleEvent("1", "MY_CODE", 0));
    ksession.insert(new SimpleEvent("2", "YOUR_CODE", 0));
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("MY_CODE", list.get(0));
    list.clear();
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-cep-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, drl1, drl2, drl3));
    Results results = kc.updateToVersion(releaseId2);
    assertEquals(0, results.getMessages().size());
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("YOUR_CODE", list.get(0));
}
Also used : ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieSessionModel(org.kie.api.builder.model.KieSessionModel) KieBaseModel(org.kie.api.builder.model.KieBaseModel) IncrementalResults(org.kie.internal.builder.IncrementalResults) Results(org.kie.api.builder.Results) KieModuleModel(org.kie.api.builder.model.KieModuleModel) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) 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 77 with KieSession

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

the class IncrementalCompilationTest method testIncrementalCompilationWithNewEvent.

@Test
public void testIncrementalCompilationWithNewEvent() throws Exception {
    // DROOLS-1395
    String drl1 = "package fr.edf.distribution.brms.common.test\n" + "import " + DummyEvent.class.getCanonicalName() + "\n" + "declare DummyEvent\n" + "    @role( event )\n" + "    @timestamp( eventTimestamp )\n" + "end\n" + "rule \"RG_TEST_1\"\n" + "    when\n" + "       $event: DummyEvent ()\n" + "    then\n" + "        System.out.println(\"RG_TEST_1 fired\");\n" + "        retract($event);\n" + "end";
    String drl2 = "package fr.edf.distribution.brms.common.test\n" + "import " + DummyEvent.class.getCanonicalName() + "\n" + "import " + OtherDummyEvent.class.getCanonicalName() + "\n" + "declare DummyEvent\n" + "    @role( event )\n" + "    @timestamp( eventTimestamp )\n" + "end\n" + "declare OtherDummyEvent\n" + "    @role( event )\n" + "    @timestamp( eventTimestamp )\n" + "end\n" + "rule \"RG_TEST_2\"\n" + "    when\n" + "       $event: DummyEvent ()\n" + "       $other : OtherDummyEvent(id == $event.id, this after $event)\n" + "    then\n" + "        System.out.println(\"RG_TEST_2 fired\");\n" + "        retract($other);\n" + "end\n" + "\n" + "rule \"RG_TEST_1\"\n" + "    when\n" + "       $event: DummyEvent ()\n" + "    then\n" + "        System.out.println(\"RG_TEST_1 fired\");\n" + "        retract($event);\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, drl1));
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieSession kieSession = kc.newKieSession();
    DummyEvent evt = new DummyEvent("evt");
    kieSession.insert(evt);
    assertEquals(1, kieSession.fireAllRules());
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "2.0.0");
    deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl2));
    kc.updateToVersion(releaseId2);
    evt = new DummyEvent("evt");
    kieSession.insert(evt);
    OtherDummyEvent other = new OtherDummyEvent("evt");
    kieSession.insert(other);
    assertEquals(1, kieSession.fireAllRules());
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) 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) KieSessionModel(org.kie.api.builder.model.KieSessionModel) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 78 with KieSession

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

the class IncrementalCompilationTest method testIncrementalCompilationWithTimerNode.

@Test
public void testIncrementalCompilationWithTimerNode() throws Exception {
    // DROOLS-1195
    String drl1 = "package fr.edf.distribution.brms.common.test\n" + "import " + DummyEvent.class.getCanonicalName() + "\n" + "declare DummyEvent\n" + "    @role( event )\n" + "    @timestamp( eventTimestamp )\n" + "end\n" + "rule \"RG_TEST_TIMER\"\n" + "timer (int: 0 1; start=$expirationTimestamp , repeat-limit=0 )\n" + "    when\n" + "       $dummy: DummyEvent (id == 'timer', $expirationTimestamp : systemTimestamp )\n" + "    then\n " + "System.out.println(\"1\");\n" + "end\n";
    String drl2 = "package fr.edf.distribution.brms.common.test\n" + "import " + DummyEvent.class.getCanonicalName() + "\n" + "declare DummyEvent\n" + "    @role( event )\n" + "    @timestamp( eventTimestamp )\n" + "end\n" + "rule \"RG_TEST_TIMER_NEW\"\n" + "timer (int: 0 1; start=$expirationTimestamp , repeat-limit=0 )\n" + "    when\n" + "       $dummy: DummyEvent (id == 'timer', $expirationTimestamp : systemTimestamp )\n" + "		DummyEvent (id == 'timer_match')\n" + "    then\n " + "System.out.println(\"1\");\n" + "end\n" + "rule \"RG_OTHER_RULE\"\n" + "    when\n" + "       $dummy: DummyEvent ( id == 'timer' )\n" + "    then\n " + "System.out.println(\"2\");\n" + "end\n";
    long now = System.currentTimeMillis();
    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, drl1));
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieSession kieSession = kc.newKieSession();
    DummyEvent dummyEvent = new DummyEvent();
    dummyEvent.setId("timer");
    dummyEvent.setEventTimestamp(now);
    dummyEvent.setSystemTimestamp(now + TimeUnit.HOURS.toMillis(1));
    DummyEvent other = new DummyEvent();
    other.setId("timer_match");
    other.setEventTimestamp(now);
    kieSession.insert(dummyEvent);
    kieSession.insert(other);
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "2.0.0");
    deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl2));
    kc.updateToVersion(releaseId2);
    PseudoClockScheduler scheduler = kieSession.getSessionClock();
    scheduler.setStartupTime(now);
    scheduler.advanceTime(1, TimeUnit.DAYS);
    assertEquals(2, kieSession.fireAllRules());
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) 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) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) KieSessionModel(org.kie.api.builder.model.KieSessionModel) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 79 with KieSession

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

the class IncrementalCompilationTest method testKJarUpgradeDRLWithSpace2_usingSingleQuote.

@Test
public void testKJarUpgradeDRLWithSpace2_usingSingleQuote() throws Exception {
    // DROOLS-1399 bis (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 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 80 with KieSession

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

the class IncrementalCompilationTest method testKJarUpgradeWithSpace_usingSingleQuote.

@Test
public void testKJarUpgradeWithSpace_usingSingleQuote() throws Exception {
    // DROOLS-1399 (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 = // <<- 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());
}
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