Search in sources :

Example 16 with KiePMMLInternalException

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

the class KiePMMLClusteringModelFactory method setConstructor.

static void setConstructor(final CompilationDTO<ClusteringModel> 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();
    ClusteringModel clusteringModel = compilationDTO.getModel();
    body.addStatement(assignExprFrom("modelClass", modelClassFrom(clusteringModel.getModelClass())));
    clusteringModel.getClusters().stream().map(KiePMMLClusteringModelFactory::clusterCreationExprFrom).map(expr -> methodCallExprFrom("clusters", "add", expr)).forEach(body::addStatement);
    clusteringModel.getClusteringFields().stream().map(KiePMMLClusteringModelFactory::clusteringFieldCreationExprFrom).map(expr -> methodCallExprFrom("clusteringFields", "add", expr)).forEach(body::addStatement);
    body.addStatement(assignExprFrom("comparisonMeasure", comparisonMeasureCreationExprFrom(clusteringModel.getComparisonMeasure())));
    if (clusteringModel.getMissingValueWeights() != null) {
        body.addStatement(assignExprFrom("missingValueWeights", missingValueWeightsCreationExprFrom(clusteringModel.getMissingValueWeights())));
    }
}
Also used : KiePMMLCluster(org.kie.pmml.models.clustering.model.KiePMMLCluster) Arrays(java.util.Arrays) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) CommonCodegenUtils.methodCallExprFrom(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.methodCallExprFrom) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) CommonCodegenUtils.assignExprFrom(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.assignExprFrom) CommonCodegenUtils.literalExprFrom(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.literalExprFrom) MAIN_CLASS_NOT_FOUND(org.kie.pmml.compiler.commons.utils.JavaParserUtils.MAIN_CLASS_NOT_FOUND) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) MissingValueWeights(org.dmg.pmml.clustering.MissingValueWeights) KiePMMLComparisonMeasure(org.kie.pmml.models.clustering.model.KiePMMLComparisonMeasure) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) ComparisonMeasure(org.dmg.pmml.ComparisonMeasure) CompilationUnit(com.github.javaparser.ast.CompilationUnit) KiePMMLClusteringModel(org.kie.pmml.models.clustering.model.KiePMMLClusteringModel) KiePMMLMissingValueWeights(org.kie.pmml.models.clustering.model.KiePMMLMissingValueWeights) NodeList(com.github.javaparser.ast.NodeList) CompilationDTO(org.kie.pmml.compiler.api.dto.CompilationDTO) KiePMMLClusteringField(org.kie.pmml.models.clustering.model.KiePMMLClusteringField) ClusteringField(org.dmg.pmml.clustering.ClusteringField) JavaParserUtils(org.kie.pmml.compiler.commons.utils.JavaParserUtils) Logger(org.slf4j.Logger) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) KiePMMLModelFactoryUtils(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils) JavaParserUtils.getFullClassName(org.kie.pmml.compiler.commons.utils.JavaParserUtils.getFullClassName) KiePMMLClusteringConversionUtils.aggregateFunctionFrom(org.kie.pmml.models.clustering.compiler.factories.KiePMMLClusteringConversionUtils.aggregateFunctionFrom) KiePMMLClusteringConversionUtils.compareFunctionFrom(org.kie.pmml.models.clustering.compiler.factories.KiePMMLClusteringConversionUtils.compareFunctionFrom) Array(org.dmg.pmml.Array) KiePMMLClusteringConversionUtils.modelClassFrom(org.kie.pmml.models.clustering.compiler.factories.KiePMMLClusteringConversionUtils.modelClassFrom) Cluster(org.dmg.pmml.clustering.Cluster) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) MISSING_DEFAULT_CONSTRUCTOR(org.kie.pmml.commons.Constants.MISSING_DEFAULT_CONSTRUCTOR) KiePMMLClusteringConversionUtils.comparisonMeasureKindFrom(org.kie.pmml.models.clustering.compiler.factories.KiePMMLClusteringConversionUtils.comparisonMeasureKindFrom) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) ClusteringModel(org.dmg.pmml.clustering.ClusteringModel) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) KiePMMLClusteringModel(org.kie.pmml.models.clustering.model.KiePMMLClusteringModel) ClusteringModel(org.dmg.pmml.clustering.ClusteringModel)

Example 17 with KiePMMLInternalException

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

the class KiePMMLMiningModelFactory method setConstructor.

static void setConstructor(final MiningModelCompilationDTO 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();
    ClassOrInterfaceType kiePMMLSegmentationClass = parseClassOrInterfaceType(compilationDTO.getSegmentationCanonicalClassName());
    ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
    objectCreationExpr.setType(kiePMMLSegmentationClass);
    CommonCodegenUtils.setAssignExpressionValue(body, "segmentation", objectCreationExpr);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Example 18 with KiePMMLInternalException

use of org.kie.pmml.api.exceptions.KiePMMLInternalException 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 19 with KiePMMLInternalException

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

the class KiePMMLSegmentFactory method getSegmentSourcesMap.

static Map<String, String> getSegmentSourcesMap(final SegmentCompilationDTO segmentCompilationDTO, final boolean isInterpreted) {
    logger.debug(GET_SEGMENT, segmentCompilationDTO.getSegment());
    String kiePMMLModelClass = segmentCompilationDTO.getPackageCanonicalClassName();
    final String className = getSanitizedClassName(segmentCompilationDTO.getId());
    CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(className, segmentCompilationDTO.getPackageName(), KIE_PMML_SEGMENT_TEMPLATE_JAVA, KIE_PMML_SEGMENT_TEMPLATE);
    ClassOrInterfaceDeclaration segmentTemplate = cloneCU.getClassByName(className).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className));
    final ConstructorDeclaration constructorDeclaration = segmentTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, segmentTemplate.getName())));
    final Map<String, String> toReturn = new HashMap<>();
    setConstructor(segmentCompilationDTO.getId(), className, constructorDeclaration, kiePMMLModelClass, isInterpreted, segmentCompilationDTO.getWeight().doubleValue());
    populateGetPredicateMethod(segmentCompilationDTO.getPredicate(), segmentCompilationDTO.getFields(), segmentTemplate);
    toReturn.put(getFullClassName(cloneCU), cloneCU.toString());
    return toReturn;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) HashMap(java.util.HashMap) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException)

Example 20 with KiePMMLInternalException

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

the class KiePMMLModelFactoryUtils method addTransformationsInClassOrInterfaceDeclaration.

/**
 * Add <b>common</b> and <b>local</b> transformations management inside the given
 * <code>ClassOrInterfaceDeclaration</code>
 * @param toPopulate
 * @param transformationDictionary
 * @param localTransformations
 */
public static void addTransformationsInClassOrInterfaceDeclaration(final ClassOrInterfaceDeclaration toPopulate, final TransformationDictionary transformationDictionary, final LocalTransformations localTransformations) {
    String createTransformationDictionary = null;
    if (transformationDictionary != null) {
        BlockStmt createTransformationDictionaryBody = KiePMMLTransformationDictionaryFactory.getKiePMMLTransformationDictionaryVariableDeclaration(transformationDictionary);
        createTransformationDictionaryBody.addStatement(getReturnStmt(TRANSFORMATION_DICTIONARY));
        createTransformationDictionary = "createTransformationDictionary";
        MethodDeclaration createTransformationDictionaryMethod = toPopulate.addMethod(createTransformationDictionary, Modifier.Keyword.PRIVATE);
        createTransformationDictionaryMethod.setType(KiePMMLTransformationDictionary.class.getName());
        createTransformationDictionaryMethod.setBody(createTransformationDictionaryBody);
    }
    String createLocalTransformations = null;
    if (localTransformations != null) {
        BlockStmt createLocalTransformationsBody = KiePMMLLocalTransformationsFactory.getKiePMMLLocalTransformationsVariableDeclaration(localTransformations);
        createLocalTransformationsBody.addStatement(getReturnStmt(LOCAL_TRANSFORMATIONS));
        createLocalTransformations = "createLocalTransformations";
        MethodDeclaration createLocalTransformationsMethod = toPopulate.addMethod(createLocalTransformations, Modifier.Keyword.PRIVATE);
        createLocalTransformationsMethod.setType(KiePMMLLocalTransformations.class.getName());
        createLocalTransformationsMethod.setBody(createLocalTransformationsBody);
    }
    final ConstructorDeclaration constructorDeclaration = toPopulate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, toPopulate.getName())));
    populateTransformationsInConstructor(constructorDeclaration, createTransformationDictionary, createLocalTransformations);
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLLocalTransformations(org.kie.pmml.commons.transformations.KiePMMLLocalTransformations) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) KiePMMLTransformationDictionary(org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary)

Aggregations

KiePMMLInternalException (org.kie.pmml.api.exceptions.KiePMMLInternalException)23 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)17 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)15 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)14 CompilationUnit (com.github.javaparser.ast.CompilationUnit)7 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)7 Expression (com.github.javaparser.ast.expr.Expression)7 NameExpr (com.github.javaparser.ast.expr.NameExpr)7 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)5 NodeList (com.github.javaparser.ast.NodeList)4 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)4 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)4 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)4 ExplicitConstructorInvocationStmt (com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)4 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)4 HashMap (java.util.HashMap)4 MethodReferenceExpr (com.github.javaparser.ast.expr.MethodReferenceExpr)3 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)3 Map (java.util.Map)3 MINING_FUNCTION (org.kie.pmml.api.enums.MINING_FUNCTION)3