Search in sources :

Example 21 with KiePMMLException

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

the class PMMLMiningModelEvaluatorTest method getKiePMMLNameValueNotImplemented.

@Test
public void getKiePMMLNameValueNotImplemented() {
    final PMML4Result pmml4Result = getPMML4Result("OBJ");
    NOT_IMPLEMENTED_METHODS.forEach(multipleModelMethod -> {
        try {
            evaluator.getKiePMMLNameValue(pmml4Result, multipleModelMethod, 34.2);
            fail(multipleModelMethod + " is supposed to throw exception because not implemented");
        } catch (KiePMMLException e) {
        // expected
        }
    });
}
Also used : PMML4Result(org.kie.api.pmml.PMML4Result) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) PMMLContextTest(org.kie.pmml.commons.testingutility.PMMLContextTest) Test(org.junit.Test)

Example 22 with KiePMMLException

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

the class KiePMMLRegressionTableFactory method setStaticGetter.

// not-public code-generation
static void setStaticGetter(final RegressionTable regressionTable, final RegressionCompilationDTO compilationDTO, final MethodDeclaration staticGetterMethod, final String variableName) {
    final BlockStmt regressionTableBody = staticGetterMethod.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, staticGetterMethod)));
    final BlockStmt newBody = new BlockStmt();
    // populate maps
    String numericFunctionMapName = String.format(VARIABLE_NAME_TEMPLATE, NUMERIC_FUNCTION_MAP, variableName);
    final Map<String, Expression> numericPredictorsMap = getNumericPredictorsExpressions(regressionTable.getNumericPredictors());
    createPopulatedHashMap(newBody, numericFunctionMapName, Arrays.asList(String.class.getSimpleName(), "SerializableFunction<Double, Double>"), numericPredictorsMap);
    final Map<String, Expression> categoricalPredictorFunctionsMap = getCategoricalPredictorsExpressions(regressionTable.getCategoricalPredictors(), newBody, variableName);
    String categoricalFunctionMapName = String.format(VARIABLE_NAME_TEMPLATE, CATEGORICAL_FUNCTION_MAP, variableName);
    createPopulatedHashMap(newBody, categoricalFunctionMapName, Arrays.asList(String.class.getSimpleName(), "SerializableFunction<String, " + "Double>"), categoricalPredictorFunctionsMap);
    String predictorTermsFunctionMapName = String.format(VARIABLE_NAME_TEMPLATE, PREDICTOR_TERM_FUNCTION_MAP, variableName);
    final Map<String, Expression> predictorTermsMap = getPredictorTermFunctions(regressionTable.getPredictorTerms());
    createPopulatedHashMap(newBody, predictorTermsFunctionMapName, Arrays.asList(String.class.getSimpleName(), "SerializableFunction<Map" + "<String, " + "Object>, Double>"), predictorTermsMap);
    final VariableDeclarator variableDeclarator = getVariableDeclarator(regressionTableBody, TO_RETURN).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TO_RETURN, regressionTableBody)));
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TO_RETURN, regressionTableBody))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    builder.setArgument(0, new StringLiteralExpr(variableName));
    getChainedMethodCallExprFrom("withNumericFunctionMap", initializer).setArgument(0, new NameExpr(numericFunctionMapName) {
    });
    getChainedMethodCallExprFrom("withCategoricalFunctionMap", initializer).setArgument(0, new NameExpr(categoricalFunctionMapName));
    getChainedMethodCallExprFrom("withPredictorTermsFunctionMap", initializer).setArgument(0, new NameExpr(predictorTermsFunctionMapName));
    getChainedMethodCallExprFrom("withIntercept", initializer).setArgument(0, getExpressionForObject(regressionTable.getIntercept().doubleValue()));
    getChainedMethodCallExprFrom("withTargetField", initializer).setArgument(0, getExpressionForObject(compilationDTO.getTargetFieldName()));
    getChainedMethodCallExprFrom("withTargetCategory", initializer).setArgument(0, getExpressionForObject(regressionTable.getTargetCategory()));
    final Expression resultUpdaterExpression = getResultUpdaterExpression(compilationDTO.getDefaultNormalizationMethod());
    getChainedMethodCallExprFrom("withResultUpdater", initializer).setArgument(0, resultUpdaterExpression);
    regressionTableBody.getStatements().forEach(newBody::addStatement);
    staticGetterMethod.setBody(newBody);
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 23 with KiePMMLException

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

the class KiePMMLRegressionModelFactory method setStaticGetter.

static void setStaticGetter(final CompilationDTO<RegressionModel> compilationDTO, final ClassOrInterfaceDeclaration modelTemplate, final String nestedTable) {
    KiePMMLModelFactoryUtils.initStaticGetter(compilationDTO, modelTemplate);
    final BlockStmt body = getMethodDeclarationBlockStmt(modelTemplate, GET_MODEL);
    final VariableDeclarator variableDeclarator = getVariableDeclarator(body, TO_RETURN).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TO_RETURN, body)));
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TO_RETURN, body))).asMethodCallExpr();
    MethodCallExpr methodCallExpr = new MethodCallExpr();
    methodCallExpr.setScope(new NameExpr(nestedTable));
    methodCallExpr.setName(GETKIEPMML_TABLE);
    getChainedMethodCallExprFrom("withAbstractKiePMMLTable", initializer).setArgument(0, methodCallExpr);
}
Also used : CommonCodegenUtils.getMethodDeclarationBlockStmt(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getMethodDeclarationBlockStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 24 with KiePMMLException

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

the class RegressionModelImplementationProviderTest method validateNoRegressionTables.

@Test
public void validateNoRegressionTables() throws Exception {
    final PMML pmml = TestUtils.loadFromFile(SOURCE_1);
    assertNotNull(pmml);
    assertEquals(1, pmml.getModels().size());
    assertTrue(pmml.getModels().get(0) instanceof RegressionModel);
    RegressionModel regressionModel = (RegressionModel) pmml.getModels().get(0);
    regressionModel.getRegressionTables().clear();
    final List<Field<?>> fields = getFieldsFromDataDictionary(pmml.getDataDictionary());
    try {
        PROVIDER.validate(fields, regressionModel);
        fail("Expecting validation failure due to missing RegressionTables");
    } catch (KiePMMLException e) {
    // Expected
    }
    regressionModel = new RegressionModel(regressionModel.getMiningFunction(), regressionModel.getMiningSchema(), null);
    try {
        PROVIDER.validate(fields, regressionModel);
        fail("Expecting validation failure due to missing RegressionTables");
    } catch (KiePMMLException e) {
    // Expected
    }
}
Also used : Field(org.dmg.pmml.Field) PMML(org.dmg.pmml.PMML) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) RegressionModel(org.dmg.pmml.regression.RegressionModel) KiePMMLRegressionModel(org.kie.pmml.models.regression.model.KiePMMLRegressionModel) Test(org.junit.Test)

Example 25 with KiePMMLException

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

the class KiePMMLComplexPartialScoreFactory method getComplexPartialScoreVariableDeclaration.

static BlockStmt getComplexPartialScoreVariableDeclaration(final String variableName, final ComplexPartialScore complexPartialScore) {
    final MethodDeclaration methodDeclaration = COMPLEX_PARTIAL_SCORE_TEMPLATE.getMethodsByName(GETKIEPMMLCOMPLEXPARTIALSCORE).get(0).clone();
    final BlockStmt complexPartialScoreBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(complexPartialScoreBody, COMPLEX_PARTIAL_SCORE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, COMPLEX_PARTIAL_SCORE, complexPartialScoreBody)));
    variableDeclarator.setName(variableName);
    final BlockStmt toReturn = new BlockStmt();
    String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, 0);
    BlockStmt toAdd = getKiePMMLExpressionBlockStmt(nestedVariableName, complexPartialScore.getExpression());
    toAdd.getStatements().forEach(toReturn::addStatement);
    final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, COMPLEX_PARTIAL_SCORE, toReturn))).asObjectCreationExpr();
    objectCreationExpr.getArguments().set(0, new StringLiteralExpr(variableName));
    objectCreationExpr.getArguments().set(2, new NameExpr(nestedVariableName));
    complexPartialScoreBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

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