Search in sources :

Example 86 with KieModuleModel

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

the class IncrementalCompilationTest method createKJarWIthPackages.

private void createKJarWIthPackages(KieServices ks, ReleaseId releaseId1, String... pkgs) {
    String drl1 = "global java.util.List list;\n" + "rule R1 when\n" + "  String()\n" + "then\n" + "  list.add(\"R1\");" + "end\n";
    String drl2 = "global java.util.List list;\n" + "rule R2 when\n" + "  String()\n" + "then\n" + "  list.add(\"R2\");" + "end\n";
    KieModuleModel kproj = ks.newKieModuleModel();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("kbase1").setDefault(true);
    for (String pkg : pkgs) {
        kieBaseModel1.addPackage(pkg);
    }
    KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("ksession1").setDefault(true);
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writeKModuleXML(kproj.toXML());
    kfs.generateAndWritePomXML(releaseId1);
    kfs.write("src/main/resources/pkg1/r1.drl", drl1);
    kfs.write("src/main/resources/pkg2/r2.drl", drl2);
    KieModule km = deployJar(ks, buildKJar(ks, kfs, releaseId1));
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieSessionModel(org.kie.api.builder.model.KieSessionModel)

Example 87 with KieModuleModel

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

the class IncrementalCompilationTest method testRemoveRuleWithNonInitializedPath.

@Test
public void testRemoveRuleWithNonInitializedPath() {
    // DROOLS-1177
    String drl1 = "import " + MyEvent.class.getCanonicalName() + "\n" + "declare MyEvent @role( event ) end\n" + "rule \"RG_TEST_1\"\n" + "    when\n" + "       $dummy: MyEvent (id == 1)\n" + "		$other: MyEvent (this != $dummy)\n" + "    then\n" + "        retract($other);\n" + "end\n" + "rule \"RG_TEST_2\"\n" + "    when\n" + "       $dummy: MyEvent (id == 1)\n" + "    then\n" + "        retract($dummy);\n" + "end\n";
    String drl2 = "import " + MyEvent.class.getCanonicalName() + "\n" + "declare IMyEvent @role( event ) end\n" + "rule \"RG_TEST_2\"\n" + "    when\n" + "       $dummy: MyEvent (id == 1)\n" + "    then\n" + "        retract($dummy);\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);
    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 ksession = kc.newKieSession();
    ksession.insert(new MyEvent(0));
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl2));
    kc.updateToVersion(releaseId2);
}
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 88 with KieModuleModel

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

the class IncrementalCompilationTest method testIncrementalCompilationWithIncludes.

@Test
public void testIncrementalCompilationWithIncludes() throws Exception {
    // DROOLS-462
    String drl1 = "global java.util.List list\n" + "rule R1 when\n" + " $s : String() " + "then\n" + " list.add( \"a\" + $s );" + "end\n";
    String drl2 = "global java.util.List list\n" + "rule R1 when\n" + " $s : String() " + "then\n" + " list.add( \"b\" + $s );" + "end\n";
    ReleaseId releaseId = KieServices.Factory.get().newReleaseId("org.test", "test", "1.0.0-SNAPSHOT");
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").addPackage("org.pkg1");
    kieBaseModel1.newKieSessionModel("KSession1");
    KieBaseModel kieBaseModel2 = kproj.newKieBaseModel("KBase2").addPackage("org.pkg2").addInclude("KBase1");
    kieBaseModel2.newKieSessionModel("KSession2");
    KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).write("src/main/resources/KBase1/org/pkg1/r1.drl", drl1).writeKModuleXML(kproj.toXML());
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    kieBuilder.buildAll();
    assertEquals(0, kieBuilder.getResults().getMessages().size());
    KieContainer kc = ks.newKieContainer(releaseId);
    KieSession ksession = kc.newKieSession("KSession2");
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.insert("Foo");
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("aFoo", list.get(0));
    list.clear();
    kfs.delete("src/main/resources/KBase1/org/pkg1/r1.drl");
    kfs.write("src/main/resources/KBase1/org/pkg1/r2.drl", drl2);
    IncrementalResults results = ((InternalKieBuilder) kieBuilder).incrementalBuild();
    assertEquals(0, results.getAddedMessages().size());
    Results updateResults = kc.updateToVersion(releaseId);
    assertEquals(0, updateResults.getMessages().size());
    ksession.insert("Bar");
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.containsAll(asList("bBar", "bFoo")));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) IncrementalResults(org.kie.internal.builder.IncrementalResults) KieBaseModel(org.kie.api.builder.model.KieBaseModel) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) 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) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 89 with KieModuleModel

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

the class IncrementalCompilationTest method testIncrementalCompilationWithSlidingWindow.

@Test
public void testIncrementalCompilationWithSlidingWindow() {
    // DROOLS-881
    String drl1 = "import " + MyEvent.class.getCanonicalName() + "\n" + "declare MyEvent @role( event ) end\n" + "rule A when\n" + "    Number($number : intValue)\n" + "              from accumulate( MyEvent($id : id) over window:time(10s), sum($id) )\n" + "then\n" + "    System.out.println(\"1. SUM : \" + $number);\n" + "end\n" + "\n" + "rule B when\n" + "    Number($number : intValue)\n" + "              from accumulate( MyEvent($id : id) over window:time(10s), count($id) )\n" + "then\n" + "    System.out.println(\"1. CNT : \" + $number);\n" + "end";
    String drl2 = "import " + MyEvent.class.getCanonicalName() + "\n" + "declare MyEvent @role( event ) end\n" + "rule A when\n" + "    Number($number : intValue)\n" + "              from accumulate( MyEvent($id : id) over window:time(10s), sum($id) )\n" + "then\n" + "    System.out.println(\"2. SUM : \" + $number);\n" + "end\n" + "\n" + "rule B when\n" + "    Number($number : intValue)\n" + "              from accumulate( MyEvent($id : id) over window:time(10s), count($id) )\n" + "then\n" + "    System.out.println(\"2. CNT : \" + $number);\n" + "end";
    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));
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl2));
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieSession ksession = kc.newKieSession();
    PseudoClockScheduler clock = ksession.getSessionClock();
    ksession.insert(new MyEvent(1));
    ksession.fireAllRules();
    clock.advanceTime(7, TimeUnit.SECONDS);
    kc.updateToVersion(releaseId2);
    ksession.fireAllRules();
    clock.advanceTime(7, TimeUnit.SECONDS);
    kc.updateToVersion(releaseId1);
    ksession.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 90 with KieModuleModel

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

the class SeveralKieSessionsTest method prepareKieModule.

/**
 * Inserts a new KieModule containing single KieBase and two KieSession
 * instances into KieRepository.
 *
 * @return created module ReleaseId
 */
private ReleaseId prepareKieModule() {
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId = ks.newReleaseId("org.drools.compiler", "severalKieSessionsTest", "1.0.0");
    KieModuleModel module = ks.newKieModuleModel();
    KieBaseModel baseModel = module.newKieBaseModel("defaultKBase");
    baseModel.setDefault(true);
    baseModel.addPackage("*");
    baseModel.newKieSessionModel("defaultKSession").setDefault(true);
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writeKModuleXML(module.toXML());
    kfs.generateAndWritePomXML(releaseId);
    kfs.write("src/main/resources/" + PACKAGE_PATH + "/" + DRL_FILE_NAME, ResourceFactory.newClassPathResource(DRL_FILE_NAME, this.getClass()));
    KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(0, builder.getResults().getMessages().size());
    ks.getRepository().addKieModule(builder.getKieModule());
    return releaseId;
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder)

Aggregations

KieModuleModel (org.kie.api.builder.model.KieModuleModel)126 KieFileSystem (org.kie.api.builder.KieFileSystem)78 KieServices (org.kie.api.KieServices)76 Test (org.junit.Test)67 KieBuilder (org.kie.api.builder.KieBuilder)56 KieBaseModel (org.kie.api.builder.model.KieBaseModel)55 KieContainer (org.kie.api.runtime.KieContainer)50 ReleaseId (org.kie.api.builder.ReleaseId)38 KieSession (org.kie.api.runtime.KieSession)27 KieSessionModel (org.kie.api.builder.model.KieSessionModel)25 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)18 InternalKieContainer (org.drools.core.impl.InternalKieContainer)14 KieModule (org.kie.api.builder.KieModule)14 KieBase (org.kie.api.KieBase)11 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)10 Results (org.kie.api.builder.Results)10 Resource (org.kie.api.io.Resource)9 ArrayList (java.util.ArrayList)8 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)8 Before (org.junit.Before)7