Search in sources :

Example 36 with KieContainer

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

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

the class IncrementalCompilationTest method testIncrementalCompilationWithAmbiguousRedeclares.

@Test
public void testIncrementalCompilationWithAmbiguousRedeclares() {
    String drl1 = "package domestic; " + "import foreign.*; " + "declare foreign.Score " + "    id       : String " + "end ";
    String drl2 = "\n" + "package domestic; " + "import foreign.*; " + "declare foreign.Score " + "    id       : String " + "end\n" + "declare Score " + "    value : double " + "end " + "";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    ReleaseId id = ks.newReleaseId("org.test", "foo", "1.0-SNAPSHOT");
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    kfs.generateAndWritePomXML(id);
    kfs.write(ks.getResources().newReaderResource(new StringReader(drl1)).setResourceType(ResourceType.DRL).setSourcePath("drl1.drl"));
    kieBuilder.buildAll();
    KieContainer kc = ks.newKieContainer(id);
    KieSession ksession = kc.newKieSession();
    ksession.fireAllRules();
    kfs.write(ks.getResources().newReaderResource(new StringReader(drl2)).setResourceType(ResourceType.DRL).setSourcePath("drl2.drl"));
    IncrementalResults results = ((InternalKieBuilder) kieBuilder).incrementalBuild();
    System.out.println(results.getAddedMessages());
    assertEquals(0, results.getAddedMessages().size());
    Results updateResults = kc.updateToVersion(id);
    assertEquals(0, updateResults.getMessages().size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) IncrementalResults(org.kie.internal.builder.IncrementalResults) Results(org.kie.api.builder.Results) StringReader(java.io.StringReader) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) IncrementalResults(org.kie.internal.builder.IncrementalResults) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 38 with KieContainer

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

the class IncrementalCompilationTest method testIncrementalTypeDeclarationOnInterface.

@Test
public void testIncrementalTypeDeclarationOnInterface() {
    // DROOLS-861
    String drl1 = "import " + Service.class.getCanonicalName() + "\n" + "rule A when\n" + "    Service( )\n" + "then\n" + "end";
    String drl2 = "import " + Service.class.getCanonicalName() + "\n" + "declare Service @role( event ) end\n" + "rule A when\n" + "    Service( )\n" + "then\n" + "end";
    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);
}
Also used : CompletionService(java.util.concurrent.CompletionService) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) ExecutorService(java.util.concurrent.ExecutorService) Service(org.kie.api.Service) 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 39 with KieContainer

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

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

the class IncrementalCompilationTest method testIncremenatalCompilationAddingFieldToDeclaredType.

@Test
public void testIncremenatalCompilationAddingFieldToDeclaredType() {
    // DROOLS-2197
    String declares1 = "declare Address\n" + "   streetName : String\n" + "   city : String\n" + "end";
    String declares2 = "declare Address\n" + "   streetName : String\n" + "   city : String\n" + "   flg: String\n" + "end";
    String rules1 = "rule R when\n" + "    a : Address( city == \"Antibes\" )\n" + "then\n" + "    a.setStreetName(\"Av. Jean Medecin\");\n" + "end";
    String rules2 = "rule R when\n" + "    a : Address( city == \" Paris \" )\n" + "then\n" + "    a.setStreetName(\" Champs Elisees \");\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    ReleaseId id = ks.newReleaseId("org.test", "foo", "1.0-SNAPSHOT");
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    kfs.generateAndWritePomXML(id);
    kfs.write(ks.getResources().newReaderResource(new StringReader(declares1)).setResourceType(ResourceType.DRL).setSourcePath("declares.drl"));
    kfs.write(ks.getResources().newReaderResource(new StringReader(rules1)).setResourceType(ResourceType.DRL).setSourcePath("rules.drl"));
    kieBuilder.buildAll();
    KieContainer kc = ks.newKieContainer(id);
    KieSession ksession = kc.newKieSession();
    ksession.fireAllRules();
    kfs.write(ks.getResources().newReaderResource(new StringReader(declares2)).setResourceType(ResourceType.DRL).setSourcePath("declares.drl"));
    kfs.write(ks.getResources().newReaderResource(new StringReader(rules2)).setResourceType(ResourceType.DRL).setSourcePath("rules.drl"));
    IncrementalResults results = ((InternalKieBuilder) kieBuilder).incrementalBuild();
    System.out.println(results.getAddedMessages());
    assertEquals(0, results.getAddedMessages().size());
    Results updateResults = kc.updateToVersion(id);
    assertEquals(0, updateResults.getMessages().size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) IncrementalResults(org.kie.internal.builder.IncrementalResults) Results(org.kie.api.builder.Results) StringReader(java.io.StringReader) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) IncrementalResults(org.kie.internal.builder.IncrementalResults) 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