use of org.kie.pmml.api.exceptions.KiePMMLException 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.api.exceptions.KiePMMLException in project drools by kiegroup.
the class HasKnowledgeBuilderImpl method compileAndLoadClass.
/**
* Compile the given sources and add them to given <code>Classloader</code> of the current instance.
* Returns the <code>Class</code> with the given <b>fullClassName</b>
* @param sourcesMap
* @param fullClassName
* @return
*/
@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, JavaConfiguration.CompilerType.ECLIPSE);
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 KiePMMLMiningModelFactory method getKiePMMLMiningModelSourcesMapCommon.
static Map<String, String> getKiePMMLMiningModelSourcesMapCommon(final MiningModelCompilationDTO compilationDTO, final Map<String, String> toReturn) {
logger.trace("getKiePMMLMiningModelSourcesMap {} {} {}", compilationDTO.getFields(), compilationDTO.getModel(), compilationDTO.getPackageName());
if (!toReturn.containsKey(compilationDTO.getSegmentationCanonicalClassName())) {
throw new KiePMMLException("Expected generated class " + compilationDTO.getSegmentationCanonicalClassName() + " not " + "found");
}
CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(compilationDTO.getSimpleClassName(), compilationDTO.getPackageName(), KIE_PMML_MINING_MODEL_TEMPLATE_JAVA, KIE_PMML_MINING_MODEL_TEMPLATE);
ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(compilationDTO.getSimpleClassName()).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + compilationDTO.getSimpleClassName()));
setConstructor(compilationDTO, modelTemplate);
toReturn.put(getFullClassName(cloneCU), cloneCU.toString());
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLSegmentationFactory method setConstructor.
static void setConstructor(final String generatedClassName, final String segmentationName, final ConstructorDeclaration constructorDeclaration, final MULTIPLE_MODEL_METHOD multipleModelMethod, final List<String> segmentsClasses) {
setConstructorSuperNameInvocation(generatedClassName, constructorDeclaration, segmentationName);
final BlockStmt body = constructorDeclaration.getBody();
final ExplicitConstructorInvocationStmt superStatement = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body).orElseThrow(() -> new KiePMMLException(String.format(MISSING_CONSTRUCTOR_IN_BODY, body)));
CommonCodegenUtils.setExplicitConstructorInvocationStmtArgument(superStatement, "multipleModelMethod", multipleModelMethod.getClass().getCanonicalName() + "." + multipleModelMethod.name());
final List<AssignExpr> assignExprs = body.findAll(AssignExpr.class);
assignExprs.forEach(assignExpr -> {
if (assignExpr.getTarget().asNameExpr().getNameAsString().equals("segments")) {
for (String segmentClass : segmentsClasses) {
ClassOrInterfaceType kiePMMLSegmentClass = parseClassOrInterfaceType(segmentClass);
ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
objectCreationExpr.setType(kiePMMLSegmentClass);
NodeList<Expression> arguments = NodeList.nodeList(objectCreationExpr);
MethodCallExpr methodCallExpr = new MethodCallExpr();
methodCallExpr.setScope(assignExpr.getTarget().asNameExpr());
methodCallExpr.setName("add");
methodCallExpr.setArguments(arguments);
ExpressionStmt expressionStmt = new ExpressionStmt();
expressionStmt.setExpression(methodCallExpr);
body.addStatement(expressionStmt);
}
}
});
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLClassificationTableFactory method getClassificationTableBuilder.
public static Map.Entry<String, String> getClassificationTableBuilder(final RegressionCompilationDTO compilationDTO, final LinkedHashMap<String, KiePMMLTableSourceCategory> regressionTablesMap) {
logger.trace("getRegressionTableBuilder {}", regressionTablesMap);
String className = "KiePMMLClassificationTable" + classArity.addAndGet(1);
CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(className, compilationDTO.getPackageName(), KIE_PMML_CLASSIFICATION_TABLE_TEMPLATE_JAVA, KIE_PMML_CLASSIFICATION_TABLE_TEMPLATE);
ClassOrInterfaceDeclaration tableTemplate = cloneCU.getClassByName(className).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className));
final MethodDeclaration staticGetterMethod = tableTemplate.getMethodsByName(GETKIEPMML_TABLE).get(0);
setStaticGetter(compilationDTO, regressionTablesMap, staticGetterMethod, className.toLowerCase());
return new AbstractMap.SimpleEntry<>(getFullClassName(cloneCU), cloneCU.toString());
}
Aggregations