Search in sources :

Example 41 with KieBuilder

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

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

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

use of org.kie.api.builder.KieBuilder 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)

Example 45 with KieBuilder

use of org.kie.api.builder.KieBuilder 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)

Aggregations

KieBuilder (org.kie.api.builder.KieBuilder)211 KieFileSystem (org.kie.api.builder.KieFileSystem)179 KieServices (org.kie.api.KieServices)145 Test (org.junit.Test)134 KieSession (org.kie.api.runtime.KieSession)59 KieModuleModel (org.kie.api.builder.model.KieModuleModel)57 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)54 KieContainer (org.kie.api.runtime.KieContainer)48 IncrementalResults (org.kie.internal.builder.IncrementalResults)35 ReleaseId (org.kie.api.builder.ReleaseId)34 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)33 Results (org.kie.api.builder.Results)32 Message (org.kie.api.builder.Message)30 KieBase (org.kie.api.KieBase)29 ArrayList (java.util.ArrayList)22 KieBaseModel (org.kie.api.builder.model.KieBaseModel)22 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)21 Resource (org.kie.api.io.Resource)19 FactType (org.kie.api.definition.type.FactType)16 KieModule (org.kie.api.builder.KieModule)12