Search in sources :

Example 76 with KieFileSystem

use of org.kie.api.builder.KieFileSystem 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 77 with KieFileSystem

use of org.kie.api.builder.KieFileSystem 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 78 with KieFileSystem

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

the class CepEspTest method testTemporalEvaluatorsUsingRawDateFields.

@Test
public void testTemporalEvaluatorsUsingRawDateFields() throws InterruptedException {
    // DROOLS-421
    String drl = "\n" + "import java.util.*; " + "global List list; " + "declare Delivery " + "  effectiveDate : Date " + "end " + "declare Config " + "  todate : Date " + "end " + "rule Control " + "when " + "  $dpo: Delivery( $eff : effectiveDate ) " + "  $gCfg: Config( todate == null || todate after[ 0d ] $eff ) " + "then " + "  list.add( 0 ); " + "end " + "rule Init " + "when " + "then " + "  Delivery dpo = new Delivery( new Date( 1000 ) ); " + "  Config gCfg = new Config(); " + "      gCfg.setTodate( new Date( 2000 ) ); " + "  " + "  insert( dpo ); " + "  insert( gCfg ); " + "end " + "";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write(ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("rules.drl"));
    KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
    kbuilder.buildAll();
    assertEquals(0, kbuilder.getResults().getMessages().size());
    KieSession ksession = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieSession();
    assertNotNull(ksession);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    ksession.dispose();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 79 with KieFileSystem

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

the class CepEspTest method testTemporalEvaluatorsUsingSelfDates.

@Test
public void testTemporalEvaluatorsUsingSelfDates() throws InterruptedException {
    // DROOLS-421
    String drl = "\n" + "import java.util.*; " + "global List list; " + "declare Delivery " + "  thisDate : Date " + "  thatDate : Date " + "end " + "rule Init when then insert( new Delivery( new Date( 1000 ), new Date( 200 ) ) ); end " + "rule Control " + "when " + "  Delivery( thisDate == null || thisDate after[ 0d ] thatDate ) " + "then " + "  list.add( 0 ); " + "end " + "";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write(ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("rules.drl"));
    KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
    kbuilder.buildAll();
    assertEquals(0, kbuilder.getResults().getMessages().size());
    KieSession ksession = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieSession();
    assertNotNull(ksession);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    ksession.dispose();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 80 with KieFileSystem

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

the class CepEspTest method testTemporalEvaluatorsUsingRawDateFieldsFromFrom.

@Test
public void testTemporalEvaluatorsUsingRawDateFieldsFromFrom() throws InterruptedException {
    // DROOLS-421
    String drl = "\n" + "import java.util.*; " + "global List list; " + "declare Delivery " + "  effectiveDate : Date " + "end " + "declare Config " + "  todate : Date " + "end " + "rule Control " + "when " + "  $dpo: Delivery( $eff : effectiveDate ) from new Delivery( new Date( 1000 ) ) " + "  $gCfg: Config( todate == null || todate after[ 0d ] $eff ) from new Config( new Date( 2000 ) ) " + "then " + "  list.add( 0 ); " + "end " + "";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write(ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("rules.drl"));
    KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
    kbuilder.buildAll();
    assertEquals(0, kbuilder.getResults().getMessages().size());
    KieSession ksession = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieSession();
    assertNotNull(ksession);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    ksession.dispose();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Aggregations

KieFileSystem (org.kie.api.builder.KieFileSystem)297 KieServices (org.kie.api.KieServices)241 Test (org.junit.Test)211 KieBuilder (org.kie.api.builder.KieBuilder)179 KieSession (org.kie.api.runtime.KieSession)101 KieModuleModel (org.kie.api.builder.model.KieModuleModel)79 KieContainer (org.kie.api.runtime.KieContainer)74 ReleaseId (org.kie.api.builder.ReleaseId)64 Results (org.kie.api.builder.Results)60 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)53 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)39 KieBase (org.kie.api.KieBase)38 IncrementalResults (org.kie.internal.builder.IncrementalResults)36 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)32 KieBaseModel (org.kie.api.builder.model.KieBaseModel)31 ArrayList (java.util.ArrayList)30 Message (org.kie.api.builder.Message)26 KieModule (org.kie.api.builder.KieModule)22 Message (org.drools.compiler.Message)20 Resource (org.kie.api.io.Resource)18