Skip to content

Commit

Permalink
[incubator-kie-issues#908] Validate new DMN 1.5 features (#5785)
Browse files Browse the repository at this point in the history
* [incubator-kie-issues#908] WIP. Moved shared models to kie-dmn-test-resources. Adapted UnnamedImportUtilsTest.
Add kie-dmn-test-resources as dependency for kie-dmn-validation. Add fix for empty name import. Not working: references to unnamed imported model

* [incubator-kie-issues#908] WIP. Working: references to unnamed imported model

* [incubator-kie-issues#908] WIP. Implemented validation/evaluation for DateRangeLoop, listreplace, unnamed import

* [incubator-kie-issues#908] Validate DMN 1.5 features. Moved shared models to kie-dmn-test-resources

* [incubator-kie-issues#908] Fixed model.

* [incubator-kie-issues#908] Add empty model name test

---------

Co-authored-by: Gabriele-Cardosi <gabriele.cardosi@ibm.com>
  • Loading branch information
gitgabrio and Gabriele-Cardosi authored Mar 15, 2024
1 parent 1feb703 commit 6281abd
Show file tree
Hide file tree
Showing 25 changed files with 809 additions and 380 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ public static void processMergedModel(DMNModelImpl parentModel, DMNModelImpl mer
// Here we try to put all the definitions from the "imported" model inside the parent one
Definitions parentDefinitions = parentModel.getDefinitions();
Definitions mergedDefinitions = mergedModel.getDefinitions();

mergeDefinitions(parentDefinitions, mergedDefinitions);

mergedModel.getTypeRegistry().getTypes().forEach((s, stringDMNTypeMap) ->
stringDMNTypeMap.values().
forEach(dmnType -> parentModel.getTypeRegistry().registerType(dmnType)));
}

public static void mergeDefinitions(Definitions parentDefinitions, Definitions mergedDefinitions) {
// incubator-kie-issues#852: The idea is to not treat the anonymous models as import, but to "merge" them with original opne,
// Here we try to put all the definitions from the "imported" model inside the parent one
parentDefinitions.getArtifact().addAll(mergedDefinitions.getArtifact());

addIfNotPresent(parentDefinitions.getDecisionService(), mergedDefinitions.getDecisionService());
Expand All @@ -59,9 +70,6 @@ public static void processMergedModel(DMNModelImpl parentModel, DMNModelImpl mer
addIfNotPresent(parentDefinitions.getImport(), mergedDefinitions.getImport());
addIfNotPresent(parentDefinitions.getItemDefinition(), mergedDefinitions.getItemDefinition());
mergedDefinitions.getChildren().forEach(parentDefinitions::addChildren);
mergedModel.getTypeRegistry().getTypes().forEach((s, stringDMNTypeMap) ->
stringDMNTypeMap.values().
forEach(dmnType -> parentModel.getTypeRegistry().registerType(dmnType)));
}

static <T extends NamedElement> void addIfNotPresent(Collection<T> target, Collection<T> source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public void testEmptyNamedModelImport() {
this.getClass(),
"Imported_Model_Unamed.dmn");

final DMNModel importedModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_f27bb64b-6fc7-4e1f-9848-11ba35e0df36",
final DMNModel importedModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_f27bb64b-6fc7-4e1f-9848-11ba35e0df44",
"Imported Model");
assertThat(importedModel).isNotNull();
for (final DMNMessage message : importedModel.getMessages()) {
Expand Down Expand Up @@ -324,7 +324,7 @@ public void testOverridingEmptyNamedModelImport() {
this.getClass(),
"Imported_Model_Unamed.dmn");

final DMNModel importedModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_f27bb64b-6fc7-4e1f-9848-11ba35e0df36",
final DMNModel importedModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_f27bb64b-6fc7-4e1f-9848-11ba35e0df44",
"Imported Model");
assertThat(importedModel).isNotNull();
for (final DMNMessage message : importedModel.getMessages()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package org.kie.dmn.core.compiler;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;

import org.drools.util.FileUtils;
import org.junit.Test;
import org.kie.dmn.api.core.DMNModel;
import org.kie.dmn.api.core.DMNRuntime;
Expand All @@ -18,22 +17,22 @@
import org.kie.dmn.model.api.NamedElement;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.kie.dmn.core.compiler.UnnamedImportUtils.addIfNotPresent;
import static org.kie.dmn.core.compiler.UnnamedImportUtils.isInUnnamedImport;

public class UnnamedImportUtilsTest {

@Test
public void isInUnnamedImportTrue() {
File importingModelFile = FileUtils.getFile("Importing_EmptyNamed_Model.dmn");
assertThat(importingModelFile).isNotNull().exists();
File importedModelFile = FileUtils.getFile("Imported_Model_Unamed.dmn");
assertThat(importedModelFile).isNotNull().exists();
final DMNRuntime runtime = DMNRuntimeUtil.createRuntimeWithAdditionalResources(importingModelFile,
importedModelFile);
final DMNRuntime runtime = DMNRuntimeUtil.createRuntimeWithAdditionalResources("Importing_EmptyNamed_Model" +
".dmn",
this.getClass(),
"Imported_Model_Unamed.dmn");

final DMNModel importedModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_f27bb64b-6fc7-4e1f-9848-11ba35e0df36",
final DMNModel importedModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_f27bb64b-6fc7-4e1f-9848-11ba35e0df44",
"Imported Model");
assertThat(importedModel).isNotNull();
final DMNModelImpl importingModel = (DMNModelImpl)runtime.getModel("http://www.trisotech.com/dmn/definitions/_f79aa7a4-f9a3-410a-ac95-bea496edabgc",
Expand All @@ -48,14 +47,11 @@ public void isInUnnamedImportTrue() {

@Test
public void isInUnnamedImportFalse() {
File importingModelFile = FileUtils.getFile("Importing_Named_Model.dmn");
assertThat(importingModelFile).isNotNull().exists();
File importedModelFile = FileUtils.getFile("Imported_Model_Unamed.dmn");
assertThat(importedModelFile).isNotNull().exists();
final DMNRuntime runtime = DMNRuntimeUtil.createRuntimeWithAdditionalResources(importingModelFile,
importedModelFile);
final DMNRuntime runtime = DMNRuntimeUtil.createRuntimeWithAdditionalResources("Importing_Named_Model.dmn",
this.getClass(),
"Imported_Model_Unamed.dmn");

final DMNModel importedModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_f27bb64b-6fc7-4e1f-9848-11ba35e0df36",
final DMNModel importedModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_f27bb64b-6fc7-4e1f-9848-11ba35e0df44",
"Imported Model");
assertThat(importedModel).isNotNull();
final DMNModelImpl importingModel = (DMNModelImpl)runtime.getModel("http://www.trisotech.com/dmn/definitions/_f79aa7a4-f9a3-410a-ac95-bea496edabgc",
Expand All @@ -70,40 +66,43 @@ public void isInUnnamedImportFalse() {

@Test
public void addIfNotPresentTrue() throws IOException {
File importedModelFile = FileUtils.getFile("Imported_Model_Unamed.dmn");
assertThat(importedModelFile).isNotNull().exists();

String xml = new String(Files.readAllBytes(Paths.get(importedModelFile.toURI())));
Definitions definitions = DMNMarshallerFactory.newDefaultMarshaller().unmarshal(xml);
definitions.getDecisionService().forEach(definition -> assertTrue(added(definition)));
definitions.getBusinessContextElement().forEach(definition -> assertTrue(added(definition)));
definitions.getDrgElement().forEach(definition -> assertTrue(added(definition)));
definitions.getImport().forEach(definition -> assertTrue(added(definition)));
definitions.getItemDefinition().forEach(definition -> assertTrue(added(definition)));
URL importedModelFileResource = Thread.currentThread().getContextClassLoader().getResource(
"Imported_Model_Unamed.dmn");
assertNotNull(importedModelFileResource);
try (InputStream is = importedModelFileResource.openStream()) {
String xml = new String(is.readAllBytes(), StandardCharsets.UTF_8);
Definitions definitions = DMNMarshallerFactory.newDefaultMarshaller().unmarshal(xml);
definitions.getDecisionService().forEach(definition -> assertTrue(added(definition)));
definitions.getBusinessContextElement().forEach(definition -> assertTrue(added(definition)));
definitions.getDrgElement().forEach(definition -> assertTrue(added(definition)));
definitions.getImport().forEach(definition -> assertTrue(added(definition)));
definitions.getItemDefinition().forEach(definition -> assertTrue(added(definition)));
}
}

@Test
public void addIfNotPresentFalse() throws IOException {
File importingModelFile = FileUtils.getFile("Importing_OverridingEmptyNamed_Model.dmn");
assertThat(importingModelFile).isNotNull().exists();
File importedModelFile = FileUtils.getFile("Imported_Model_Unamed.dmn");
assertThat(importedModelFile).isNotNull().exists();
final DMNRuntime runtime = DMNRuntimeUtil.createRuntimeWithAdditionalResources(importingModelFile,
importedModelFile);

final DMNRuntime runtime = DMNRuntimeUtil.createRuntimeWithAdditionalResources("Importing_EmptyNamed_Model.dmn",
this.getClass(),
"Imported_Model_Unamed.dmn");
final DMNModelImpl importingModel = (DMNModelImpl)runtime.getModel("http://www.trisotech.com/dmn/definitions/_f79aa7a4-f9a3-410a-ac95-bea496edabgc",
"Importing empty-named Model");
assertThat(importingModel).isNotNull();

Definitions importingDefinitions = importingModel.getDefinitions();

String importedXml = new String(Files.readAllBytes(Paths.get(importedModelFile.toURI())));
Definitions importedDefinitions = DMNMarshallerFactory.newDefaultMarshaller().unmarshal(importedXml);
importedDefinitions.getDecisionService().forEach(definition -> assertFalse(added(importingDefinitions.getDecisionService(), definition)));
importedDefinitions.getBusinessContextElement().forEach(definition -> assertFalse(added(importingDefinitions.getBusinessContextElement(), definition)));
importedDefinitions.getDrgElement().forEach(definition -> assertFalse(added(importingDefinitions.getDrgElement(), definition)));
importedDefinitions.getImport().forEach(definition -> assertFalse(added(importingDefinitions.getImport(), definition)));
importedDefinitions.getItemDefinition().forEach(definition -> assertFalse(added(importingDefinitions.getItemDefinition(), definition)));
URL importedModelFileResource = Thread.currentThread().getContextClassLoader().getResource(
"Imported_Model_Unamed.dmn");
assertNotNull(importedModelFileResource);
try (InputStream is = importedModelFileResource.openStream()) {
String importedXml = new String(is.readAllBytes(), StandardCharsets.UTF_8);
Definitions importedDefinitions = DMNMarshallerFactory.newDefaultMarshaller().unmarshal(importedXml);
importedDefinitions.getDecisionService().forEach(definition -> assertFalse(added(importingDefinitions.getDecisionService(), definition)));
importedDefinitions.getBusinessContextElement().forEach(definition -> assertFalse(added(importingDefinitions.getBusinessContextElement(), definition)));
importedDefinitions.getDrgElement().forEach(definition -> assertFalse(added(importingDefinitions.getDrgElement(), definition)));
importedDefinitions.getImport().forEach(definition -> assertFalse(added(importingDefinitions.getImport(),
definition)));
importedDefinitions.getItemDefinition().forEach(definition -> assertFalse(added(importingDefinitions.getItemDefinition(), definition)));
}
}

private <T extends NamedElement> boolean added(T source) {
Expand Down

This file was deleted.

Loading

0 comments on commit 6281abd

Please sign in to comment.