Search in sources :

Example 36 with KieBuilder

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

the class KieBuilderTest method testSetPomModelReuse.

@Test
public void testSetPomModelReuse() throws IOException {
    String namespace = "org.kie.test";
    ReleaseId releaseId = KieServices.Factory.get().newReleaseId(namespace, "pomModelReuse", "1.0-SNAPSHOT");
    String pom = KieBuilderImpl.generatePomXml(releaseId);
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    kfs.writePomXML(pom);
    // Create a KieBuilder instance
    KieBuilder kieBuilder1 = createKieBuilder(kfs);
    kieBuilder1.buildAll();
    // Get PomModel to re-use in second KieBuilder instance
    PomModel pomModel = ((KieBuilderImpl) kieBuilder1).getPomModel();
    kfs.writePomXML(pom);
    // Create another KieBuilder instance with the same KieFileSystem, setting PomModel
    KieBuilder kieBuilder2 = createKieBuilder(kfs);
    ((KieBuilderImpl) kieBuilder2).setPomModel(pomModel);
    kieBuilder2.buildAll();
    // Read pom.xml from first KieBuilder's KieModule
    InternalKieModule kieModule1 = (InternalKieModule) ((KieBuilderImpl) kieBuilder1).getKieModuleIgnoringErrors();
    final Reader reader1 = kieModule1.getResource("META-INF/maven/org.kie.test/pomModelReuse/pom.xml").getReader();
    int charCode;
    String readPom1 = "";
    while ((charCode = reader1.read()) != -1) {
        readPom1 = readPom1 + (char) charCode;
    }
    reader1.close();
    assertEquals(pom, readPom1);
    // Read pom.xml from second KieBuilder's KieModule
    InternalKieModule kieModule2 = (InternalKieModule) ((KieBuilderImpl) kieBuilder2).getKieModuleIgnoringErrors();
    final Reader reader2 = kieModule2.getResource("META-INF/maven/org.kie.test/pomModelReuse/pom.xml").getReader();
    String readPom2 = "";
    while ((charCode = reader2.read()) != -1) {
        readPom2 = readPom2 + (char) charCode;
    }
    reader1.close();
    assertEquals(pom, readPom2);
}
Also used : KieBuilderImpl(org.drools.compiler.kie.builder.impl.KieBuilderImpl) KieFileSystem(org.kie.api.builder.KieFileSystem) PomModel(org.appformer.maven.support.PomModel) Reader(java.io.Reader) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 37 with KieBuilder

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

the class CalendarTest method test.

@Test
public void test() {
    // BZ-1007385
    String drl = "package org.drools.compiler.integrationtests;\n" + "\n" + "global java.util.List list\n" + " \n" + "rule \"weekend\"\n" + "    calendars \"weekend\"\n" + "    \n" + "    when\n" + "    then\n" + "        list.add(\"weekend\");\n" + "end\n" + " \n" + "rule \"weekday\"\n" + "    calendars \"weekday\"\n" + "\n" + "    when\n" + "    then\n" + "       list.add(\"weekday\");\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write("src/main/resources/kbase1/calendars.drl", drl);
    KieBuilder kbuilder = ks.newKieBuilder(kfs);
    kbuilder.buildAll();
    List<Message> res = kbuilder.getResults().getMessages(Level.ERROR);
    assertEquals(res.toString(), 0, res.size());
    KieBase kbase = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).getKieBase();
    KieSession ksession = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieSession();
    ArrayList<String> list = new ArrayList<String>();
    ksession.getCalendars().set("weekend", WEEKEND);
    ksession.getCalendars().set("weekday", WEEKDAY);
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    ksession.dispose();
    assertEquals(1, list.size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.kie.api.builder.Message) KieBase(org.kie.api.KieBase) 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 38 with KieBuilder

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

the class CepEspTest method testTemporalEvaluatorsWithEventsFromNode.

@Test
public void testTemporalEvaluatorsWithEventsFromNode() throws InterruptedException {
    // DROOLS-421
    String drl = "\n" + "import java.util.*; " + "global List list; " + "declare Delivery " + "  @role( event ) @timestamp( effectiveDate ) " + "  effectiveDate : Date " + "  configs : List " + "end " + "declare Config " + "  @role( event ) @timestamp( todate ) " + "  todate : Date " + "end " + "rule Control " + "when " + "  $dpo: Delivery() " + "  $gCfg: Config( todate == null || this after[ 0d ] $dpo ) from $dpo.configs " + "then " + "  list.add( 0 ); " + "end " + "rule Init " + "when " + "then " + "  Delivery dpo = new Delivery( new Date(), new ArrayList() ); " + "  Config gCfg = new Config(); " + "      gCfg.setTodate( new Date( new Date().getTime() + 1000 ) ); " + "  dpo.getConfigs().add( gCfg ); " + "  " + "  insert( dpo ); " + "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 39 with KieBuilder

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

the class CepEspTest method testThrowsWhenCreatingKieBaseUsingWindowsInCloudMode.

@Test
public void testThrowsWhenCreatingKieBaseUsingWindowsInCloudMode() {
    String drl = "declare TestEvent\n" + "    @role( event )\n" + "    name : String\n" + "end\n" + "\n" + "rule R when\n" + "        TestEvent ( name == \"EventA\" ) over window:time( 1s ) from entry-point EventStream\n" + "    then\n" + "        // consequence\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kieModule = ks.newKieModuleModel();
    KieBaseModel defaultBase = kieModule.newKieBaseModel("KBase").setDefault(true).addPackage("*").setEventProcessingMode(EventProcessingOption.CLOUD);
    defaultBase.newKieSessionModel("KSession").setClockType(ClockTypeOption.get("pseudo")).setDefault(true);
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    kfs.writeKModuleXML(kieModule.toXML());
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    KieContainer kieContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    try {
        kieContainer.getKieBase("KBase");
        fail("Should throw a RuntimeException because the CLOUD kbase is trying to use features only available in STREAM mode");
    } catch (Exception e) {
    }
}
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) IOException(java.io.IOException) ParseException(java.text.ParseException) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 40 with KieBuilder

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

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