use of org.kie.api.builder.KieBuilder in project drools by kiegroup.
the class LifecycleTest method initSession.
@Before
public void initSession() {
String drlString = "package org.jboss.brms\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick\n" + " @role( event )\n" + " @expires( 4s )\n" + "end\n" + "rule \"TestEventReceived\"\n" + " when\n" + " $event : StockTick() over window:time(4s) from entry-point EventStream\n" + " then\n" + " // do something;\n" + "end";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
final Resource drl = ks.getResources().newByteArrayResource(drlString.getBytes()).setTargetPath("org/jboss/brms/lifecycle.drl");
kfs.write(drl);
KieModuleModel kmoduleModel = ks.newKieModuleModel();
kmoduleModel.newKieBaseModel("defaultKBase").addPackage("*").setEventProcessingMode(EventProcessingOption.STREAM).newKieSessionModel("defaultKieSession").setDefault(true).setClockType(ClockTypeOption.get("pseudo"));
kfs.writeKModuleXML(kmoduleModel.toXML());
KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
assertEquals(0, builder.getResults().getMessages().size());
this.kieSession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
}
use of org.kie.api.builder.KieBuilder in project drools by kiegroup.
the class ExtendsTest method testDeclareExtendsWithFullyQualifiedName.
@Test
public void testDeclareExtendsWithFullyQualifiedName() {
String drl = "package org.drools.extends.test; \n" + "" + "declare org.drools.extends.test.Foo end \n" + "declare org.drools.extends.test.Bar extends org.drools.extends.test.Foo end \n" + "";
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kfs = kieServices.newKieFileSystem();
kfs.write(kieServices.getResources().newByteArrayResource(drl.getBytes()).setSourcePath("test.drl").setResourceType(ResourceType.DRL));
KieBuilder kieBuilder = kieServices.newKieBuilder(kfs);
kieBuilder.buildAll();
assertFalse(kieBuilder.getResults().hasMessages(Message.Level.ERROR));
}
use of org.kie.api.builder.KieBuilder in project drools by kiegroup.
the class Misc2Test method testIsAWith2KContainers.
@Test
public void testIsAWith2KContainers() {
// BZ-996056
String str = "import org.drools.compiler.Person\n" + "\n" + "global java.util.List students\n" + "\n" + "declare trait Student\n" + " school : String\n" + "end\n" + "\n" + "rule \"create student\" \n" + " when\n" + " $student : Person( age < 26, this not isA Student )\n" + " then\n" + " Student s = don( $student, Student.class );\n" + " s.setSchool(\"Masaryk University\");\n" + " update( $student );\n" + "end\n" + "\n" + "rule \"found student\"\n" + " salience 10\n" + " when\n" + " student : Person( this isA Student )\n" + " then\n" + " students.add(student);\n" + "end";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write("src/main/resources/isA.drl", str);
KieBuilder kbuilder = ks.newKieBuilder(kfs);
kbuilder.buildAll();
assertEquals(0, kbuilder.getResults().getMessages().size());
ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).getKieBase();
KieSession ksession = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieSession();
assertNotNull(ksession);
List students = new ArrayList();
ksession.setGlobal("students", students);
ksession.insert(new Person("tom", 20));
ksession.fireAllRules();
assertEquals(1, students.size());
}
use of org.kie.api.builder.KieBuilder in project drools by kiegroup.
the class Misc2Test method testNPEOnMutableGlobal.
@Test
public void testNPEOnMutableGlobal() throws Exception {
// BZ-1019473
String str = "package org.drools.compiler\n" + "global java.util.List context\n" + "rule B\n" + " when\n" + " Message( message == \"b\" )\n" + " $s : String() from context\n" + " then\n" + " System.out.println($s);\n" + "end";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ResourceFactory.newByteArrayResource(str.getBytes()).setTargetPath("org/drools/compiler/rules.drl"));
KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
kbuilder.buildAll();
assertEquals(0, kbuilder.getResults().getMessages().size());
ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).getKieBase();
KieSession ksession = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieSession();
assertNotNull(ksession);
List<String> context = new ArrayList<String>();
ksession.setGlobal("context", context);
FactHandle b = ksession.insert(new Message("b"));
ksession.delete(b);
int fired = ksession.fireAllRules(1);
assertEquals(0, fired);
ksession.dispose();
}
use of org.kie.api.builder.KieBuilder in project drools by kiegroup.
the class ListenersTest method prepareKieModule.
/**
* Inserts a new KieModule containing single KieBase and a stateful and stateless KieSessions with listeners
* into KieRepository.
*
* @return created KIE module ReleaseId
*/
private ReleaseId prepareKieModule() {
final KieServices ks = KieServices.Factory.get();
KieModuleModel module = ks.newKieModuleModel();
KieBaseModel baseModel = module.newKieBaseModel("defaultKBase");
baseModel.setDefault(true);
baseModel.addPackage("*");
KieSessionModel sessionModel = baseModel.newKieSessionModel("defaultKSession");
sessionModel.setDefault(true);
sessionModel.setType(KieSessionModel.KieSessionType.STATEFUL);
sessionModel.newListenerModel(MarkingAgendaEventListener.class.getName(), ListenerModel.Kind.AGENDA_EVENT_LISTENER);
sessionModel.newListenerModel(MarkingRuntimeEventListener.class.getName(), ListenerModel.Kind.RULE_RUNTIME_EVENT_LISTENER);
KieSessionModel statelessSessionModel = baseModel.newKieSessionModel("defaultStatelessKSession");
statelessSessionModel.setDefault(true);
statelessSessionModel.setType(KieSessionModel.KieSessionType.STATELESS);
statelessSessionModel.newListenerModel(MarkingAgendaEventListener.class.getName(), ListenerModel.Kind.AGENDA_EVENT_LISTENER);
statelessSessionModel.newListenerModel(MarkingRuntimeEventListener.class.getName(), ListenerModel.Kind.RULE_RUNTIME_EVENT_LISTENER);
KieFileSystem kfs = ks.newKieFileSystem();
kfs.writeKModuleXML(module.toXML());
kfs.generateAndWritePomXML(RELEASE_ID);
kfs.write("src/main/resources/" + PACKAGE_PATH + "/test.drl", ResourceFactory.newByteArrayResource(DRL.getBytes()));
KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
assertEquals("Unexpected compilation errors", 0, builder.getResults().getMessages().size());
ks.getRepository().addKieModule(builder.getKieModule());
return RELEASE_ID;
}
Aggregations