Search in sources :

Example 31 with KieModuleModel

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

the class CepEspTest method testStreamModeWithSubnetwork.

@Test
public void testStreamModeWithSubnetwork() {
    // BZ-1009348
    String drl = "package org.drools.compiler.integrationtests\n" + "\n" + "declare Event\n" + "  @role(event)\n" + "  name : String\n" + "end\n" + "\n" + "global java.util.List list\n" + "\n" + "rule \"firstRule\"\n" + "\n" + "    when\n" + "        not (\n" + "            $e : Event() over window:length(3)\n" + "            and Event( this == $e ) // test pass when you comment this line\n" + "        )\n" + "    then\n" + "        list.add(\"firstRule\");\n" + "\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write("src/main/resources/notinfusion.drl", drl);
    KieModuleModel kmoduleModel = ks.newKieModuleModel();
    kmoduleModel.newKieBaseModel("KieBase").addPackage("*").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM).newKieSessionModel("KieSession").setDefault(true);
    kfs.writeKModuleXML(kmoduleModel.toXML());
    KieBuilder kbuilder = ks.newKieBuilder(kfs).buildAll();
    List<org.kie.api.builder.Message> res = kbuilder.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR);
    assertEquals(res.toString(), 0, res.size());
    KieSession ksession = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieSession();
    ArrayList<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(1, list.size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 32 with KieModuleModel

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

the class CepEspTest method testUpdateEventThroughEntryPoint.

@Test
public void testUpdateEventThroughEntryPoint() throws Exception {
    String drl = "import org.drools.compiler.integrationtests.CepEspTest.TestEvent\n" + "\n" + "declare TestEvent\n" + "    @role( event )\n" + "    @expires( 4s )\n" + "end\n" + "\n" + "rule \"TestEventReceived\"\n" + "    no-loop\n" + "    when\n" + "        $event : TestEvent ( name != null ) over window:time( 4s ) from entry-point EventStream\n" + "    then\n" + "        // insert( new Message( $event.getValue().toString() ) );\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    KieModuleModel kieModule = ks.newKieModuleModel();
    kieModule.newKieBaseModel("KBase").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM).newKieSessionModel("KSession").setDefault(true);
    kfs.writeKModuleXML(kieModule.toXML());
    kfs.write("src/main/resources/lifecycle.drl", drl);
    KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(0, builder.getResults().getMessages().size());
    KieSession kieSession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    EntryPoint entryPoint = kieSession.getEntryPoint("EventStream");
    TestEvent event = new TestEvent("testEvent1");
    FactHandle handle = entryPoint.insert(event);
    TestEvent event2 = new TestEvent("testEvent2");
    entryPoint.update(handle, event2);
    // make sure the event is in the entry-point
    assertFalse(entryPoint.getObjects().contains(event));
    assertTrue(entryPoint.getObjects().contains(event2));
    assertEquals(entryPoint.getObject(handle), event2);
    kieSession.dispose();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) InternalFactHandle(org.drools.core.common.InternalFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieModuleModel(org.kie.api.builder.model.KieModuleModel) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 33 with KieModuleModel

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

the class CepJavaTypeTest method testJavaTypeAnnotatedWithRole_WindowLength.

@Test
public void testJavaTypeAnnotatedWithRole_WindowLength() {
    String drl = "package org.drools.compiler.integrationtests\n" + "\n" + "import org.drools.compiler.integrationtests.CepJavaTypeTest.Event;\n" + "\n" + "rule \"CEP Window Length\"\n" + "when\n" + "    Event() over window:length (10)\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel module = ks.newKieModuleModel();
    KieBaseModel defaultBase = module.newKieBaseModel("defaultKBase").setDefault(true).addPackage("*");
    defaultBase.newKieSessionModel("defaultKSession").setDefault(true);
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    kfs.writeKModuleXML(module.toXML());
    KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
    assertTrue(builder.getResults().getMessages().isEmpty());
}
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) Test(org.junit.Test)

Example 34 with KieModuleModel

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

the class CepQueryTest method prepare.

@Before
public void prepare() {
    final String drl = "package org.drools.compiler.integrationtests\n" + "import org.drools.compiler.integrationtests.CepQueryTest.TestEvent;\n" + "declare TestEvent\n" + "    @role( event )\n" + "end\n" + "query EventsAfterZeroToNineSeconds\n" + "    $event : TestEvent() from entry-point FirstStream\n" + "    $result : TestEvent( this after [0s, 9s] $event) from entry-point SecondStream\n" + "end\n";
    final KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    KieModuleModel module = ks.newKieModuleModel();
    KieBaseModel baseModel = module.newKieBaseModel("defaultKBase").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    baseModel.newKieSessionModel("defaultKSession").setDefault(true).setClockType(ClockTypeOption.get("pseudo"));
    kfs.writeKModuleXML(module.toXML());
    kfs.write(ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("defaultPkg/query.drl"));
    assertTrue(ks.newKieBuilder(kfs).buildAll().getResults().getMessages().isEmpty());
    ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    clock = ksession.getSessionClock();
    firstEntryPoint = ksession.getEntryPoint("FirstStream");
    secondEntryPoint = ksession.getEntryPoint("SecondStream");
}
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) Before(org.junit.Before)

Example 35 with KieModuleModel

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

the class ClassLoaderTest method testClassLoaderFromPojo.

@Test
public void testClassLoaderFromPojo() throws IOException {
    // DROOLS-1108
    final String source = "package org.drools.testdrl;\n" + "public class MyPojo {\n" + "    public String getUrlPath() {" + "        return getClass().getClassLoader().getResource(\"META-INF/foo.xml\").getPath();\n" + "    }\n" + "}\n";
    final String drl1 = "package org.drools.testdrl;\n" + "import org.drools.testdrl.MyPojo;\n" + "global java.util.List list;\n" + "rule R1 when\n" + "then\n" + "   insert(new MyPojo());\n" + "end\n" + "rule R2 when\n" + "    $m : MyPojo()\n" + "then\n" + "   list.add($m.getUrlPath());\n" + "end\n";
    final String foo = "<xyz/>\n";
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId = ks.newReleaseId("org.kie", "test-cl", "1.0.0");
    final KieFileSystem kfs = ks.newKieFileSystem();
    final KieModuleModel kproj = ks.newKieModuleModel();
    final KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("testKbase").setDefault(true).addPackage("org.drools.testdrl");
    final KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("testKsession").setDefault(true);
    kfs.writeKModuleXML(kproj.toXML());
    kfs.write("src/main/resources/org/drools/testdrl/rules1.drl", drl1);
    kfs.write("src/main/java/org/drools/testdrl/MyPojo.java", source);
    kfs.write("src/main/resources/META-INF/foo.xml", foo);
    kfs.generateAndWritePomXML(releaseId);
    final KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    assertTrue(kieBuilder.buildAll().getResults().getMessages().isEmpty());
    final KieContainer kieContainer = ks.newKieContainer(releaseId);
    final ClassLoader classLoader = kieContainer.getClassLoader();
    final URL url = classLoader.getResource("META-INF/foo.xml");
    assertNotNull(url);
    final KieSession ksession = kieContainer.newKieSession("testKsession");
    final List<URL> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(url.getPath(), list.get(0));
    ksession.dispose();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) URL(java.net.URL) KieSessionModel(org.kie.api.builder.model.KieSessionModel) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

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