use of org.kie.pmml.commons.Constants.PACKAGE_CLASS_TEMPLATE in project drools by kiegroup.
the class PMMLCompilerImpl method getKiePMMLModelsWithSources.
@Override
public List<KiePMMLModel> getKiePMMLModelsWithSources(final String factoryClassName, final String packageName, final InputStream inputStream, final String fileName, final HasClassLoader hasClassloader) {
logger.trace("getModels {} {}", inputStream, hasClassloader);
try {
PMML commonPMMLModel = KiePMMLUtil.load(inputStream, fileName);
Set<String> expectedClasses = commonPMMLModel.getModels().stream().map(model -> {
String modelPackageName = getSanitizedPackageName(String.format(PACKAGE_CLASS_TEMPLATE, packageName, model.getModelName()));
return modelPackageName + "." + getSanitizedClassName(model.getModelName());
}).collect(Collectors.toSet());
final List<KiePMMLModel> toReturn = getModelsWithSources(packageName, commonPMMLModel, hasClassloader);
final Set<String> generatedClasses = new HashSet<>();
Map<String, Boolean> expectedClassModelTypeMap = expectedClasses.stream().collect(Collectors.toMap(expectedClass -> expectedClass, expectedClass -> {
HasSourcesMap retrieved = getHasSourceMap(toReturn, expectedClass);
generatedClasses.addAll(retrieved.getSourcesMap().keySet());
return retrieved.isInterpreted();
}));
if (!generatedClasses.containsAll(expectedClasses)) {
expectedClasses.removeAll(generatedClasses);
String missingClasses = String.join(", ", expectedClasses);
throw new KiePMMLException("Expected generated class " + missingClasses + " not found");
}
Map<String, String> factorySourceMap = getFactorySourceCode(factoryClassName, packageName, expectedClassModelTypeMap);
KiePMMLFactoryModel kiePMMLFactoryModel = new KiePMMLFactoryModel(factoryClassName, packageName, factorySourceMap);
toReturn.add(kiePMMLFactoryModel);
return toReturn;
} catch (KiePMMLInternalException e) {
throw new KiePMMLException("KiePMMLInternalException", e);
} catch (KiePMMLException e) {
throw e;
} catch (Exception e) {
throw new ExternalException("ExternalException", e);
}
}
use of org.kie.pmml.commons.Constants.PACKAGE_CLASS_TEMPLATE in project drools by kiegroup.
the class KiePMMLMiningModelFactoryTest method getKiePMMLMiningModelSourcesMapCompiled.
@Test
public void getKiePMMLMiningModelSourcesMapCompiled() {
final List<KiePMMLModel> nestedModels = new ArrayList<>();
final HasKnowledgeBuilderMock hasKnowledgeBuilderMock = new HasKnowledgeBuilderMock(KNOWLEDGE_BUILDER);
final CommonCompilationDTO<MiningModel> source = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, MINING_MODEL, hasKnowledgeBuilderMock);
final MiningModelCompilationDTO compilationDTO = MiningModelCompilationDTO.fromCompilationDTO(source);
final List<String> expectedGeneratedClasses = MINING_MODEL.getSegmentation().getSegments().stream().map(segment -> {
String modelName = segment.getModel().getModelName();
String sanitizedPackageName = getSanitizedPackageName(compilationDTO.getSegmentationPackageName() + "." + segment.getId());
String sanitizedClassName = getSanitizedClassName(modelName);
return String.format(PACKAGE_CLASS_TEMPLATE, sanitizedPackageName, sanitizedClassName);
}).collect(Collectors.toList());
expectedGeneratedClasses.forEach(expectedGeneratedClass -> {
try {
hasKnowledgeBuilderMock.getClassLoader().loadClass(expectedGeneratedClass);
fail("Expecting class not found: " + expectedGeneratedClass);
} catch (Exception e) {
assertTrue(e instanceof ClassNotFoundException);
}
});
final Map<String, String> retrieved = KiePMMLMiningModelFactory.getKiePMMLMiningModelSourcesMapCompiled(compilationDTO, nestedModels);
assertNotNull(retrieved);
int expectedNestedModels = MINING_MODEL.getSegmentation().getSegments().size();
assertEquals(expectedNestedModels, nestedModels.size());
expectedGeneratedClasses.forEach(expectedGeneratedClass -> {
try {
hasKnowledgeBuilderMock.getClassLoader().loadClass(expectedGeneratedClass);
} catch (Exception e) {
fail("Expecting class to be loaded, but got: " + e.getClass().getName() + " -> " + e.getMessage());
e.printStackTrace();
}
});
}
Aggregations