Search in sources :

Example 36 with KieModule

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

the class SimulateTestBase method createKJarWithMultipleResources.

public static ReleaseId createKJarWithMultipleResources(String id, String[] resources, ResourceType[] types) {
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    KieFileSystem kfs = ks.newKieFileSystem();
    for (int i = 0; i < resources.length; i++) {
        String res = resources[i];
        String type = types[i].getDefaultExtension();
        kfs.write("src/main/resources/" + id.replaceAll("\\.", "/") + "/org/test/res" + i + "." + type, res);
    }
    KieBaseModel kBase1 = kproj.newKieBaseModel(id).setEqualsBehavior(EqualityBehaviorOption.EQUALITY).setEventProcessingMode(EventProcessingOption.STREAM);
    KieSessionModel ksession1 = kBase1.newKieSessionModel(id + ".KSession1").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo"));
    kfs.writeKModuleXML(kproj.toXML());
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    assertTrue(kieBuilder.getResults().getMessages().isEmpty());
    KieModule kieModule = kieBuilder.getKieModule();
    return kieModule.getReleaseId();
}
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) KieBuilder(org.kie.api.builder.KieBuilder) KieModule(org.kie.api.builder.KieModule) KieSessionModel(org.kie.api.builder.model.KieSessionModel)

Example 37 with KieModule

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

the class NegativePatternsTest method prepareKieSession.

@Before
public void prepareKieSession() {
    String drl = "package org.drools.compiler.integrationtests\n" + "\n" + "import org.drools.compiler.integrationtests.NegativePatternsTest.TestEvent\n" + "\n" + "declare TestEvent\n" + "    @role( event )\n" + "    @expires( 22ms )\n" + "end\n" + "\n" + "rule \"SingleAbsence\"\n" + "    duration( 18ms )\n" + "    when\n" + "        not ( TestEvent ( ) from entry-point EventStream )\n" + "    then\n" + "        // consequence\n" + "end\n" + "\n" + "rule \"SingleConstrained\"\n" + "    duration( 18ms )\n" + "    when\n" + "        not ( TestEvent ( name == \"EventA\" ) from entry-point EventStream )\n" + "    then\n" + "        // consequence\n" + "end\n" + "\n" + "rule \"MultipleEvents\"\n" + "    duration( 18ms )\n" + "    when\n" + "        TestEvent ( name == \"EventA\" ) over window:time( 22ms ) from entry-point EventStream\n" + "        not ( TestEvent ( name == \"EventB\" ) over window:time( 22ms )  from entry-point EventStream )\n" + "    then\n" + "        // consequence\n" + "end\n" + "\n" + "rule \"MultipleEntryPoints\"\n" + "    duration( 18ms )\n" + "    when\n" + "        not (\n" + "            TestEvent( name == \"EventA\" ) from entry-point EventStream\n" + "        )\n" + "        not (\n" + "            TestEvent( name == \"EventB\" ) from entry-point OtherStream\n" + "        )\n" + "    then\n" + "        // consequence\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kieModule = ks.newKieModuleModel();
    KieBaseModel defaultBase = kieModule.newKieBaseModel("defaultKBase").setDefault(true).addPackage("*").setEventProcessingMode(EventProcessingOption.STREAM);
    defaultBase.newKieSessionModel(DEFAULT_SESSION_NAME).setClockType(ClockTypeOption.get("pseudo")).setDefault(true);
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    kfs.writeKModuleXML(kieModule.toXML());
    KieModule builtModule = ks.newKieBuilder(kfs).buildAll().getKieModule();
    ks.getRepository().addKieModule(builtModule);
    ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession(DEFAULT_SESSION_NAME);
    firedRulesListener = new TrackingAgendaEventListener();
    ksession.addEventListener(firedRulesListener);
}
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) KieModule(org.kie.api.builder.KieModule) Before(org.junit.Before)

Example 38 with KieModule

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

the class KieBuilderTest method testStatelessSessionDeclarativeChannelRegistration.

@Test
public void testStatelessSessionDeclarativeChannelRegistration() {
    final String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + "         xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "  <kbase name=\"kbase1\">\n" + "    <ksession name=\"ksession1\" default=\"true\" type=\"stateless\">\n" + "       <channels>\n" + "         <channel name=\"testChannel\" type=\"org.drools.compiler.integrationtests.KieBuilderTest$MockChannel\" />\n" + "       </channels>\n" + "    </ksession>" + "  </kbase>\n" + "</kmodule>";
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
    final Resource r1 = ResourceFactory.newByteArrayResource(drl1.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
    final KieModule km = createAndDeployJar(ks, kmodule, releaseId1, r1);
    KieContainer kieContainer = ks.newKieContainer(km.getReleaseId());
    StatelessKieSession statelessKieSession = kieContainer.newStatelessKieSession();
    assertEquals(1, statelessKieSession.getChannels().size());
    assertTrue(statelessKieSession.getChannels().keySet().contains("testChannel"));
}
Also used : Resource(org.kie.api.io.Resource) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieServices(org.kie.api.KieServices) 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 KieModule

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

the class KieCompilationCacheTest method testCacheWigAccumulate.

@Test
public void testCacheWigAccumulate() throws Exception {
    String drl1 = "package org.drools.compiler.integrationtests\n" + "rule R11 when\n" + "   Number() from accumulate(String(), \n" + "              init(int x = 0;)," + "              action(x++;)," + "              reverse(x--;)," + "              result(x))\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "cache-accumulate", "1.0-SNAPSHOT");
    KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).write("src/main/resources/KBase1/org/pkg1/r1.drl", drl1).writeKModuleXML(createKieProjectWithPackagesAnd2KieBases(ks).toXML());
    ks.newKieBuilder(kfs).buildAll();
    InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId);
    byte[] jar = kieModule.getBytes();
    MemoryFileSystem mfs = MemoryFileSystem.readFromJar(jar);
    File file = mfs.getFile(KieBuilderImpl.getCompilationCachePath(releaseId, "KBase1"));
    assertNotNull(file);
    Resource jarRes = ks.getResources().newByteArrayResource(jar);
    KieModule km = ks.getRepository().addKieModule(jarRes);
    KieSession ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
    ksession.insert(new String("Hello World"));
    assertEquals(1, ksession.fireAllRules());
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystem(org.kie.api.builder.KieFileSystem) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) File(org.drools.compiler.compiler.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 40 with KieModule

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

the class KieCompilationCacheTest method testHelloWorldWithPackagesAnd2KieBases.

@Test
public void testHelloWorldWithPackagesAnd2KieBases() throws Exception {
    String drl1 = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getCanonicalName() + "\n" + "rule R11 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule R12 when\n" + "   $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
    String drl2 = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getCanonicalName() + "\n" + "rule R21 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule R22 when\n" + "   $m : Message( message == \"Aloha Earth\" )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "hello-world", "1.0-SNAPSHOT");
    KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).write("src/main/resources/KBase1/org/pkg1/r1.drl", drl1).write("src/main/resources/KBase1/org/pkg2/r2.drl", drl2).writeKModuleXML(createKieProjectWithPackagesAnd2KieBases(ks).toXML());
    ks.newKieBuilder(kfs).buildAll();
    InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId);
    byte[] jar = kieModule.getBytes();
    MemoryFileSystem mfs = MemoryFileSystem.readFromJar(jar);
    File file = mfs.getFile(KieBuilderImpl.getCompilationCachePath(releaseId, "KBase1"));
    assertNotNull(file);
    file = mfs.getFile(KieBuilderImpl.getCompilationCachePath(releaseId, "KBase2"));
    assertNotNull(file);
    Resource jarRes = ks.getResources().newByteArrayResource(jar);
    KieModule km = ks.getRepository().addKieModule(jarRes);
    KieSession ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
    ksession.insert(new Message("Aloha Earth"));
    assertEquals(0, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2");
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(0, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2");
    ksession.insert(new Message("Aloha Earth"));
    assertEquals(1, ksession.fireAllRules());
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) File(org.drools.compiler.compiler.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Aggregations

KieModule (org.kie.api.builder.KieModule)124 KieServices (org.kie.api.KieServices)104 Test (org.junit.Test)94 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)84 ReleaseId (org.kie.api.builder.ReleaseId)84 KieContainer (org.kie.api.runtime.KieContainer)75 KieSession (org.kie.api.runtime.KieSession)64 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)53 Message (org.drools.compiler.Message)25 KieFileSystem (org.kie.api.builder.KieFileSystem)23 Resource (org.kie.api.io.Resource)20 ArrayList (java.util.ArrayList)15 KieModuleModel (org.kie.api.builder.model.KieModuleModel)15 KieBuilder (org.kie.api.builder.KieBuilder)13 KieRepository (org.kie.api.builder.KieRepository)10 MemoryFileSystem (org.drools.compiler.compiler.io.memory.MemoryFileSystem)7 KieBaseModel (org.kie.api.builder.model.KieBaseModel)7 KieBase (org.kie.api.KieBase)6 List (java.util.List)5 Results (org.kie.api.builder.Results)5