Search in sources :

Example 71 with KieFileSystem

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

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

the class AccumulateTest method testIncompatibleTypeOnAccumulateFunction.

@Test
public void testIncompatibleTypeOnAccumulateFunction() {
    // DROOLS-1243
    String drl = "import " + MyPerson.class.getCanonicalName() + ";\n" + "import " + BigDecimal.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R when\n" + "  $theFrom : BigDecimal() from accumulate(MyPerson( $val : age ); \n" + "                                          sum( $val ) )\n" + "then\n" + "  list.add($theFrom);\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    Results results = ks.newKieBuilder(kfs).buildAll().getResults();
    assertFalse(results.getMessages().isEmpty());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) QueryResults(org.kie.api.runtime.rule.QueryResults) Results(org.kie.api.builder.Results) KieServices(org.kie.api.KieServices) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 73 with KieFileSystem

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

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

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

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