Skip to content

Commit

Permalink
[incubator-kie-issues#1066] Implement usage of namespace inside href …
Browse files Browse the repository at this point in the history
…for unnamed imports (apache#5830)

* [incubator-kie-issues#1066] Soring both default and imported id for unnamed imports

* [incubator-kie-issues#1066] Implementing tests with and without namespace in href

---------

Co-authored-by: Gabriele-Cardosi <[email protected]>
  • Loading branch information
2 people authored and rgdoliveira committed Apr 16, 2024
1 parent fa4a182 commit dc8417a
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -167,16 +168,12 @@ public String nameInCurrentModel(DMNNode node) {
return node.getName();
} else {
Optional<String> lookupAlias = getImportAliasFor(node.getModelNamespace(), node.getModelName());
if (lookupAlias.isPresent()) {
return lookupAlias.get() + "." + node.getName();
} else {
return null;
}
return lookupAlias.map(s -> s + "." + node.getName()).orElse(null);
}
}

public void addInput(InputDataNode idn) {
inputs.put(computeDRGElementModelLocalId(idn), idn);
computeDRGElementModelLocalId(idn).forEach(id -> inputs.put(id, idn));
}

@Override
Expand All @@ -199,24 +196,24 @@ public InputDataNode getInputByName(String name) {

@Override
public Set<InputDataNode> getInputs() {
return this.inputs.values().stream().collect( Collectors.toCollection(LinkedHashSet::new) );
return new LinkedHashSet<>(this.inputs.values());
}

public void addDecision(DecisionNode dn) {
decisions.put(computeDRGElementModelLocalId(dn), dn);
computeDRGElementModelLocalId(dn).forEach(id -> decisions.put(id, dn));
}

private String computeDRGElementModelLocalId(DMNNode node) {
private List<String> computeDRGElementModelLocalId(DMNNode node) {
// incubator-kie-issues#852: The idea is to not treat the anonymous models as import, but to "merge" them with original opne,
// Here, if the node comes from an unnamed imported model, then it is stored only with its id, to be looked for
// as if defined in the model itself
if (node.getModelNamespace().equals(definitions.getNamespace())) {
return node.getId();
return Collections.singletonList(node.getId());
} else if (isInUnnamedImport(node, this)) {
// the node is an unnamed import
return node.getId();
return Arrays.asList(node.getId(), node.getModelNamespace() + "#" + node.getId());
} else {
return node.getModelNamespace() + "#" + node.getId();
return Collections.singletonList(node.getModelNamespace() + "#" + node.getId());
}
}

Expand All @@ -241,7 +238,7 @@ public DecisionNode getDecisionByName(String name) {

@Override
public Set<DecisionNode> getDecisions() {
return this.decisions.values().stream().collect(Collectors.toCollection(LinkedHashSet::new));
return new LinkedHashSet<>(this.decisions.values());
}

@Override
Expand All @@ -265,7 +262,7 @@ public Set<InputDataNode> getRequiredInputsForDecisionId(String decisionId) {
}

public void addDecisionService(DecisionServiceNode dsn) {
decisionServices.put(computeDRGElementModelLocalId(dsn), dsn);
computeDRGElementModelLocalId(dsn).forEach(id -> decisionServices.put(id, dsn));
}

public DecisionServiceNode getDecisionServiceById(String id) {
Expand All @@ -286,11 +283,11 @@ public DecisionServiceNode getDecisionServiceByName(String name) {

@Override
public Collection<DecisionServiceNode> getDecisionServices() {
return this.decisionServices.values().stream().collect(Collectors.toCollection(LinkedHashSet::new));
return new LinkedHashSet<>(this.decisionServices.values());
}

public void addBusinessKnowledgeModel(BusinessKnowledgeModelNode bkm) {
bkms.put(computeDRGElementModelLocalId(bkm), bkm);
computeDRGElementModelLocalId(bkm).forEach(id -> bkms.put(id, bkm));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,10 @@ public static Object coerceUsingType(Object value, DMNType type, boolean typeChe
private boolean isNodeValueDefined(DMNResultImpl result, DMNNode callerNode, DMNNode node) {
if (node.getModelNamespace().equals(result.getContext().scopeNamespace().orElse(result.getModel().getNamespace()))) {
return result.getContext().isDefined(node.getName());
} else {
} else if (isInUnnamedImport(node, (DMNModelImpl) result.getModel())) {
// the node is an unnamed import
return result.getContext().isDefined(node.getName());
} else {
Optional<String> importAlias = callerNode.getModelImportAliasFor(node.getModelNamespace(), node.getModelName());
if (importAlias.isPresent()) {
Object aliasContext = result.getContext().get(importAlias.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,18 @@ public void testImport() {
}

@Test
public void testEmptyNamedModelImport() {
final DMNRuntime runtime = createRuntimeWithAdditionalResources("valid_models/DMNv1_5/Importing_EmptyNamed_Model.dmn",
public void testEmptyNamedModelImportWithHrefNamespace() {
commonValidateUnnamedImport("valid_models/DMNv1_5/Importing_EmptyNamed_Model_With_Href_Namespace.dmn", "valid_models/DMNv1_5/Imported_Model_Unamed.dmn");
}

@Test
public void testEmptyNamedModelImportWithoutHrefNamespace() {
commonValidateUnnamedImport("valid_models/DMNv1_5/Importing_EmptyNamed_Model_Without_Href_Namespace.dmn", "valid_models/DMNv1_5/Imported_Model_Unamed.dmn");
}

@Test
public void testOverridingEmptyNamedModelImport() {
final DMNRuntime runtime = createRuntimeWithAdditionalResources("valid_models/DMNv1_5/Importing_OverridingEmptyNamed_Model.dmn",
this.getClass(),
"valid_models/DMNv1_5/Imported_Model_Unamed.dmn");

Expand All @@ -286,7 +296,7 @@ public void testEmptyNamedModelImport() {
}

final DMNModel importingModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_f79aa7a4-f9a3-410a-ac95-bea496edabgc",
"Importing empty-named Model");
"Importing empty-named Model");
assertThat(importingModel).isNotNull();
for (final DMNMessage message : importingModel.getMessages()) {
LOG.debug("{}", message);
Expand Down Expand Up @@ -319,10 +329,26 @@ public void testEmptyNamedModelImport() {
}

@Test
public void testOverridingEmptyNamedModelImport() {
final DMNRuntime runtime = createRuntimeWithAdditionalResources("valid_models/DMNv1_5/Importing_OverridingEmptyNamed_Model.dmn",
public void testWrongComparisonOps() {
final DMNRuntime runtime = createRuntime("WrongComparisonOps.dmn", this.getClass());
final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_a937d093-86d3-4306-8db8-1e7a33588b68", "Drawing 1");
assertThat(dmnModel).isNotNull();
assertThat(dmnModel.hasErrors()).as(DMNRuntimeUtil.formatMessages(dmnModel.getMessages())).isFalse();
assertThat(dmnModel.getMessages()).as(DMNRuntimeUtil.formatMessages(dmnModel.getMessages())).hasSize(4);
assertThat(dmnModel.getMessages(DMNMessage.Severity.WARN)).as(DMNRuntimeUtil.formatMessages(dmnModel.getMessages())).hasSize(4);
assertThat(dmnModel.getMessages(DMNMessage.Severity.WARN)
.stream()
.filter(m -> m.getSourceId().equals("_d72d6fab-1e67-4fe7-9c12-54800d6fe294") ||
m.getSourceId().equals("_2390dd99-094d-4f97-aecc-9cccb697ce05") ||
m.getSourceId().equals("_0c292d34-498e-4b08-ae99-3c694197b69f") ||
m.getSourceId().equals("_21c7d800-b806-4b2e-9a10-00828de7f2d2"))
.count()).as(DMNRuntimeUtil.formatMessages(dmnModel.getMessages())).isEqualTo(4L);
}

private void commonValidateUnnamedImport(String importingModelRef, String importedModelRef) {
final DMNRuntime runtime = createRuntimeWithAdditionalResources(importingModelRef,
this.getClass(),
"valid_models/DMNv1_5/Imported_Model_Unamed.dmn");
importedModelRef);

final DMNModel importedModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_f27bb64b-6fc7-4e1f-9848-11ba35e0df44",
"Imported Model");
Expand Down Expand Up @@ -363,21 +389,4 @@ public void testOverridingEmptyNamedModelImport() {
assertThat(allProperties).containsEntry("Imported Greeting", "Hello John!");
}
}

@Test
public void testWrongComparisonOps() {
final DMNRuntime runtime = createRuntime("WrongComparisonOps.dmn", this.getClass());
final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_a937d093-86d3-4306-8db8-1e7a33588b68", "Drawing 1");
assertThat(dmnModel).isNotNull();
assertThat(dmnModel.hasErrors()).as(DMNRuntimeUtil.formatMessages(dmnModel.getMessages())).isFalse();
assertThat(dmnModel.getMessages()).as(DMNRuntimeUtil.formatMessages(dmnModel.getMessages())).hasSize(4);
assertThat(dmnModel.getMessages(DMNMessage.Severity.WARN)).as(DMNRuntimeUtil.formatMessages(dmnModel.getMessages())).hasSize(4);
assertThat(dmnModel.getMessages(DMNMessage.Severity.WARN)
.stream()
.filter(m -> m.getSourceId().equals("_d72d6fab-1e67-4fe7-9c12-54800d6fe294") ||
m.getSourceId().equals("_2390dd99-094d-4f97-aecc-9cccb697ce05") ||
m.getSourceId().equals("_0c292d34-498e-4b08-ae99-3c694197b69f") ||
m.getSourceId().equals("_21c7d800-b806-4b2e-9a10-00828de7f2d2"))
.count()).as(DMNRuntimeUtil.formatMessages(dmnModel.getMessages())).isEqualTo(4L);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,15 @@
public class UnnamedImportUtilsTest {

@Test
public void isInUnnamedImportTrue() {
final DMNRuntime runtime = DMNRuntimeUtil.createRuntimeWithAdditionalResources("valid_models/DMNv1_5/Importing_EmptyNamed_Model" +
".dmn",
this.getClass(),
"valid_models/DMNv1_5/Imported_Model_Unamed.dmn");
public void isInUnnamedImportTrueWithHrefNamespace() {
commonIsInUnnamedImportTrue("valid_models/DMNv1_5/Importing_EmptyNamed_Model_With_Href_Namespace.dmn",
"valid_models/DMNv1_5/Imported_Model_Unamed.dmn");
}

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",
"Importing empty-named Model");
assertThat(importingModel).isNotNull();
importedModel.getDecisions().forEach(node -> assertTrue(isInUnnamedImport(node, importingModel)));
importedModel.getBusinessKnowledgeModels().forEach(node -> assertTrue(isInUnnamedImport(node, importingModel)));
importedModel.getDecisionServices().forEach(node -> assertTrue(isInUnnamedImport(node, importingModel)));
importedModel.getInputs().forEach(node -> assertTrue(isInUnnamedImport(node, importingModel)));
importedModel.getItemDefinitions().forEach(node -> assertTrue(isInUnnamedImport(node, importingModel)));
@Test
public void isInUnnamedImportTrueWithoutHrefNamespace() {
commonIsInUnnamedImportTrue("valid_models/DMNv1_5/Importing_EmptyNamed_Model_Without_Href_Namespace.dmn",
"valid_models/DMNv1_5/Imported_Model_Unamed.dmn");
}

@Test
Expand Down Expand Up @@ -81,10 +73,42 @@ public void addIfNotPresentTrue() throws IOException {
}

@Test
public void addIfNotPresentFalse() throws IOException {
final DMNRuntime runtime = DMNRuntimeUtil.createRuntimeWithAdditionalResources("valid_models/DMNv1_5/Importing_EmptyNamed_Model.dmn",
public void addIfNotPresentFalseWithHrefNamespace() throws IOException {
commonAddIfNotPresentFalse("valid_models/DMNv1_5/Importing_EmptyNamed_Model_With_Href_Namespace.dmn",
"valid_models/DMNv1_5/Imported_Model_Unamed.dmn");
}

@Test
public void addIfNotPresentFalseWithoutHrefNamespace() throws IOException {
commonAddIfNotPresentFalse("valid_models/DMNv1_5/Importing_EmptyNamed_Model_Without_Href_Namespace.dmn",
"valid_models/DMNv1_5/Imported_Model_Unamed.dmn");
}

private void commonIsInUnnamedImportTrue(String importingModelRef, String importedModelRef) {
final DMNRuntime runtime = DMNRuntimeUtil.createRuntimeWithAdditionalResources(importingModelRef,
this.getClass(),
"valid_models/DMNv1_5/Imported_Model_Unamed.dmn");
importedModelRef);

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",
"Importing empty-named Model");
assertThat(importingModel).isNotNull();
importedModel.getDecisions().forEach(node -> assertTrue(isInUnnamedImport(node, importingModel)));
importedModel.getBusinessKnowledgeModels().forEach(node -> assertTrue(isInUnnamedImport(node, importingModel)));
importedModel.getDecisionServices().forEach(node -> assertTrue(isInUnnamedImport(node, importingModel)));
importedModel.getInputs().forEach(node -> assertTrue(isInUnnamedImport(node, importingModel)));
importedModel.getItemDefinitions().forEach(node -> assertTrue(isInUnnamedImport(node, importingModel)));
}

private void commonAddIfNotPresentFalse(String importingModelRef, String importedModelRef) throws IOException {
final DMNRuntime runtime = DMNRuntimeUtil.createRuntimeWithAdditionalResources(importingModelRef,
this.getClass(),
importedModelRef);
final DMNModelImpl importingModel = (DMNModelImpl)runtime.getModel("http://www.trisotech.com/dmn/definitions/_f79aa7a4-f9a3-410a-ac95-bea496edabgc",
"Importing empty-named Model");
assertThat(importingModel).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
<dmn:typeRef>number</dmn:typeRef>
</dmn:itemComponent>
</dmn:itemDefinition>

<dmn:inputData id="_51190b90-924d-479b-872b-4c6f3486c2cb" name="A Person">
<dmn:variable id="_44a44de4-c0ab-408e-9ba9-983d8ec2f6b5"
name="A Person"
typeRef="tPerson"/>
</dmn:inputData>
<dmn:inputData id="_51190b90-924d-479b-872b-4c6f3486c2de" name="An Imported Person">
<dmn:variable id="_44a44de4-c0ab-408e-9ba9-983d8ec2f6c6"
name="An Imported Person"
Expand Down
Loading

0 comments on commit dc8417a

Please sign in to comment.