Search in sources :

Example 16 with KiePMMLException

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);
    }
}
Also used : HasClassLoader(org.kie.pmml.commons.model.HasClassLoader) LoggerFactory(org.slf4j.LoggerFactory) ExternalException(org.kie.pmml.api.exceptions.ExternalException) HashSet(java.util.HashSet) KiePMMLUtil(org.kie.pmml.compiler.commons.utils.KiePMMLUtil) KiePMMLFactoryModel(org.kie.pmml.commons.model.KiePMMLFactoryModel) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) Map(java.util.Map) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) KiePMMLModelUtils.getSanitizedPackageName(org.kie.pmml.commons.utils.KiePMMLModelUtils.getSanitizedPackageName) KiePMMLModelRetriever.getFromCommonDataAndTransformationDictionaryAndModel(org.kie.pmml.compiler.commons.implementations.KiePMMLModelRetriever.getFromCommonDataAndTransformationDictionaryAndModel) PMML(org.dmg.pmml.PMML) Logger(org.slf4j.Logger) Set(java.util.Set) KiePMMLModelRetriever.getFromCommonDataAndTransformationDictionaryAndModelWithSources(org.kie.pmml.compiler.commons.implementations.KiePMMLModelRetriever.getFromCommonDataAndTransformationDictionaryAndModelWithSources) Collectors(java.util.stream.Collectors) List(java.util.List) KiePMMLFactoryFactory.getFactorySourceCode(org.kie.pmml.compiler.commons.factories.KiePMMLFactoryFactory.getFactorySourceCode) HasSourcesMap(org.kie.pmml.commons.model.HasSourcesMap) CommonCompilationDTO(org.kie.pmml.compiler.api.dto.CommonCompilationDTO) Optional(java.util.Optional) PACKAGE_CLASS_TEMPLATE(org.kie.pmml.commons.Constants.PACKAGE_CLASS_TEMPLATE) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLModelUtils.getSanitizedClassName(org.kie.pmml.commons.utils.KiePMMLModelUtils.getSanitizedClassName) InputStream(java.io.InputStream) ExternalException(org.kie.pmml.api.exceptions.ExternalException) ExternalException(org.kie.pmml.api.exceptions.ExternalException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLFactoryModel(org.kie.pmml.commons.model.KiePMMLFactoryModel) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) PMML(org.dmg.pmml.PMML) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) HasSourcesMap(org.kie.pmml.commons.model.HasSourcesMap) HashSet(java.util.HashSet)

Example 17 with KiePMMLException

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);
    }
}
Also used : ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 18 with KiePMMLException

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;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 19 with KiePMMLException

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);
            }
        }
    });
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 20 with KiePMMLException

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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Aggregations

KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)109 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)49 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)40 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)38 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)37 Expression (com.github.javaparser.ast.expr.Expression)33 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)33 NameExpr (com.github.javaparser.ast.expr.NameExpr)32 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)26 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)20 KiePMMLInternalException (org.kie.pmml.api.exceptions.KiePMMLInternalException)18 CompilationUnit (com.github.javaparser.ast.CompilationUnit)17 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)17 NodeList (com.github.javaparser.ast.NodeList)13 HashMap (java.util.HashMap)12 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)10 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)9 ExplicitConstructorInvocationStmt (com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)9 Test (org.junit.Test)7 IOException (java.io.IOException)6