Search in sources :

Example 91 with KiePMMLException

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

the class KiePMMLSegmentFactory method setConstructor.

static void setConstructor(final String segmentName, final String generatedClassName, final ConstructorDeclaration constructorDeclaration, final String kiePMMLModelClass, final boolean isInterpreted, final double weight) {
    setConstructorSuperNameInvocation(generatedClassName, constructorDeclaration, segmentName);
    final BlockStmt body = constructorDeclaration.getBody();
    final ExplicitConstructorInvocationStmt superStatement = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body).orElseThrow(() -> new KiePMMLException(String.format(MISSING_CONSTRUCTOR_IN_BODY, body)));
    final Expression instantiationExpression = getInstantiationExpression(kiePMMLModelClass, isInterpreted);
    String modelInstantiationString = instantiationExpression.toString();
    CommonCodegenUtils.setExplicitConstructorInvocationStmtArgument(superStatement, "model", modelInstantiationString);
    CommonCodegenUtils.setAssignExpressionValue(body, "weight", new DoubleLiteralExpr(weight));
    CommonCodegenUtils.setAssignExpressionValue(body, "id", new StringLiteralExpr(segmentName));
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) KiePMMLFactoryFactory.getInstantiationExpression(org.kie.pmml.compiler.commons.factories.KiePMMLFactoryFactory.getInstantiationExpression) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)

Example 92 with KiePMMLException

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

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

the class KiePMMLSegmentFactory method getSegmentSourcesMap.

public static Map<String, String> getSegmentSourcesMap(final SegmentCompilationDTO segmentCompilationDTO, final List<KiePMMLModel> nestedModels) {
    logger.debug(GET_SEGMENT, segmentCompilationDTO.getSegment());
    final KiePMMLModel nestedModel = getFromCommonDataAndTransformationDictionaryAndModelWithSources(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)

Example 94 with KiePMMLException

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);
    }
}
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 95 with KiePMMLException

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

the class KiePMMLClassificationTableFactory method setStaticGetter.

// not-public code-generation
static void setStaticGetter(final RegressionCompilationDTO compilationDTO, final LinkedHashMap<String, KiePMMLTableSourceCategory> regressionTablesMap, final MethodDeclaration staticGetterMethod, final String variableName) {
    final BlockStmt classificationTableBody = staticGetterMethod.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, staticGetterMethod)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(classificationTableBody, TO_RETURN).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TO_RETURN, classificationTableBody)));
    final BlockStmt newBody = new BlockStmt();
    final Map<String, Expression> regressionTableCategoriesMap = new LinkedHashMap<>();
    regressionTablesMap.forEach((className, tableSourceCategory) -> {
        MethodCallExpr methodCallExpr = new MethodCallExpr();
        methodCallExpr.setScope(new NameExpr(className));
        methodCallExpr.setName(KiePMMLRegressionTableFactory.GETKIEPMML_TABLE);
        regressionTableCategoriesMap.put(tableSourceCategory.getCategory(), methodCallExpr);
    });
    // populate map
    String categoryTableMapName = String.format(VARIABLE_NAME_TEMPLATE, CATEGORICAL_TABLE_MAP, variableName);
    createPopulatedLinkedHashMap(newBody, categoryTableMapName, Arrays.asList(String.class.getSimpleName(), KiePMMLRegressionTable.class.getName()), regressionTableCategoriesMap);
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TO_RETURN, classificationTableBody))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    builder.setArgument(0, new StringLiteralExpr(variableName));
    final REGRESSION_NORMALIZATION_METHOD regressionNormalizationMethod = compilationDTO.getDefaultREGRESSION_NORMALIZATION_METHOD();
    getChainedMethodCallExprFrom("withRegressionNormalizationMethod", initializer).setArgument(0, new NameExpr(regressionNormalizationMethod.getClass().getSimpleName() + "." + regressionNormalizationMethod.name()));
    OP_TYPE opType = compilationDTO.getOP_TYPE();
    getChainedMethodCallExprFrom("withOpType", initializer).setArgument(0, new NameExpr(opType.getClass().getSimpleName() + "." + opType.name()));
    getChainedMethodCallExprFrom("withCategoryTableMap", initializer).setArgument(0, new NameExpr(categoryTableMapName));
    boolean isBinary = compilationDTO.isBinary(regressionTablesMap.size());
    final Expression probabilityMapFunctionExpression = getProbabilityMapFunctionExpression(compilationDTO.getModelNormalizationMethod(), isBinary);
    getChainedMethodCallExprFrom("withProbabilityMapFunction", initializer).setArgument(0, probabilityMapFunctionExpression);
    getChainedMethodCallExprFrom("withIsBinary", initializer).setArgument(0, getExpressionForObject(isBinary));
    getChainedMethodCallExprFrom("withTargetField", initializer).setArgument(0, getExpressionForObject(compilationDTO.getTargetFieldName()));
    getChainedMethodCallExprFrom("withTargetCategory", initializer).setArgument(0, getExpressionForObject(null));
    classificationTableBody.getStatements().forEach(newBody::addStatement);
    staticGetterMethod.setBody(newBody);
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) LinkedHashMap(java.util.LinkedHashMap) CommonCodegenUtils.createPopulatedLinkedHashMap(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.createPopulatedLinkedHashMap) Expression(com.github.javaparser.ast.expr.Expression) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) REGRESSION_NORMALIZATION_METHOD(org.kie.pmml.models.regression.model.enums.REGRESSION_NORMALIZATION_METHOD) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

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