diff --git a/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-12/src/test/java-filtered/org/kie/maven/plugin/ittests/BuildPMMLTrustyTestIT.java b/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-12/src/test/java-filtered/org/kie/maven/plugin/ittests/BuildPMMLTrustyTestIT.java index dc795183fc2..c248520a5f2 100644 --- a/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-12/src/test/java-filtered/org/kie/maven/plugin/ittests/BuildPMMLTrustyTestIT.java +++ b/kie-maven-plugin/src/it/kie-maven-plugin-test-kjar-12/src/test/java-filtered/org/kie/maven/plugin/ittests/BuildPMMLTrustyTestIT.java @@ -35,6 +35,8 @@ public class BuildPMMLTrustyTestIT { private static final String GAV_VERSION = "${org.kie.version}"; private static final String PMML_FILE_NAME = "logisticregressionirisdata/logisticRegressionIrisData.pmml"; + + private static final String INDEX_FILE_NAME = "IndexFile.pmml_json"; private static final List EXAMPLE_PMML_CLASSES = Arrays.asList("compoundnestedpredicatescorecard/CompoundNestedPredicateScorecardFactory.class"); @Test @@ -52,6 +54,7 @@ public void testContentKjarWithPMML() throws Exception { Assertions.assertThat(jarContent).isNotEmpty(); Assertions.assertThat(jarContent).contains(PMML_FILE_NAME); + Assertions.assertThat(jarContent).contains(INDEX_FILE_NAME); EXAMPLE_PMML_CLASSES.forEach(examplePMMLClass -> Assertions.assertThat(jarContent).contains(examplePMMLClass)); } } \ No newline at end of file diff --git a/kie-maven-plugin/src/main/java/org/kie/maven/plugin/executors/GeneratePMMLModelExecutor.java b/kie-maven-plugin/src/main/java/org/kie/maven/plugin/executors/GeneratePMMLModelExecutor.java index 0ede3938d38..a2ac235932a 100644 --- a/kie-maven-plugin/src/main/java/org/kie/maven/plugin/executors/GeneratePMMLModelExecutor.java +++ b/kie-maven-plugin/src/main/java/org/kie/maven/plugin/executors/GeneratePMMLModelExecutor.java @@ -21,10 +21,9 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -33,7 +32,6 @@ import org.apache.maven.project.MavenProject; import org.kie.efesto.common.api.io.IndexFile; import org.kie.efesto.common.api.model.GeneratedClassResource; -import org.kie.efesto.common.api.model.GeneratedRedirectResource; import org.kie.efesto.common.api.model.GeneratedResources; import org.kie.efesto.compilationmanager.api.model.EfestoFileResource; import org.kie.efesto.compilationmanager.api.model.EfestoResource; @@ -48,7 +46,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static org.kie.efesto.common.api.utils.JSONUtils.getGeneratedResourcesObject; import static org.kie.maven.plugin.helpers.GenerateCodeHelper.createJavaCompilerSettings; import static org.kie.maven.plugin.helpers.GenerateCodeHelper.getProjectClassLoader; import static org.kie.maven.plugin.helpers.GenerateCodeHelper.writeClasses; @@ -76,7 +73,8 @@ public static void generatePMMLModel(final KieMavenPluginContext kieMavenPluginC Thread.currentThread().setContextClassLoader(projectClassLoader); try { - Map compiledClassesMap = compileFiles(resourcesDirectories, projectClassLoader, log); + Map compiledClassesMap = compileFiles(resourcesDirectories, projectClassLoader, + outputDirectory, log); writeClasses(targetDirectory, compiledClassesMap); } finally { Thread.currentThread().setContextClassLoader(contextClassLoader); @@ -94,6 +92,7 @@ public static void generatePMMLModel(final KieMavenPluginContext kieMavenPluginC private static Map compileFiles(final List resourcesDirectories, final ClassLoader projectClassloader, + final File outputDirectory, final Log log) throws MojoExecutionException { CompilationManager compilationManager = SPIUtils.getCompilationManager(true).orElseThrow(() -> new MojoExecutionException("Failed to load " + @@ -106,55 +105,18 @@ private static Map compileFiles(final List indexFiles = compilationManager.processResource(pmmlContext, efestoResources.toArray(new EfestoResource[0])); + compilationManager.processResource(pmmlContext, efestoResources.toArray(new EfestoResource[0])); - List allIndexFiles = getAllIndexFiles(indexFiles); - logger.debug("IndexFiles generated {}", allIndexFiles); - - Map toReturn = new HashMap<>(); - for (IndexFile indexFile : allIndexFiles) { - toReturn.putAll(getCodeFromIndexFile(indexFile, pmmlContext)); - } - return toReturn; - } - - private static List getAllIndexFiles(Collection indexFiles) { - List toReturn = new ArrayList<>(); - indexFiles.forEach(indexFile -> { - if (!toReturn.contains(indexFile)) { - toReturn.add(indexFile); - } - List partial = getRedirectFromIndexFile(indexFile); - partial.forEach(partialIndexFile -> { - if (!toReturn.contains(partialIndexFile)) { - toReturn.add(partialIndexFile); - } - }); - }); - return toReturn; + Map indexFilesCreated = pmmlContext.createIndexFiles(outputDirectory.toPath()); + indexFilesCreated.forEach((key, value) -> logger.debug("IndexFile generated {} {}", key, value.toPath())); + return getCodeFromPMMLContext(pmmlContext); } - private static List getRedirectFromIndexFile(IndexFile indexFile) { - GeneratedResources generatedResources = getGeneratedResources(indexFile); - return generatedResources.stream() - .filter(GeneratedRedirectResource.class::isInstance) - .map(GeneratedRedirectResource.class::cast) - .map(GeneratedRedirectResource::getTarget) - .map(target -> getTargetIndexFile(indexFile, target)) + private static Map getCodeFromPMMLContext(PMMLCompilationContext pmmlContext) { + List generatedClasses = pmmlContext.getGeneratedResourcesMap().values().stream() + .flatMap((Function>) generatedResources -> + getGeneratedClassesFromGeneratedResources(generatedResources).stream()) .collect(Collectors.toList()); - } - - private static IndexFile getTargetIndexFile(IndexFile indexFile, String target) { - try { - return new IndexFile(indexFile.getParentFile().getCanonicalPath(), target); - } catch (IOException e) { - throw new KiePMMLException(String.format("Failed to create target file %s for %s", target, indexFile), e); - } - } - - private static Map getCodeFromIndexFile(IndexFile indexFile, - PMMLCompilationContext pmmlContext) { - List generatedClasses = getGeneratedClassesFromIndexFile(indexFile); return generatedClasses.stream().collect(Collectors.toMap(fullClassName -> fullClassName, fullClassName -> getMappedCode(fullClassName, pmmlContext))); @@ -168,8 +130,7 @@ private static byte[] getMappedCode(String fullClassName, PMMLCompilationContext return toReturn; } - private static List getGeneratedClassesFromIndexFile(IndexFile indexFile) { - GeneratedResources generatedResources = getGeneratedResources(indexFile); + private static List getGeneratedClassesFromGeneratedResources(GeneratedResources generatedResources) { return generatedResources.stream() .filter(GeneratedClassResource.class::isInstance) .map(GeneratedClassResource.class::cast) @@ -177,14 +138,6 @@ private static List getGeneratedClassesFromIndexFile(IndexFile indexFile .collect(Collectors.toList()); } - private static GeneratedResources getGeneratedResources(IndexFile indexFile) { - try { - return getGeneratedResourcesObject(indexFile); - } catch (Exception e) { - throw new KiePMMLException("Failed to get GeneratedResources from index file " + indexFile); - } - } - private static List getEfestoResources(final List resourcesDirectories, final Log log) throws MojoExecutionException { List toReturn = new ArrayList<>();