Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DROOLS-7090] Fix kie-maven-plugin/PMML #4

Merged
merged 1 commit into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> EXAMPLE_PMML_CLASSES = Arrays.asList("compoundnestedpredicatescorecard/CompoundNestedPredicateScorecardFactory.class");

@Test
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -76,7 +73,8 @@ public static void generatePMMLModel(final KieMavenPluginContext kieMavenPluginC
Thread.currentThread().setContextClassLoader(projectClassLoader);

try {
Map<String, byte[]> compiledClassesMap = compileFiles(resourcesDirectories, projectClassLoader, log);
Map<String, byte[]> compiledClassesMap = compileFiles(resourcesDirectories, projectClassLoader,
outputDirectory, log);
writeClasses(targetDirectory, compiledClassesMap);
} finally {
Thread.currentThread().setContextClassLoader(contextClassLoader);
Expand All @@ -94,6 +92,7 @@ public static void generatePMMLModel(final KieMavenPluginContext kieMavenPluginC

private static Map<String, byte[]> compileFiles(final List<org.apache.maven.model.Resource> resourcesDirectories,
final ClassLoader projectClassloader,
final File outputDirectory,
final Log log) throws MojoExecutionException {
CompilationManager compilationManager =
SPIUtils.getCompilationManager(true).orElseThrow(() -> new MojoExecutionException("Failed to load " +
Expand All @@ -106,55 +105,18 @@ private static Map<String, byte[]> compileFiles(final List<org.apache.maven.mode

PMMLCompilationContext pmmlContext = new PMMLCompilationContextImpl("", memoryCompilerClassLoader);

Collection<IndexFile> indexFiles = compilationManager.processResource(pmmlContext, efestoResources.toArray(new EfestoResource[0]));
compilationManager.processResource(pmmlContext, efestoResources.toArray(new EfestoResource[0]));

List<IndexFile> allIndexFiles = getAllIndexFiles(indexFiles);
logger.debug("IndexFiles generated {}", allIndexFiles);

Map<String, byte[]> toReturn = new HashMap<>();
for (IndexFile indexFile : allIndexFiles) {
toReturn.putAll(getCodeFromIndexFile(indexFile, pmmlContext));
}
return toReturn;
}

private static List<IndexFile> getAllIndexFiles(Collection<IndexFile> indexFiles) {
List<IndexFile> toReturn = new ArrayList<>();
indexFiles.forEach(indexFile -> {
if (!toReturn.contains(indexFile)) {
toReturn.add(indexFile);
}
List<IndexFile> partial = getRedirectFromIndexFile(indexFile);
partial.forEach(partialIndexFile -> {
if (!toReturn.contains(partialIndexFile)) {
toReturn.add(partialIndexFile);
}
});
});
return toReturn;
Map<String, IndexFile> indexFilesCreated = pmmlContext.createIndexFiles(outputDirectory.toPath());
indexFilesCreated.forEach((key, value) -> logger.debug("IndexFile generated {} {}", key, value.toPath()));
return getCodeFromPMMLContext(pmmlContext);
}

private static List<IndexFile> 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<String, byte[]> getCodeFromPMMLContext(PMMLCompilationContext pmmlContext) {
List<String> generatedClasses = pmmlContext.getGeneratedResourcesMap().values().stream()
.flatMap((Function<GeneratedResources, Stream<String>>) 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<String, byte[]> getCodeFromIndexFile(IndexFile indexFile,
PMMLCompilationContext pmmlContext) {
List<String> generatedClasses = getGeneratedClassesFromIndexFile(indexFile);
return generatedClasses.stream().collect(Collectors.toMap(fullClassName -> fullClassName,
fullClassName -> getMappedCode(fullClassName,
pmmlContext)));
Expand All @@ -168,23 +130,14 @@ private static byte[] getMappedCode(String fullClassName, PMMLCompilationContext
return toReturn;
}

private static List<String> getGeneratedClassesFromIndexFile(IndexFile indexFile) {
GeneratedResources generatedResources = getGeneratedResources(indexFile);
private static List<String> getGeneratedClassesFromGeneratedResources(GeneratedResources generatedResources) {
return generatedResources.stream()
.filter(GeneratedClassResource.class::isInstance)
.map(GeneratedClassResource.class::cast)
.map(GeneratedClassResource::getFullClassName)
.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<EfestoResource> getEfestoResources(final List<org.apache.maven.model.Resource> resourcesDirectories,
final Log log) throws MojoExecutionException {
List<EfestoResource> toReturn = new ArrayList<>();
Expand Down