Skip to content

Commit

Permalink
DROOLS-5351 - Allow to provide relative resource resolver for DMN run…
Browse files Browse the repository at this point in the history
…time (apache#2899)

* changed order to allow relativeResolver to be invoked first if given

* expose resource resolver via DMNRutimeBuilder

* reworked accordingly to
https://github.com/kiegroup/drools/pull/2899#issuecomment-630854916

* test coverage (#2)

* better default case (#3)

Co-authored-by: tarilabs <matteo.mortari@gmail.com>
  • Loading branch information
mswiderski and tarilabs authored May 22, 2020
1 parent fa6ccfe commit cee0d36
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,12 @@ public void accept(Exception t) {
}

protected static Resource resolveRelativeResource(ClassLoader classLoader, DMNModelImpl model, Import i, DMNModelInstrumentedBase node, Function<String, Reader> relativeResolver) {
if (model.getResource() != null) {
URL pmmlURL = pmmlImportURL(classLoader, model, i, node);
return ResourceFactory.newUrlResource(pmmlURL);
} else if (relativeResolver != null) {
if (relativeResolver != null) {
Reader reader = relativeResolver.apply(i.getLocationURI());
return ResourceFactory.newReaderResource(reader);
} else if (model.getResource() != null) {
URL pmmlURL = pmmlImportURL(classLoader, model, i, node);
return ResourceFactory.newUrlResource(pmmlURL);
}
throw new UnsupportedOperationException("Unable to determine relative Resource for import named: " + i.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.kie.dmn.core.internal.utils;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -67,11 +68,22 @@ private static class DMNRuntimeBuilderCtx {

public final DMNCompilerConfigurationImpl cc;
public final List<DMNProfile> dmnProfiles = new ArrayList<>();
private RelativeImportResolver relativeResolver;

public DMNRuntimeBuilderCtx() {
this.cc = new DMNCompilerConfigurationImpl();
}

public void setRelativeResolver(RelativeImportResolver relativeResolver) {
this.relativeResolver = relativeResolver;
}

}

@FunctionalInterface
public static interface RelativeImportResolver {

Reader resolve(String modelNamespace, String modelName, String locationURI);
}

/**
Expand Down Expand Up @@ -101,6 +113,11 @@ public DMNRuntimeBuilder setRootClassLoader(ClassLoader classLoader) {
return this;
}

public DMNRuntimeBuilder setRelativeImportResolver(RelativeImportResolver relativeResolver) {
ctx.setRelativeResolver(relativeResolver);
return this;
}

/**
* Internal Utility class.
*/
Expand Down Expand Up @@ -165,7 +182,21 @@ public Either<Exception, DMNRuntime> fromResources(Collection<Resource> resource

List<DMNModel> dmnModels = new ArrayList<>();
for (DMNResource dmnRes : sortedDmnResources) {
DMNModel dmnModel = dmnCompiler.compile(dmnRes.getDefinitions(), dmnRes.getResAndConfig().getResource(), dmnModels);
DMNModel dmnModel = null;
if (ctx.relativeResolver != null) {
if (dmnCompiler instanceof DMNCompilerImpl) {
dmnModel = ((DMNCompilerImpl) dmnCompiler).compile(dmnRes.getDefinitions(),
dmnModels,
dmnRes.getResAndConfig().getResource(),
relativeURI -> ctx.relativeResolver.resolve(dmnRes.getDefinitions().getNamespace(),
dmnRes.getDefinitions().getName(),
relativeURI));
} else {
throw new IllegalStateException("specified a RelativeImportResolver but the compiler is not org.kie.dmn.core.compiler.DMNCompilerImpl");
}
} else {
dmnModel = dmnCompiler.compile(dmnRes.getDefinitions(), dmnRes.getResAndConfig().getResource(), dmnModels);
}
if (dmnModel != null) {
dmnModels.add(dmnModel);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package org.kie.dmn.core.pmml;

import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;

import org.drools.compiler.kie.builder.impl.DrlProject;
Expand All @@ -39,10 +41,12 @@
import org.kie.dmn.core.impl.CompositeTypeImpl;
import org.kie.dmn.core.impl.DMNModelImpl;
import org.kie.dmn.core.impl.SimpleTypeImpl;
import org.kie.dmn.core.internal.utils.DMNRuntimeBuilder;
import org.kie.dmn.core.util.DMNRuntimeUtil;
import org.kie.dmn.feel.lang.types.BuiltInType;
import org.kie.internal.builder.IncrementalResults;
import org.kie.internal.builder.InternalKieBuilder;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.services.KieAssemblersImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -82,6 +86,16 @@ public void testWithInputTypes() {
runDMNModelInvokingPMML(runtime);
}

@Test
public void testBasicNoKieAssembler() {
DMNRuntime dmnRuntime = DMNRuntimeBuilder.fromDefaults()
.setRelativeImportResolver((ns, n, uri) -> new InputStreamReader(DMNRuntimePMMLTest.class.getResourceAsStream(uri)))
.buildConfiguration()
.fromResources(Arrays.asList(ResourceFactory.newClassPathResource("KiePMMLScoreCard.dmn", DMNRuntimePMMLTest.class)))
.getOrElseThrow(e -> new RuntimeException("Error compiling DMN model(s)", e));
runDMNModelInvokingPMML(dmnRuntime);
}

static void runDMNModelInvokingPMML(final DMNRuntime runtime) {
final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_ca466dbe-20b4-4e88-a43f-4ce3aff26e4f", "KiePMMLScoreCard");
assertThat( dmnModel, notNullValue() );
Expand Down

0 comments on commit cee0d36

Please sign in to comment.