Search in sources :

Example 46 with KieModuleModel

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

the class KieContainerDefaultsTest method testTwoStatelessKieSessionsDefaultNotSet.

/**
 * This test checks how StatelessKieSessions behave when default state isn't explicitly set.
 */
@Test
public void testTwoStatelessKieSessionsDefaultNotSet() {
    final KieModuleModel kieModule = kieServices.newKieModuleModel();
    kieModule.newKieBaseModel("firstKBase").setDefault(true).newKieSessionModel("firstKSession").setType(KieSessionModel.KieSessionType.STATELESS);
    kieModule.newKieBaseModel("secondKBase").setDefault(true).newKieSessionModel("secondKSession").setType(KieSessionModel.KieSessionType.STATELESS);
    buildKieModule(kieModule);
    final KieContainer kieContainer = kieServices.newKieContainer(RELEASE_ID);
    Assertions.assertThatThrownBy(() -> kieContainer.newStatelessKieSession()).isInstanceOf(RuntimeException.class).hasMessageContaining("Cannot find a default StatelessKieSession");
}
Also used : KieModuleModel(org.kie.api.builder.model.KieModuleModel) InternalKieContainer(org.drools.core.impl.InternalKieContainer) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 47 with KieModuleModel

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

the class KieContainerDefaultsTest method testTwoStatelessKieSessionsOneDefault.

/**
 * This test checks if default StatelessKieSessions behave as expected.
 */
@Test
public void testTwoStatelessKieSessionsOneDefault() {
    final KieModuleModel kieModule = kieServices.newKieModuleModel();
    kieModule.newKieBaseModel("firstKBase").setDefault(true).newKieSessionModel("firstKSession").setType(KieSessionModel.KieSessionType.STATELESS).setDefault(true);
    kieModule.newKieBaseModel("secondKBase").setDefault(true).newKieSessionModel("secondKSession").setType(KieSessionModel.KieSessionType.STATELESS).setDefault(false);
    buildKieModule(kieModule);
    final KieContainer kieContainer = kieServices.newKieContainer(RELEASE_ID);
    final StatelessKieSession firstKSession = kieContainer.newStatelessKieSession("firstKSession");
    final StatelessKieSession secondKSession = kieContainer.newStatelessKieSession("secondKSession");
    Assertions.assertThat(firstKSession).isEqualTo(((InternalKieContainer) kieContainer).getStatelessKieSession());
    Assertions.assertThat(secondKSession).isNotEqualTo(((InternalKieContainer) kieContainer).getStatelessKieSession());
}
Also used : KieModuleModel(org.kie.api.builder.model.KieModuleModel) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) InternalKieContainer(org.drools.core.impl.InternalKieContainer) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 48 with KieModuleModel

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

the class KieModuleModelExample method go.

public void go(PrintStream out) {
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    Resource ex1Res = ks.getResources().newFileSystemResource(getFile("named-kiesession"));
    Resource ex2Res = ks.getResources().newFileSystemResource(getFile("kiebase-inclusion"));
    ReleaseId rid = ks.newReleaseId("org.drools", "kiemodulemodel-example", Drools.getFullVersion());
    kfs.generateAndWritePomXML(rid);
    KieModuleModel kModuleModel = ks.newKieModuleModel();
    kModuleModel.newKieBaseModel("kiemodulemodel").addInclude("kbase1").addInclude("kbase2").newKieSessionModel("ksession6");
    kfs.writeKModuleXML(kModuleModel.toXML());
    kfs.write("src/main/resources/kiemodulemodel/HAL6.drl", getRule());
    KieBuilder kb = ks.newKieBuilder(kfs);
    kb.setDependencies(ex1Res, ex2Res);
    // kieModule is automatically deployed to KieRepository if successfully built.
    kb.buildAll();
    if (kb.getResults().hasMessages(Level.ERROR)) {
        throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
    }
    KieContainer kContainer = ks.newKieContainer(rid);
    KieSession kSession = kContainer.newKieSession("ksession6");
    kSession.setGlobal("out", out);
    Object msg1 = createMessage(kContainer, "Dave", "Hello, HAL. Do you read me, HAL?");
    kSession.insert(msg1);
    kSession.fireAllRules();
    Object msg2 = createMessage(kContainer, "Dave", "Open the pod bay doors, HAL.");
    kSession.insert(msg2);
    kSession.fireAllRules();
    Object msg3 = createMessage(kContainer, "Dave", "What's the problem?");
    kSession.insert(msg3);
    kSession.fireAllRules();
}
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) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer)

Example 49 with KieModuleModel

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

the class KieContainerTest method createKieModule.

/**
 * Helper method creating simple KieModule with given ReleaseId.
 */
private void createKieModule(final ReleaseId releaseId) {
    final KieModuleModel kmodule = kieServices.newKieModuleModel();
    kmodule.newKieBaseModel("defaultKBase").setDefault(true).newKieSessionModel(SESSION_NAME);
    final KieFileSystem kfs = kieServices.newKieFileSystem();
    kfs.generateAndWritePomXML(releaseId);
    kfs.writeKModuleXML(kmodule.toXML());
    kfs.write("src/main/resources/defaultKBase/test.drl", kieServices.getResources().newByteArrayResource(DRL.getBytes()));
    final KieBuilder kbuilder = kieServices.newKieBuilder(kfs);
    kbuilder.buildAll();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBuilder(org.kie.api.builder.KieBuilder)

Example 50 with KieModuleModel

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

the class DMNExtensionRegisterTest method testUsingKModuleProperty_WrongClasses.

@Test
public void testUsingKModuleProperty_WrongClasses() {
    final KieServices ks = KieServices.Factory.get();
    final KieFileSystem kfs = ks.newKieFileSystem();
    KieModuleModel kmm = ks.newKieModuleModel();
    kmm.setConfigurationProperty("org.kie.dmn.profiles.FirstNameLastNameProfile", "foo");
    kfs.writeKModuleXML(kmm.toXML());
    kfs.write(ks.getResources().newClassPathResource("0001-input-data-string-with-extensions.dmn", this.getClass()));
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    Results results = kieBuilder.getResults();
    LOG.info("buildAll() completed.");
    results.getMessages(Level.WARNING).forEach(e -> LOG.warn("{}", e));
    assertTrue(results.getMessages(Level.WARNING).size() > 0);
    final KieContainer kieContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    DMNRuntime runtime = kieContainer.newKieSession().getKieRuntime(DMNRuntime.class);
    DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/kie-dmn", "0001-input-data-string");
    assertThat(dmnModel, notNullValue());
    assertThat(formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    assertEquals(3, dmnModel.getDefinitions().getDrgElement().size());
    InputData inputData1 = (InputData) dmnModel.getDefinitions().getDrgElement().get(1);
    assertEquals("First Name", inputData1.getName());
    DMNElement.ExtensionElements id1elements = inputData1.getExtensionElements();
    assertTrue(id1elements != null);
    assertEquals(0, id1elements.getAny().size());
    InputData inputData2 = (InputData) dmnModel.getDefinitions().getDrgElement().get(2);
    assertEquals("Last Name", inputData2.getName());
    DMNElement.ExtensionElements id2elements = inputData2.getExtensionElements();
    assertTrue(id2elements != null);
    assertEquals(0, id2elements.getAny().size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Results(org.kie.api.builder.Results) KieModuleModel(org.kie.api.builder.model.KieModuleModel) DMNElement(org.kie.dmn.model.v1_1.DMNElement) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) InputData(org.kie.dmn.model.v1_1.InputData) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

KieModuleModel (org.kie.api.builder.model.KieModuleModel)126 KieFileSystem (org.kie.api.builder.KieFileSystem)78 KieServices (org.kie.api.KieServices)76 Test (org.junit.Test)67 KieBuilder (org.kie.api.builder.KieBuilder)56 KieBaseModel (org.kie.api.builder.model.KieBaseModel)55 KieContainer (org.kie.api.runtime.KieContainer)50 ReleaseId (org.kie.api.builder.ReleaseId)38 KieSession (org.kie.api.runtime.KieSession)27 KieSessionModel (org.kie.api.builder.model.KieSessionModel)25 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)18 InternalKieContainer (org.drools.core.impl.InternalKieContainer)14 KieModule (org.kie.api.builder.KieModule)14 KieBase (org.kie.api.KieBase)11 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)10 Results (org.kie.api.builder.Results)10 Resource (org.kie.api.io.Resource)9 ArrayList (java.util.ArrayList)8 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)8 Before (org.junit.Before)7