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
}
});
}
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);
}
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);
}
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
}
}
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;
}
Aggregations