use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class HasKnowledgeBuilderMock method compileAndLoadClass.
@Override
public Class<?> compileAndLoadClass(Map<String, String> sourcesMap, String fullClassName) {
ClassLoader classLoader = getClassLoader();
if (!(classLoader instanceof ProjectClassLoader)) {
throw new IllegalStateException("Expected ProjectClassLoader, received " + classLoader.getClass().getName());
}
ProjectClassLoader projectClassLoader = (ProjectClassLoader) classLoader;
final Map<String, byte[]> byteCode = KieMemoryCompiler.compileNoLoad(sourcesMap, projectClassLoader);
byteCode.forEach(projectClassLoader::defineClass);
try {
return projectClassLoader.loadClass(fullClassName);
} catch (Exception e) {
throw new KiePMMLException(e);
}
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLScorecardModelFactory method getKiePMMLScorecardModelSourcesMap.
public static Map<String, String> getKiePMMLScorecardModelSourcesMap(final DroolsCompilationDTO<Scorecard> compilationDTO) {
logger.trace("getKiePMMLScorecardModelSourcesMap {} {} {}", compilationDTO.getFields(), compilationDTO.getModel(), compilationDTO.getPackageName());
CompilationUnit cloneCU = getKiePMMLModelCompilationUnit(compilationDTO, KIE_PMML_SCORECARD_MODEL_TEMPLATE_JAVA, KIE_PMML_SCORECARD_MODEL_TEMPLATE);
String className = compilationDTO.getSimpleClassName();
ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(className).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className));
setConstructor(compilationDTO, modelTemplate);
Map<String, String> toReturn = new HashMap<>();
String fullClassName = compilationDTO.getPackageCanonicalClassName();
toReturn.put(fullClassName, cloneCU.toString());
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLTreeModelFactory method getKiePMMLTreeModelSourcesMap.
public static Map<String, String> getKiePMMLTreeModelSourcesMap(final TreeCompilationDTO compilationDTO) {
logger.trace("getKiePMMLTreeModelSourcesMap {} {} {}", compilationDTO.getFields(), compilationDTO.getModel(), compilationDTO.getPackageName());
String className = compilationDTO.getSimpleClassName();
String packageName = compilationDTO.getPackageName();
CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(className, packageName, KIE_PMML_TREE_MODEL_TEMPLATE_JAVA, KIE_PMML_TREE_MODEL_TEMPLATE);
ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(className).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className));
final Double missingValuePenalty = compilationDTO.getMissingValuePenalty();
final KiePMMLNodeFactory.NodeNamesDTO nodeNamesDTO = new KiePMMLNodeFactory.NodeNamesDTO(compilationDTO.getNode(), createNodeClassName(), null, missingValuePenalty);
String fullNodeClassName = packageName + "." + nodeNamesDTO.nodeClassName;
Map<String, String> toReturn = getKiePMMLNodeSourcesMap(nodeNamesDTO, compilationDTO.getFields(), packageName);
setConstructor(compilationDTO, modelTemplate, fullNodeClassName);
String fullClassName = packageName + "." + className;
toReturn.put(fullClassName, cloneCU.toString());
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLUtil method populateMissingOutputFieldDataType.
/**
* Method to populate the <b>dataType</b> property of <code>OutputField</code>s.
* Such property was optional until 4.4.1 spec
* @param toPopulate
* @param miningFields
* @param dataFields
*/
static void populateMissingOutputFieldDataType(List<OutputField> toPopulate, List<MiningField> miningFields, List<DataField> dataFields) {
// partial implementation to fix missing "dataType" inside OutputField; "dataType" became mandatory only in 4.4.1 version
List<MiningField> targetFields = getMiningTargetFields(miningFields);
toPopulate.stream().filter(outputField -> outputField.getDataType() == null).forEach(outputField -> {
MiningField referencedField = null;
if (outputField.getTargetField() != null) {
referencedField = targetFields.stream().filter(targetField -> outputField.getTargetField().equals(targetField.getName())).findFirst().orElseThrow(() -> new KiePMMLException("Failed to find a target field for OutputField " + outputField.getName().getValue()));
}
if (referencedField == null && (outputField.getResultFeature() == null || outputField.getResultFeature().equals(ResultFeature.PREDICTED_VALUE))) {
// default predictedValue
referencedField = targetFields.stream().findFirst().orElse(// It is allowed to not have any "target" field inside MiningSchema
null);
}
if (referencedField == null && ResultFeature.PROBABILITY.equals(outputField.getResultFeature())) {
// we set the "dataType" to "double" because outputField is a "probability", we may return
outputField.setDataType(DataType.DOUBLE);
return;
}
if (referencedField != null) {
FieldName targetFieldName = referencedField.getName();
DataField dataField = dataFields.stream().filter(df -> df.getName().equals(targetFieldName)).findFirst().orElseThrow(() -> new KiePMMLException("Failed to find a DataField field for " + "MiningField " + targetFieldName.toString()));
outputField.setDataType(dataField.getDataType());
}
});
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class TestingModelImplementationProvider method getKiePMMLTestModelSourcesMap.
private Map<String, String> getKiePMMLTestModelSourcesMap(final CompilationDTO<TestModel> compilationDTO) {
String className = compilationDTO.getSimpleClassName();
CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(className, compilationDTO.getPackageName(), KIE_PMML_TEST_MODEL_TEMPLATE_JAVA, KIE_PMML_TEST_MODEL_TEMPLATE);
ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(className).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className));
String modelName = compilationDTO.getModelName();
final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
setConstructor(className, constructorDeclaration, modelName);
Map<String, String> toReturn = new HashMap<>();
toReturn.put(getFullClassName(cloneCU), cloneCU.toString());
return toReturn;
}
Aggregations