Search in sources :

Example 26 with KieBuilder

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();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) Before(org.junit.Before)

Example 27 with KieBuilder

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));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 28 with KieBuilder

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());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) KieBuilder(org.kie.api.builder.KieBuilder) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 29 with KieBuilder

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();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 30 with KieBuilder

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;
}
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) KieSessionModel(org.kie.api.builder.model.KieSessionModel)

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