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);
}
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());
}
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();
}
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) {
}
}
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());
}
Aggregations