Search in sources :

Example 86 with KiePMMLException

use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.

the class KiePMMLTreeModelFactory method getKiePMMLTreeModel.

public static KiePMMLTreeModel getKiePMMLTreeModel(final DroolsCompilationDTO<TreeModel> compilationDTO) throws IllegalAccessException, InstantiationException {
    logger.trace("getKiePMMLTreeModel {} {}", compilationDTO.getPackageName(), compilationDTO.getModel());
    Map<String, String> sourcesMap = getKiePMMLTreeModelSourcesMap(compilationDTO);
    try {
        Class<?> kiePMMLTreeModelClass = compilationDTO.compileAndLoadClass(sourcesMap);
        return (KiePMMLTreeModel) kiePMMLTreeModelClass.newInstance();
    } catch (Exception e) {
        throw new KiePMMLException(e);
    }
}
Also used : KiePMMLTreeModel(org.kie.pmml.models.drools.tree.model.KiePMMLTreeModel) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 87 with KiePMMLException

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 DroolsCompilationDTO<TreeModel> compilationDTO) {
    logger.trace("getKiePMMLTreeModelSourcesMap {} {} {}", compilationDTO.getFields(), compilationDTO.getModel(), compilationDTO.getPackageName());
    CompilationUnit cloneCU = getKiePMMLModelCompilationUnit(compilationDTO, KIE_PMML_TREE_MODEL_TEMPLATE_JAVA, KIE_PMML_TREE_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;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) KiePMMLDroolsModelFactoryUtils.getKiePMMLModelCompilationUnit(org.kie.pmml.models.drools.utils.KiePMMLDroolsModelFactoryUtils.getKiePMMLModelCompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) HashMap(java.util.HashMap) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 88 with KiePMMLException

use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.

the class KiePMMLMiningModelFactory method getKiePMMLMiningModel.

public static KiePMMLMiningModel getKiePMMLMiningModel(final MiningModelCompilationDTO compilationDTO) {
    logger.debug("getKiePMMLMiningModel {}", compilationDTO.getModel());
    final List<KiePMMLModel> nestedModels = new ArrayList<>();
    Map<String, String> sourcesMap = getKiePMMLMiningModelSourcesMapCompiled(compilationDTO, nestedModels);
    try {
        Class<?> kiePMMLMiningModel = compilationDTO.compileAndLoadClass(sourcesMap);
        return (KiePMMLMiningModel) kiePMMLMiningModel.newInstance();
    } catch (Exception e) {
        throw new KiePMMLException(e);
    }
}
Also used : KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) KiePMMLMiningModel(org.kie.pmml.models.mining.model.KiePMMLMiningModel) ArrayList(java.util.ArrayList) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 89 with KiePMMLException

use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.

the class KiePMMLSegmentationFactory method getSegmentationSourcesMapCommon.

static Map<String, String> getSegmentationSourcesMapCommon(final MiningModelCompilationDTO compilationDTO, final Map<String, String> toReturn) {
    logger.debug("getSegmentationSourcesMapCommon {}", compilationDTO.getSegmentation());
    String className = compilationDTO.getSegmentationClassName();
    CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(className, compilationDTO.getSegmentationPackageName(), KIE_PMML_SEGMENTATION_TEMPLATE_JAVA, KIE_PMML_SEGMENTATION_TEMPLATE);
    ClassOrInterfaceDeclaration segmentationTemplate = cloneCU.getClassByName(className).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className));
    final ConstructorDeclaration constructorDeclaration = segmentationTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, compilationDTO.getSegmentationName())));
    // Avoid stream/map to preserve insertion order and then execution order
    final List<String> segmentsClasses = new ArrayList<>();
    if (compilationDTO.getSegmentation().getSegments() != null) {
        for (Segment segment : compilationDTO.getSegmentation().getSegments()) {
            segmentsClasses.add(getSanitizedPackageName(compilationDTO.getSegmentationPackageName() + "." + segment.getId()) + "." + getSanitizedClassName(segment.getId()));
        }
    }
    if (!toReturn.keySet().containsAll(segmentsClasses)) {
        String missingClasses = String.join(", ", segmentsClasses);
        throw new KiePMMLException("Expected generated class " + missingClasses + " not found");
    }
    setConstructor(className, compilationDTO.getSegmentationName(), constructorDeclaration, MULTIPLE_MODEL_METHOD.byName(compilationDTO.getSegmentation().getMultipleModelMethod().value()), segmentsClasses);
    toReturn.put(getFullClassName(cloneCU), cloneCU.toString());
    return toReturn;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) ArrayList(java.util.ArrayList) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) Segment(org.dmg.pmml.mining.Segment)

Example 90 with KiePMMLException

use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.

the class KiePMMLSegmentFactory method getSegmentSourcesMapCompiled.

public static Map<String, String> getSegmentSourcesMapCompiled(final SegmentCompilationDTO segmentCompilationDTO, final List<KiePMMLModel> nestedModels) {
    logger.debug(GET_SEGMENT, segmentCompilationDTO.getSegment());
    final KiePMMLModel nestedModel = getFromCommonDataAndTransformationDictionaryAndModelWithSourcesCompiled(segmentCompilationDTO).orElseThrow(() -> new KiePMMLException("Failed to get the KiePMMLModel for segment " + segmentCompilationDTO.getModel().getModelName()));
    final Map<String, String> toReturn = getSegmentSourcesMapCommon(segmentCompilationDTO, nestedModels, nestedModel);
    segmentCompilationDTO.addFields(getFieldsFromModel(segmentCompilationDTO.getModel()));
    return toReturn;
}
Also used : KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) 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