use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLAttributeFactory method getAttributeVariableDeclaration.
static BlockStmt getAttributeVariableDeclaration(final String variableName, final Attribute attribute, final List<Field<?>> fields) {
final MethodDeclaration methodDeclaration = ATTRIBUTE_TEMPLATE.getMethodsByName(GETKIEPMMLATTRIBUTE).get(0).clone();
final BlockStmt attributeBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(attributeBody, ATTRIBUTE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, ATTRIBUTE, attributeBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
String predicateVariableName = String.format("%s_Predicate", variableName);
BlockStmt toAdd = getKiePMMLPredicate(predicateVariableName, attribute.getPredicate(), fields);
toAdd.getStatements().forEach(toReturn::addStatement);
final Expression complexPartialScoreExpression;
if (attribute.getComplexPartialScore() != null) {
String complexPartialScoreVariableName = String.format("%s_ComplexPartialScore", variableName);
toAdd = getComplexPartialScoreVariableDeclaration(complexPartialScoreVariableName, attribute.getComplexPartialScore());
toAdd.getStatements().forEach(toReturn::addStatement);
complexPartialScoreExpression = new NameExpr(complexPartialScoreVariableName);
} else {
complexPartialScoreExpression = new NullLiteralExpr();
}
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, ATTRIBUTE, attributeBody))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
builder.setArgument(0, new StringLiteralExpr(variableName));
builder.setArgument(2, new NameExpr(predicateVariableName));
getChainedMethodCallExprFrom("withPartialScore", initializer).setArgument(0, getExpressionForObject(attribute.getPartialScore()));
getChainedMethodCallExprFrom("withComplexPartialScore", initializer).setArgument(0, complexPartialScoreExpression);
attributeBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException 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));
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class RegressionModelImplementationProvider method getKiePMMLModel.
@Override
public KiePMMLRegressionModel getKiePMMLModel(final CompilationDTO<RegressionModel> compilationDTO) {
logger.trace("getKiePMMLModel {} {} {} {}", compilationDTO.getPackageName(), compilationDTO.getFields(), compilationDTO.getModel(), compilationDTO.getHasClassloader());
validate(compilationDTO.getFields(), compilationDTO.getModel());
try {
return KiePMMLRegressionModelFactory.getKiePMMLRegressionModelClasses(RegressionCompilationDTO.fromCompilationDTO(compilationDTO));
} catch (IOException | IllegalAccessException | InstantiationException e) {
throw new KiePMMLException(e.getMessage(), e);
}
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class RegressionModelImplementationProvider method validateClassification.
private void validateClassification(final List<Field<?>> fields, final RegressionModel toValidate) {
final String categoricalTargeName = getCategoricalTargetName(fields, toValidate);
final OP_TYPE opType = getOpType(fields, toValidate, categoricalTargeName);
switch(opType) {
case CATEGORICAL:
validateClassificationCategorical(fields, toValidate, categoricalTargeName);
break;
case ORDINAL:
validateClassificationOrdinal(toValidate);
break;
default:
throw new KiePMMLException("Invalid target type " + opType);
}
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLModelFactoryUtils method setConstructorSuperNameInvocation.
/**
* Set the <b>name</b> parameter on <b>super</b> invocation
* @param generatedClassName
* @param constructorDeclaration
* @param name
*/
public static void setConstructorSuperNameInvocation(final String generatedClassName, final ConstructorDeclaration constructorDeclaration, final String name) {
constructorDeclaration.setName(generatedClassName);
final BlockStmt body = constructorDeclaration.getBody();
final ExplicitConstructorInvocationStmt superStatement = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body).orElseThrow(() -> new KiePMMLException(String.format(MISSING_CONSTRUCTOR_IN_BODY, body)));
CommonCodegenUtils.setExplicitConstructorInvocationStmtArgument(superStatement, "name", String.format("\"%s\"", name));
}
Aggregations