Search in sources :

Example 66 with KieContainer

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

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

Example 68 with KieContainer

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

the class IncrementalCompilationTest method testIncrementalCompilationWithRedeclares.

@Test
public void testIncrementalCompilationWithRedeclares() {
    // DROOLS-363
    String drl1 = "package org.drools.compiler\n" + "global java.util.List list\n" + "" + "declare Fooz id : int end \n" + "" + "rule R0 when then insert( new Fooz( 1 ) ); end \n" + "" + "";
    String drl2 = "package org.drools.compiler\n" + "global java.util.List list\n" + "" + "declare Fooz id : int end \n" + "" + "declare Barz end \n" + "" + "rule R2 when then insert( new Fooz( 2 ) ); end \n" + "" + "rule R1 when\n" + " $f : Fooz() " + "then\n" + " list.add( $f.getId() );" + " System.out.println( \"Foo in \" + $f + \" >> \" + System.identityHashCode( $f.getClass() ) ); \n" + "end\n" + "";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    createAndDeployJar(ks, releaseId1, drl1);
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieSession ksession = kc.newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    createAndDeployJar(ks, releaseId2, drl1, drl2);
    Results updateResults = kc.updateToVersion(releaseId2);
    assertEquals(0, updateResults.getMessages().size());
    ksession.fireAllRules();
    assertEquals(2, list.size());
}
Also used : IncrementalResults(org.kie.internal.builder.IncrementalResults) Results(org.kie.api.builder.Results) 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) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 69 with KieContainer

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

the class IncrementalCompilationTest method testPropertyReactivityOfAnOriginallyUnknownClass.

@Test
public void testPropertyReactivityOfAnOriginallyUnknownClass() {
    // DROOLS-1684
    String drl1 = "import " + TypeA.class.getCanonicalName() + "\n" + "rule \"RULE_1\"\n" + "    when\n" + "        TypeA()" + "    then\n" + "end\n";
    String drl2 = "import " + TypeB.class.getCanonicalName() + "\n" + "rule \"RULE_2\"\n" + "    when\n" + "        $b : TypeB() @watch(!*)" + "    then\n" + "        modify($b) { setValue(0) } \n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId1, drl1);
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieSession ksession = kc.newKieSession();
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    createAndDeployJar(ks, releaseId2, drl2);
    kc.updateToVersion(releaseId2);
    ksession.insert(new TypeB(1));
    int fired = ksession.fireAllRules(10);
    assertEquals(1, fired);
}
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 70 with KieContainer

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

the class IncrementalCompilationTest method testKJarUpgradeSameSessionRemovingGlobal.

@Test
public void testKJarUpgradeSameSessionRemovingGlobal() throws Exception {
    // DROOLS-752
    String drl1 = "package org.drools.compiler\n" + "global java.lang.String foo\n" + "global java.lang.String bar\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    String drl2 = "package org.drools.compiler\n" + "global java.lang.String foo\n" + "global java.lang.String baz\n" + "rule R2 when\n" + "   $m : Message( )\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, drl1);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    ksession.setGlobal("foo", "foo");
    ksession.setGlobal("bar", "bar");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    km = createAndDeployJar(ks, releaseId2, drl2);
    kc.updateToVersion(releaseId2);
    ksession.setGlobal("baz", "baz");
    Globals globals = ksession.getGlobals();
    assertEquals(2, globals.getGlobalKeys().size());
    assertEquals("foo", ksession.getGlobal("foo"));
    assertNull(ksession.getGlobal("bar"));
    assertEquals("baz", ksession.getGlobal("baz"));
}
Also used : Globals(org.kie.api.runtime.Globals) 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

KieContainer (org.kie.api.runtime.KieContainer)357 KieServices (org.kie.api.KieServices)271 Test (org.junit.Test)246 KieSession (org.kie.api.runtime.KieSession)191 ReleaseId (org.kie.api.builder.ReleaseId)185 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)108 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)90 KieFileSystem (org.kie.api.builder.KieFileSystem)81 KieModule (org.kie.api.builder.KieModule)75 KieBuilder (org.kie.api.builder.KieBuilder)56 KieModuleModel (org.kie.api.builder.model.KieModuleModel)54 KieBase (org.kie.api.KieBase)49 ArrayList (java.util.ArrayList)44 Results (org.kie.api.builder.Results)40 Resource (org.kie.api.io.Resource)29 KieMavenRepository.getKieMavenRepository (org.kie.scanner.KieMavenRepository.getKieMavenRepository)29 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)27 Message (org.drools.compiler.Message)26 KieBaseModel (org.kie.api.builder.model.KieBaseModel)26 InternalKieContainer (org.drools.core.impl.InternalKieContainer)25