Search in sources :

Example 51 with KieModuleModel

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

the class DMNExtensionRegisterTest method testUsingKModuleProperty.

@Test
public void testUsingKModuleProperty() {
    final KieServices ks = KieServices.Factory.get();
    final KieFileSystem kfs = ks.newKieFileSystem();
    KieModuleModel kmm = ks.newKieModuleModel();
    kmm.setConfigurationProperty("org.kie.dmn.profiles.FirstNameLastNameProfile", FirstNameLastNameProfile.class.getCanonicalName());
    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(1, id1elements.getAny().size());
    FirstNameDescription firstNameDescription = (FirstNameDescription) id1elements.getAny().get(0);
    assertTrue(firstNameDescription.getContent().equals("First name in latin characters"));
    InputData inputData2 = (InputData) dmnModel.getDefinitions().getDrgElement().get(2);
    assertEquals("Last Name", inputData2.getName());
    DMNElement.ExtensionElements id2elements = inputData2.getExtensionElements();
    assertTrue(id2elements != null);
    assertEquals(1, id2elements.getAny().size());
    LastNameDescription lastNameDescription = (LastNameDescription) id2elements.getAny().get(0);
    assertTrue(lastNameDescription.getContent().equals("Last name in latin characters"));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) Results(org.kie.api.builder.Results) KieModuleModel(org.kie.api.builder.model.KieModuleModel) DMNElement(org.kie.dmn.model.v1_1.DMNElement) KieBuilder(org.kie.api.builder.KieBuilder) InputData(org.kie.dmn.model.v1_1.InputData) DMNModel(org.kie.dmn.api.core.DMNModel) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 52 with KieModuleModel

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

the class DroolsAbstractPMMLTest method getModelSession.

protected KieSession getModelSession(String[] pmmlSources, boolean verbose) {
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    KieFileSystem kfs = ks.newKieFileSystem();
    for (int j = 0; j < pmmlSources.length; j++) {
        Resource res = ResourceFactory.newClassPathResource(pmmlSources[j]).setResourceType(ResourceType.PMML);
        kfs.write(res);
    }
    KieModuleModel model = ks.newKieModuleModel();
    model.setConfigurationProperty("drools.propertySpecific", "ALLOWED");
    KieBaseModel kbModel = model.newKieBaseModel(DEFAULT_KIEBASE).addPackage(BASE_PACK).setEventProcessingMode(EventProcessingOption.CLOUD);
    kfs.writeKModuleXML(model.toXML());
    KieBuilder kb = ks.newKieBuilder(kfs);
    kb.buildAll();
    if (kb.getResults().hasMessages(Message.Level.ERROR)) {
        throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
    }
    KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
    // KieBase kieBase = kContainer.getKieBase("SampleMine_SampleMineSegmentation_SEGMENT_1");
    KieBase kieBase = kContainer.getKieBase();
    setKbase(kieBase);
    return kieBase.newKieSession();
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieRepository(org.kie.api.builder.KieRepository) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer)

Example 53 with KieModuleModel

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

the class DMNAssemblerTest method testStrictModeProp.

@Test
public void testStrictModeProp() {
    final KieServices services = KieServices.Factory.get();
    final KieFileSystem fileSystem = services.newKieFileSystem();
    KieModuleModel moduleModel = services.newKieModuleModel();
    moduleModel.setConfigurationProperty("org.kie.dmn.strictConformance", "true");
    fileSystem.writeKModuleXML(moduleModel.toXML());
    fileSystem.write(services.getResources().newClassPathResource("strictMode.dmn", this.getClass()));
    services.newKieBuilder(fileSystem).buildAll();
    final KieContainer container = services.newKieContainer(services.getRepository().getDefaultReleaseId());
    DMNRuntime runtime = container.newKieSession().getKieRuntime(DMNRuntime.class);
    DMNModel model = runtime.getModel("http://www.trisotech.com/dmn/definitions/_ecf4ea54-2abc-4e2f-a101-4fe14e356a46", "strictMode");
    DMNContext ctx = runtime.newContext();
    ctx.set("timestring", "2016-12-20T14:30:22z");
    DMNResult result = runtime.evaluateAll(model, ctx);
    assertNull(result.getDecisionResultByName("time").getResult());
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) DMNContext(org.kie.dmn.api.core.DMNContext) KieServices(org.kie.api.KieServices) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 54 with KieModuleModel

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

the class KieModuleBuilderTest method testKieModuleUsingPOMMissingKBaseDefinition.

@Test
public void testKieModuleUsingPOMMissingKBaseDefinition() throws Exception {
    KieServices ks = KieServices.Factory.get();
    // Build a KieModule jar, deploy it into local Maven repository
    ReleaseId releaseId = ks.newReleaseId("org.kie", "metadata-test2", "1.0-SNAPSHOT");
    String pomText = getPom(releaseId);
    File pomFile = new File(System.getProperty("java.io.tmpdir"), MavenRepository.toFileName(releaseId, null) + ".pom");
    try {
        FileOutputStream fos = new FileOutputStream(pomFile);
        fos.write(pomText.getBytes());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writePomXML(getPom(releaseId));
    kfs.write("src/main/java/org/kie/test/Bean.java", createJavaSourceInPackage());
    KieBuilder kieBuilder1 = ks.newKieBuilder(kfs);
    Assert.assertTrue(kieBuilder1.buildAll().getResults().getMessages().isEmpty());
    InternalKieModule kieModule = (InternalKieModule) kieBuilder1.getKieModule();
    KieMavenRepository.getKieMavenRepository().installArtifact(releaseId, kieModule, pomFile);
    // Build a second KieModule, depends on the first KieModule jar which we have deployed into Maven
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "metadata-test-using-pom", "1.0-SNAPSHOT");
    String pomText2 = getPom(releaseId2, releaseId);
    File pomFile2 = new File(System.getProperty("java.io.tmpdir"), MavenRepository.toFileName(releaseId2, null) + ".pom");
    try {
        FileOutputStream fos = new FileOutputStream(pomFile2);
        fos.write(pomText2.getBytes());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    // Try building the second KieModule
    KieServices kieServices = KieServices.Factory.get();
    KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
    InputStream is = new FileInputStream(pomFile2);
    KieModuleModel kproj2 = ks.newKieModuleModel();
    kieFileSystem.write("pom.xml", KieServices.Factory.get().getResources().newInputStreamResource(is));
    kieFileSystem.writeKModuleXML(kproj2.toXML());
    kieFileSystem.write("src/main/resources/rule.drl", createDRLWithImport("rule1"));
    KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
    kieBuilder.buildAll();
    assertTrue(kieBuilder.getResults().getMessages().isEmpty());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) KieModuleModel(org.kie.api.builder.model.KieModuleModel) FileOutputStream(java.io.FileOutputStream) KieBuilder(org.kie.api.builder.KieBuilder) File(java.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 55 with KieModuleModel

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

the class KieModuleDeploymentHelperImpl method createKieFileSystemWithKProject.

/**
 * Create the {@link KieFileSystem} instance to store the content going into the KJar.
 *
 * @param kbaseName
 * @param ksessionName
 * @return
 */
private KieFileSystem createKieFileSystemWithKProject(String kbaseName, String ksessionName) {
    KieModuleModel kproj = config.getKieProject();
    KieFileSystem kfs = config.getKieServicesInstance().newKieFileSystem();
    kfs.writeKModuleXML(kproj.toXML());
    return kfs;
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel)

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