Search in sources :

Example 6 with KiePMMLInternalException

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

the class KiePMMLScorecardModelFactoryTest method setConstructor.

@Test
public void setConstructor() {
    String fullCharacteristicsClassName = PACKAGE_NAME + ".fullCharacteristicsClassName";
    final CommonCompilationDTO<Scorecard> source = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, basicComplexPartialScorePmml, basicComplexPartialScore, new HasClassLoaderMock());
    KiePMMLScorecardModelFactory.setConstructor(ScorecardCompilationDTO.fromCompilationDTO(source), scorecardTemplate, fullCharacteristicsClassName);
    final ConstructorDeclaration constructorDeclaration = scorecardTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, scorecardTemplate.getName())));
    final BlockStmt body = constructorDeclaration.getBody();
    final ExplicitConstructorInvocationStmt retrieved = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body).orElseThrow(() -> new KiePMMLException(String.format(MISSING_CONSTRUCTOR_IN_BODY, body)));
    Statement expected = JavaParserUtils.parseStatement(String.format("super(\"%1$s\", Collections.emptyList()" + ", new %2$s" + "(), %3$s, %4$s, %5$s, %6$s);\n", getSanitizedClassName(basicComplexPartialScore.getModelName()), fullCharacteristicsClassName, basicComplexPartialScore.getInitialScore(), basicComplexPartialScore.isUseReasonCodes(), REASONCODE_ALGORITHM.class.getName() + "." + REASONCODE_ALGORITHM.byName(basicComplexPartialScore.getReasonCodeAlgorithm().value()), basicComplexPartialScore.getBaselineScore()));
    assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
}
Also used : ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) Scorecard(org.dmg.pmml.scorecard.Scorecard) HasClassLoaderMock(org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock) Test(org.junit.Test)

Example 7 with KiePMMLInternalException

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

the class KiePMMLModelFactoryUtils method init.

/**
 * Initialize the given <code>ClassOrInterfaceDeclaration</code> with all the <b>common</b> code needed to
 * generate a <code>KiePMMLModel</code>
 * @param compilationDTO
 * @param modelTemplate
 */
public static void init(final CompilationDTO<? extends Model> compilationDTO, final ClassOrInterfaceDeclaration modelTemplate) {
    final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
    final String name = compilationDTO.getModelName();
    final String generatedClassName = compilationDTO.getSimpleClassName();
    final List<MiningField> miningFields = compilationDTO.getKieMiningFields();
    final List<OutputField> outputFields = compilationDTO.getKieOutputFields();
    final List<TargetField> targetFields = compilationDTO.getKieTargetFields();
    final Expression miningFunctionExpression;
    if (compilationDTO.getMINING_FUNCTION() != null) {
        MINING_FUNCTION miningFunction = compilationDTO.getMINING_FUNCTION();
        miningFunctionExpression = new NameExpr(miningFunction.getClass().getName() + "." + miningFunction.name());
    } else {
        miningFunctionExpression = new NullLiteralExpr();
    }
    final PMML_MODEL pmmlModelEnum = compilationDTO.getPMML_MODEL();
    final NameExpr pmmlMODELExpression = new NameExpr(pmmlModelEnum.getClass().getName() + "." + pmmlModelEnum.name());
    String targetFieldName = compilationDTO.getTargetFieldName();
    final Expression targetFieldExpression;
    if (targetFieldName != null) {
        targetFieldExpression = new StringLiteralExpr(targetFieldName);
    } else {
        targetFieldExpression = new NullLiteralExpr();
    }
    setKiePMMLModelConstructor(generatedClassName, constructorDeclaration, name, miningFields, outputFields, targetFields);
    addTransformationsInClassOrInterfaceDeclaration(modelTemplate, compilationDTO.getTransformationDictionary(), compilationDTO.getLocalTransformations());
    final BlockStmt body = constructorDeclaration.getBody();
    CommonCodegenUtils.setAssignExpressionValue(body, "pmmlMODEL", pmmlMODELExpression);
    CommonCodegenUtils.setAssignExpressionValue(body, "miningFunction", miningFunctionExpression);
    CommonCodegenUtils.setAssignExpressionValue(body, "targetField", targetFieldExpression);
    addGetCreatedKiePMMLMiningFieldsMethod(modelTemplate, compilationDTO.getMiningSchema().getMiningFields(), compilationDTO.getFields());
    MethodCallExpr getCreatedKiePMMLMiningFieldsExpr = new MethodCallExpr();
    getCreatedKiePMMLMiningFieldsExpr.setScope(new ThisExpr());
    getCreatedKiePMMLMiningFieldsExpr.setName(GET_CREATED_KIEPMMLMININGFIELDS);
    CommonCodegenUtils.setAssignExpressionValue(body, "kiePMMLMiningFields", getCreatedKiePMMLMiningFieldsExpr);
    if (compilationDTO.getOutput() != null) {
        addGetCreatedKiePMMLOutputFieldsMethod(modelTemplate, compilationDTO.getOutput().getOutputFields());
        MethodCallExpr getCreatedKiePMMLOutputFieldsExpr = new MethodCallExpr();
        getCreatedKiePMMLOutputFieldsExpr.setScope(new ThisExpr());
        getCreatedKiePMMLOutputFieldsExpr.setName(GET_CREATED_KIEPMMLOUTPUTFIELDS);
        CommonCodegenUtils.setAssignExpressionValue(body, "kiePMMLOutputFields", getCreatedKiePMMLOutputFieldsExpr);
    }
}
Also used : KiePMMLMiningField(org.kie.pmml.commons.model.KiePMMLMiningField) MiningField(org.kie.pmml.api.models.MiningField) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) TargetField(org.kie.pmml.api.models.TargetField) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) KiePMMLOutputField(org.kie.pmml.commons.model.KiePMMLOutputField) OutputField(org.kie.pmml.api.models.OutputField) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) PMML_MODEL(org.kie.pmml.api.enums.PMML_MODEL) MINING_FUNCTION(org.kie.pmml.api.enums.MINING_FUNCTION) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) CommonCodegenUtils.addListPopulationByMethodCallExpr(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.addListPopulationByMethodCallExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 8 with KiePMMLInternalException

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

the class ModelImplementationProvider method getKiePMMLModelWithSourcesCompiled.

/**
 * Method provided only to have <b>drools</b> models working when invoked by a <code>MiningModel</code>
 * Default implementation provided for <b>not-drools</b> models.
 * @param compilationDTO
 * @return
 * @throws KiePMMLInternalException
 */
default KiePMMLModelWithSources getKiePMMLModelWithSourcesCompiled(final CompilationDTO<T> compilationDTO) {
    KiePMMLModelWithSources toReturn = getKiePMMLModelWithSources(compilationDTO);
    final Map<String, String> sourcesMap = ((HasSourcesMap) toReturn).getSourcesMap();
    try {
        compilationDTO.compileAndLoadClass(sourcesMap);
    } catch (Exception e) {
        throw new KiePMMLException(e);
    }
    return toReturn;
}
Also used : KiePMMLModelWithSources(org.kie.pmml.commons.model.KiePMMLModelWithSources) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) HasSourcesMap(org.kie.pmml.commons.model.HasSourcesMap) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 9 with KiePMMLInternalException

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

Example 10 with KiePMMLInternalException

use of org.kie.pmml.api.exceptions.KiePMMLInternalException 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()));
}
Also used : ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)

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