use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class PMMLMiningModelEvaluatorTest method getKiePMMLNameValueWeightNoNumber.
@Test
public void getKiePMMLNameValueWeightNoNumber() {
final PMML4Result pmml4Result = getPMML4Result("OBJ");
VALUE_WEIGHT_METHODS.forEach(multipleModelMethod -> {
try {
evaluator.getKiePMMLNameValue(pmml4Result, multipleModelMethod, 34.2);
fail(multipleModelMethod + " is supposed to throw exception because raw object is not a number");
} catch (KiePMMLException e) {
// expected
}
});
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLApplyFactory method getApplyVariableDeclaration.
static BlockStmt getApplyVariableDeclaration(final String variableName, final Apply apply) {
final MethodDeclaration methodDeclaration = APPLY_TEMPLATE.getMethodsByName(GETKIEPMMLAPPLY).get(0).clone();
final BlockStmt applyBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(applyBody, APPLY).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, APPLY, applyBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
int counter = 0;
final NodeList<Expression> arguments = new NodeList<>();
for (org.dmg.pmml.Expression expression : apply.getExpressions()) {
String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
arguments.add(new NameExpr(nestedVariableName));
BlockStmt toAdd = getKiePMMLExpressionBlockStmt(nestedVariableName, expression);
toAdd.getStatements().forEach(toReturn::addStatement);
counter++;
}
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, APPLY, toReturn))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
final StringLiteralExpr nameExpr = new StringLiteralExpr(variableName);
final StringLiteralExpr functionExpr = new StringLiteralExpr(apply.getFunction());
builder.setArgument(0, nameExpr);
builder.setArgument(2, functionExpr);
getChainedMethodCallExprFrom("withDefaultValue", initializer).setArgument(0, getExpressionForObject(apply.getDefaultValue()));
getChainedMethodCallExprFrom("withMapMissingTo", initializer).setArgument(0, getExpressionForObject(apply.getMapMissingTo()));
final Expression invalidTreatmentExpr = apply.getInvalidValueTreatment() != null ? new StringLiteralExpr(apply.getInvalidValueTreatment().value()) : new NullLiteralExpr();
getChainedMethodCallExprFrom("withInvalidValueTreatmentMethod", initializer).setArgument(0, invalidTreatmentExpr);
getChainedMethodCallExprFrom("asList", initializer).setArguments(arguments);
applyBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLConstantFactory method getConstantVariableDeclaration.
static BlockStmt getConstantVariableDeclaration(final String variableName, final Constant constant) {
final MethodDeclaration methodDeclaration = CONSTANT_TEMPLATE.getMethodsByName(GETKIEPMMLCONSTANT).get(0).clone();
final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, CONSTANT).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, CONSTANT, toReturn)));
variableDeclarator.setName(variableName);
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, CONSTANT, toReturn))).asObjectCreationExpr();
final StringLiteralExpr nameExpr = new StringLiteralExpr(variableName);
final Expression valueExpr = getExpressionForObject(constant.getValue());
final Expression dataTypeExpression = getExpressionForDataType(constant.getDataType());
objectCreationExpr.getArguments().set(0, nameExpr);
objectCreationExpr.getArguments().set(2, valueExpr);
objectCreationExpr.getArguments().set(3, dataTypeExpression);
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLDiscretizeFactory method getDiscretizeVariableDeclaration.
static BlockStmt getDiscretizeVariableDeclaration(final String variableName, final Discretize discretize) {
final MethodDeclaration methodDeclaration = DISCRETIZE_TEMPLATE.getMethodsByName(GETKIEPMMLDISCRETIZE).get(0).clone();
final BlockStmt discretizeBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(discretizeBody, DISCRETIZE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, DISCRETIZE, discretizeBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
int counter = 0;
final NodeList<Expression> arguments = new NodeList<>();
for (DiscretizeBin discretizeBin : discretize.getDiscretizeBins()) {
String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
arguments.add(new NameExpr(nestedVariableName));
BlockStmt toAdd = getDiscretizeBinVariableDeclaration(nestedVariableName, discretizeBin);
toAdd.getStatements().forEach(toReturn::addStatement);
counter++;
}
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, DISCRETIZE, toReturn))).asObjectCreationExpr();
final Expression nameExpr = new StringLiteralExpr(discretize.getField().getValue());
final Expression mapMissingToExpr = getExpressionForObject(discretize.getMapMissingTo());
final Expression defaultValueExpr = getExpressionForObject(discretize.getDefaultValue());
final Expression dataTypeExpression = getExpressionForDataType(discretize.getDataType());
objectCreationExpr.getArguments().set(0, nameExpr);
objectCreationExpr.getArguments().get(2).asMethodCallExpr().setArguments(arguments);
objectCreationExpr.getArguments().set(3, mapMissingToExpr);
objectCreationExpr.getArguments().set(4, defaultValueExpr);
objectCreationExpr.getArguments().set(5, dataTypeExpression);
discretizeBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLFalsePredicateFactory method getFalsePredicateVariableDeclaration.
static BlockStmt getFalsePredicateVariableDeclaration(final String variableName, final False falsePredicate) {
final MethodDeclaration methodDeclaration = FALSEPREDICATE_TEMPLATE.getMethodsByName(GETKIEPMMLFALSEPREDICATE).get(0).clone();
final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, FALSEPREDICATE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, FALSEPREDICATE, toReturn)));
variableDeclarator.setName(variableName);
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, FALSEPREDICATE, toReturn))).asObjectCreationExpr();
final StringLiteralExpr nameExpr = new StringLiteralExpr(variableName);
objectCreationExpr.getArguments().set(0, nameExpr);
return toReturn;
}
Aggregations