From afa8a6de2d4bcc1db1fd27952f43450c26091c1f Mon Sep 17 00:00:00 2001 From: Yeser Amer Date: Tue, 9 Jul 2024 10:29:02 +0200 Subject: [PATCH] Improved Error Messages + Logs --- .../core/compiler/ImportDMNResolverUtil.java | 65 ++++++++++++------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/compiler/ImportDMNResolverUtil.java b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/compiler/ImportDMNResolverUtil.java index 4a15ef4d199..1f8413fd34e 100644 --- a/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/compiler/ImportDMNResolverUtil.java +++ b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/compiler/ImportDMNResolverUtil.java @@ -29,55 +29,74 @@ import org.kie.dmn.model.api.Import; import org.kie.dmn.model.api.NamespaceConsts; import org.kie.dmn.model.v1_1.TImport; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ImportDMNResolverUtil { + private static final Logger LOGGER = LoggerFactory.getLogger(ImportDMNResolverUtil.class); + private ImportDMNResolverUtil() { // No constructor for util class. } - public static Either resolveImportDMN(Import _import, Collection all, Function idExtractor) { - final String iNamespace = _import.getNamespace(); - final String iName = _import.getName(); - final String iModelName = _import.getAdditionalAttributes().get(TImport.MODELNAME_QNAME); - List allInNS = all.stream() - .filter(m -> idExtractor.apply(m).getNamespaceURI().equals(iNamespace)) + public static Either resolveImportDMN(Import importElement, Collection dmns, Function idExtractor) { + final String importNamespace = importElement.getNamespace(); + final String importName = importElement.getName(); + final String importLocationURI = importElement.getLocationURI(); // This is optional + final String importModelName = importElement.getAdditionalAttributes().get(TImport.MODELNAME_QNAME); + + LOGGER.debug("Resolving DMN Import with namespace={} name={} locationURI={}, modelName={}", + importNamespace, importName, importLocationURI, importModelName); + + List matchingDmns = dmns.stream() + .filter(m -> idExtractor.apply(m).getNamespaceURI().equals(importNamespace)) .collect(Collectors.toList()); - if (allInNS.size() == 1) { - T located = allInNS.get(0); + if (matchingDmns.size() == 1) { + T located = matchingDmns.get(0); // Check if the located DMN Model in the NS, correspond for the import `drools:modelName`. - if (iModelName == null || idExtractor.apply(located).getLocalPart().equals(iModelName)) { + if (importModelName == null || idExtractor.apply(located).getLocalPart().equals(importModelName)) { + LOGGER.debug("DMN Import with namespace={} and importModelName={} resolved!", importNamespace, importModelName); return Either.ofRight(located); } else { - return Either.ofLeft(String.format("While importing DMN for namespace: %s, name: %s, modelName: %s, located within namespace only %s but does not match for the actual name", - iNamespace, iName, iModelName, + LOGGER.error("Impossible to find the Imported DMN with {} namespace, {} name and {} modelName.", + importNamespace, importName, importModelName); + return Either.ofLeft(String.format("While importing DMN for namespace: %s, name: %s, modelName: %s, located " + + "within namespace only %s but does not match for the actual modelName", + importNamespace, importName, importModelName, idExtractor.apply(located))); } } else { - List usingNSandName = allInNS.stream() - .filter(m -> idExtractor.apply(m).getLocalPart().equals(iModelName)) - .collect(Collectors.toList()); + List usingNSandName = matchingDmns.stream() + .filter(m -> idExtractor.apply(m).getLocalPart().equals(importModelName)) + .toList(); if (usingNSandName.size() == 1) { + LOGGER.debug("DMN Import with namespace={} and importModelName={} resolved!", importNamespace, importModelName); return Either.ofRight(usingNSandName.get(0)); - } else if (usingNSandName.size() == 0) { - return Either.ofLeft(String.format("Could not locate required dependency while importing DMN for namespace: %s, name: %s, modelName: %s.", - iNamespace, iName, iModelName)); + } else if (usingNSandName.isEmpty()) { + LOGGER.error("Impossible to find the Imported DMN with {} namespace, {} name and {} modelName.", + importNamespace, importName, importModelName); + return Either.ofLeft(String.format("Impossible to find the Imported DMN with %s namespace %s name and %s modelName.", + importNamespace, importName, importModelName)); } else { - return Either.ofLeft(String.format("While importing DMN for namespace: %s, name: %s, modelName: %s, could not locate required dependency within: %s.", - iNamespace, iName, iModelName, - allInNS.stream().map(idExtractor).collect(Collectors.toList()))); + LOGGER.error("Found {} number of collision resolving an Imported DMN with {} namespace {} name and {} modelName.", + usingNSandName.size(), importNamespace, importName, importModelName); + return Either.ofLeft(String.format("Found a collision resolving an Imported DMN with %s namespace, %s " + + "name and modelName %s. There are %s DMN files with the same namespace in your project. Please " + + "change the DMN namespaces and make them unique to fix this issue.", + importNamespace, importName, importModelName, usingNSandName.size())); } } } - public static enum ImportType { + public enum ImportType { UNKNOWN, DMN, PMML; } - public static ImportType whichImportType(Import _import) { - switch (_import.getImportType()) { + public static ImportType whichImportType(Import importElement) { + switch (importElement.getImportType()) { case org.kie.dmn.model.v1_1.KieDMNModelInstrumentedBase.URI_DMN: case "http://www.omg.org/spec/DMN1-2Alpha/20160929/MODEL": case org.kie.dmn.model.v1_2.KieDMNModelInstrumentedBase.URI_DMN: