use of org.kie.pmml.commons.model.HasSourcesMap in project drools by kiegroup.
the class PMMLCompilerService method addPMMLRuleMapper.
static void addPMMLRuleMapper(final KiePMMLModel kiePMMLModel, final List<String> generatedRuleMappers, final String sourcePath) {
if (!(kiePMMLModel instanceof HasSourcesMap)) {
String errorMessage = String.format("Expecting HasSourcesMap instance, retrieved %s inside %s", kiePMMLModel.getClass().getName(), sourcePath);
throw new KiePMMLException(errorMessage);
}
if (kiePMMLModel instanceof HasRule) {
String pkgUUID = ((HasRule) kiePMMLModel).getPkgUUID();
String rulesFileName = kiePMMLModel.getKModulePackageName() + "." + RULES_FILE_NAME + pkgUUID;
String pmmlRuleMapper = kiePMMLModel.getKModulePackageName() + "." + KIE_PMML_RULE_MAPPER_CLASS_NAME;
String ruleMapperSource = PMMLRuleMapperFactory.getPMMLRuleMapperSource(rulesFileName);
((HasRule) kiePMMLModel).addSourceMap(pmmlRuleMapper, ruleMapperSource);
generatedRuleMappers.add(pmmlRuleMapper);
}
if (kiePMMLModel instanceof HasNestedModels) {
for (KiePMMLModel nestedModel : ((HasNestedModels) kiePMMLModel).getNestedModels()) {
addPMMLRuleMapper(nestedModel, generatedRuleMappers, sourcePath);
}
}
}
use of org.kie.pmml.commons.model.HasSourcesMap in project drools by kiegroup.
the class PMMLCompilerServiceTest method populateWithPMMLRuleMappers.
@Test
public void populateWithPMMLRuleMappers() {
final List<KiePMMLModel> toPopulate = new ArrayList<>();
toPopulate.add(new KiePMMLTestingModelWithSources("TEST", "kmodulePackageName", Collections.emptyMap()));
toPopulate.add(new KiePMMLModelHasRule("TEST", Collections.emptyList()));
toPopulate.add(new KiePMMLModelHasNestedModelsHasRule("TEST", Collections.emptyList()));
toPopulate.add(new KiePMMLModelHasNestedModelsHasSourceMap("TEST", Collections.emptyList()));
toPopulate.forEach(kiePMMLModel -> assertTrue(((HasSourcesMap) kiePMMLModel).getSourcesMap().isEmpty()));
final File file = new File("foo.pmml");
final Resource resource = new FileSystemResource(file);
PMMLCompilerService.populateWithPMMLRuleMappers(toPopulate, resource);
toPopulate.forEach(kiePmmlModel -> {
if (kiePmmlModel instanceof HasRule || kiePmmlModel instanceof KiePMMLModelHasNestedModelsHasRule) {
assertFalse(((HasSourcesMap) kiePmmlModel).getSourcesMap().isEmpty());
String expected = kiePmmlModel.getKModulePackageName() + "." + KIE_PMML_RULE_MAPPERS_CLASS_NAME;
assertTrue(((HasSourcesMap) kiePmmlModel).getSourcesMap().containsKey(expected));
if (kiePmmlModel instanceof HasRule) {
expected = kiePmmlModel.getKModulePackageName() + "." + KIE_PMML_RULE_MAPPER_CLASS_NAME;
assertTrue(((HasSourcesMap) kiePmmlModel).getSourcesMap().containsKey(expected));
}
} else {
assertTrue(((HasSourcesMap) kiePmmlModel).getSourcesMap().isEmpty());
}
});
}
use of org.kie.pmml.commons.model.HasSourcesMap 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.model.HasSourcesMap in project drools by kiegroup.
the class ModelImplementationProvider method getKiePMMLModelWithSourcesCompiled.
/**
* Method provided only to have <b>drools</b> models working when invoked by a <code>MiningModel</code>
* Default implementation provided for <b>not-drools</b> models.
* @param compilationDTO
* @return
* @throws KiePMMLInternalException
*/
default KiePMMLModelWithSources getKiePMMLModelWithSourcesCompiled(final CompilationDTO<T> compilationDTO) {
KiePMMLModelWithSources toReturn = getKiePMMLModelWithSources(compilationDTO);
final Map<String, String> sourcesMap = ((HasSourcesMap) toReturn).getSourcesMap();
try {
compilationDTO.compileAndLoadClass(sourcesMap);
} catch (Exception e) {
throw new KiePMMLException(e);
}
return toReturn;
}
use of org.kie.pmml.commons.model.HasSourcesMap in project drools by kiegroup.
the class MiningModelImplementationProviderTest method commonGetKiePMMLModelWithSources.
private void commonGetKiePMMLModelWithSources(String source) throws Exception {
final PMML pmml = getPMML(source);
KnowledgeBuilderImpl knowledgeBuilder = new KnowledgeBuilderImpl();
final MiningModel miningmodel = (MiningModel) pmml.getModels().get(0);
final CommonCompilationDTO<MiningModel> compilationDTO = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, miningmodel, new HasKnowledgeBuilderMock(knowledgeBuilder));
final KiePMMLMiningModelWithSources retrieved = (KiePMMLMiningModelWithSources) PROVIDER.getKiePMMLModelWithSources(compilationDTO);
assertNotNull(retrieved);
commonVerifyIsDeepCloneable(retrieved);
assertNotNull(retrieved.getNestedModels());
assertFalse(retrieved.getNestedModels().isEmpty());
final Map<String, String> sourcesMap = new HashMap<>(retrieved.getSourcesMap());
assertFalse(sourcesMap.isEmpty());
try {
KieMemoryCompiler.compile(sourcesMap, Thread.currentThread().getContextClassLoader());
fail("Expecting compilation error without nested models sources");
} catch (Exception e) {
// Expected
}
retrieved.getNestedModels().forEach(nestedModel -> sourcesMap.putAll(((HasSourcesMap) nestedModel).getSourcesMap()));
try {
KieMemoryCompiler.compile(sourcesMap, Thread.currentThread().getContextClassLoader());
} catch (Exception e) {
fail(e.getMessage());
}
}
Aggregations