use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class DroolsModelProvider method generateRulesFiles.
/**
* This method depends on exec-model. Be aware in case of refactoring
* @param packageDescr
* @return
*/
protected List<GeneratedFile> generateRulesFiles(PackageDescr packageDescr) {
ModelBuilderImpl<PackageSources> modelBuilder = new ModelBuilderImpl<>(PackageSources::dumpSources, new KnowledgeBuilderConfigurationImpl(getClass().getClassLoader()), new ReleaseIdImpl("dummy:dummy:0.0.0"), false);
CompositeKnowledgeBuilder batch = modelBuilder.batch();
batch.add(new DescrResource(packageDescr), ResourceType.DESCR);
try {
batch.build();
if (modelBuilder.hasErrors()) {
StringBuilder builder = new StringBuilder();
for (DroolsError error : modelBuilder.getErrors().getErrors()) {
logger.error(error.toString());
builder.append(error.toString()).append(" ");
}
throw new KiePMMLException(builder.toString());
}
} catch (Exception e) {
logger.error(e.getMessage());
StringBuilder builder = new StringBuilder(e.getMessage()).append(" ");
for (DroolsError error : modelBuilder.getErrors().getErrors()) {
logger.error(error.toString());
builder.append(error.toString()).append(" ");
}
throw new RuntimeException(builder.toString(), e);
}
return generateModels(modelBuilder).stream().map(f -> new GeneratedFile(GeneratedFile.Type.RULE, f.getPath(), new String(f.getData()))).collect(toList());
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLDroolsModelFactoryUtils method getKiePMMLModelCompilationUnit.
/**
* @param droolsCompilationDTO
* @param javaTemplate the name of the <b>file</b> to be used as template source
* @param modelClassName the name of the class used in the provided template
* @return
*/
public static <T extends Model> CompilationUnit getKiePMMLModelCompilationUnit(final DroolsCompilationDTO<T> droolsCompilationDTO, final String javaTemplate, final String modelClassName) {
logger.trace("getKiePMMLModelCompilationUnit {} {} {}", droolsCompilationDTO.getFields(), droolsCompilationDTO.getModel(), droolsCompilationDTO.getPackageName());
String className = droolsCompilationDTO.getSimpleClassName();
CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(className, droolsCompilationDTO.getPackageName(), javaTemplate, modelClassName);
ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(className).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className));
MINING_FUNCTION miningFunction = droolsCompilationDTO.getMINING_FUNCTION();
final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
String targetField = droolsCompilationDTO.getTargetFieldName();
setConstructor(droolsCompilationDTO.getModel(), constructorDeclaration, modelTemplate.getName(), targetField, miningFunction, droolsCompilationDTO.getPackageName());
addFieldTypeMapPopulation(constructorDeclaration.getBody(), droolsCompilationDTO.getFieldTypeMap());
return cloneCU;
}
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 KiePMMLTreeModelFactory method setConstructor.
static void setConstructor(final DroolsCompilationDTO<TreeModel> compilationDTO, final ClassOrInterfaceDeclaration modelTemplate) {
KiePMMLModelFactoryUtils.init(compilationDTO, modelTemplate);
final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
final BlockStmt body = constructorDeclaration.getBody();
final ExplicitConstructorInvocationStmt superStatement = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body).orElseThrow(() -> new KiePMMLException(String.format(MISSING_CONSTRUCTOR_IN_BODY, body)));
CommonCodegenUtils.setExplicitConstructorInvocationStmtArgument(superStatement, "algorithmName", String.format("\"%s\"", compilationDTO.getModel().getAlgorithmName()));
}
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);
}
}
Aggregations