use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLNodeFactory method mergeNode.
/**
* Adjust the <b>evaluateNode(?)</b> references to the ones declared in the given
* <code>JavaParserDTO.nodeTemplate</code>
*
* @param toPopulate
* @param nestedNodeNamesDTO
*/
static void mergeNode(final JavaParserDTO toPopulate, final NodeNamesDTO nestedNodeNamesDTO) {
final MethodCallExpr evaluateNodeInitializer;
// node
if (Objects.equals(toPopulate.nodeClassName, nestedNodeNamesDTO.parentNodeClassName)) {
evaluateNodeInitializer = toPopulate.evaluateRootNodeReferencesDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, NODE_FUNCTIONS, toPopulate.evaluateRootNodeReferencesDeclarator))).asMethodCallExpr();
} else {
String expected = EVALUATE_NODE + nestedNodeNamesDTO.parentNodeClassName;
final MethodDeclaration evaluateNestedNodeMethod = toPopulate.nodeTemplate.getMethodsByName(expected).get(0);
final BlockStmt evaluateNestedNodeBody = evaluateNestedNodeMethod.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_IN_METHOD, expected)));
final VariableDeclarator nestedNodeVariableDeclarator = evaluateNestedNodeBody.findAll(VariableDeclarator.class).stream().filter(variableDeclarator -> variableDeclarator.getName().asString().equals(NODE_FUNCTIONS)).findFirst().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_VARIABLE_IN_BODY, expected, evaluateNestedNodeBody)));
evaluateNodeInitializer = nestedNodeVariableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, NODE_FUNCTIONS, nestedNodeVariableDeclarator))).asMethodCallExpr();
}
mergeNodeReferences(toPopulate, nestedNodeNamesDTO, evaluateNodeInitializer);
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLTreeModelFactory method getKiePMMLTreeModel.
public static KiePMMLTreeModel getKiePMMLTreeModel(final TreeCompilationDTO compilationDTO) {
logger.trace("getKiePMMLTreeModel {} {}", compilationDTO.getPackageName(), compilationDTO.getModel());
Map<String, String> sourcesMap = getKiePMMLTreeModelSourcesMap(compilationDTO);
try {
Class<?> kiePMMLTreeModelClass = compilationDTO.compileAndLoadClass(sourcesMap);
return (KiePMMLTreeModel) kiePMMLTreeModelClass.newInstance();
} catch (Exception e) {
throw new KiePMMLException(e);
}
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class KiePMMLTreeModelFactoryTest method setConstructor.
@Test
public void setConstructor() {
String className = getSanitizedClassName(treeModel1.getModelName());
CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(className, PACKAGE_NAME, KIE_PMML_TREE_MODEL_TEMPLATE_JAVA, KIE_PMML_TREE_MODEL_TEMPLATE);
ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(className).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className));
String targetField = "whatIdo";
String fullNodeClassName = "full.Node.ClassName";
CommonCompilationDTO<TreeModel> source = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml1, treeModel1, new HasClassLoaderMock());
KiePMMLTreeModelFactory.setConstructor(TreeCompilationDTO.fromCompilationDTO(source), modelTemplate, fullNodeClassName);
ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName()))).clone();
BlockStmt body = constructorDeclaration.getBody();
// targetField
Optional<AssignExpr> optRetrieved = CommonCodegenUtils.getAssignExpression(body, "targetField");
assertTrue(optRetrieved.isPresent());
AssignExpr retrieved = optRetrieved.get();
Expression initializer = retrieved.getValue();
assertTrue(initializer instanceof StringLiteralExpr);
String expected = String.format("\"%s\"", targetField);
assertEquals(expected, initializer.toString());
// miningFunction
optRetrieved = CommonCodegenUtils.getAssignExpression(body, "miningFunction");
assertTrue(optRetrieved.isPresent());
retrieved = optRetrieved.get();
initializer = retrieved.getValue();
assertTrue(initializer instanceof NameExpr);
MINING_FUNCTION miningFunction = MINING_FUNCTION.byName(treeModel1.getMiningFunction().value());
expected = miningFunction.getClass().getName() + "." + miningFunction.name();
assertEquals(expected, initializer.toString());
// pmmlMODEL
optRetrieved = CommonCodegenUtils.getAssignExpression(body, "pmmlMODEL");
assertTrue(optRetrieved.isPresent());
retrieved = optRetrieved.get();
initializer = retrieved.getValue();
assertTrue(initializer instanceof NameExpr);
expected = PMML_MODEL.TREE_MODEL.getClass().getName() + "." + PMML_MODEL.TREE_MODEL.name();
assertEquals(expected, initializer.toString());
// nodeFunction
optRetrieved = CommonCodegenUtils.getAssignExpression(body, "nodeFunction");
assertTrue(optRetrieved.isPresent());
retrieved = optRetrieved.get();
initializer = retrieved.getValue();
assertTrue(initializer instanceof MethodReferenceExpr);
expected = fullNodeClassName;
assertEquals(expected, ((MethodReferenceExpr) initializer).getScope().toString());
expected = "evaluateNode";
assertEquals(expected, ((MethodReferenceExpr) initializer).getIdentifier());
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class PreProcess method manageInvalidValues.
/**
* Manage the <b>invalid value</b> of the given <code>ParameterInfo</code> depending on the
* <code>INVALID_VALUE_TREATMENT_METHOD</code>
* of the given <code>MiningField</code>, <b>eventually adding the ParameterInfo to the list of the ones to be
* removed from input data</b>
* @param miningField
* @param parameterInfo
* @param toRemove
*/
static void manageInvalidValues(final KiePMMLMiningField miningField, final ParameterInfo parameterInfo, final List<ParameterInfo> toRemove) {
INVALID_VALUE_TREATMENT_METHOD invalidValueTreatmentMethod = miningField.getInvalidValueTreatmentMethod() != null ? miningField.getInvalidValueTreatmentMethod() : INVALID_VALUE_TREATMENT_METHOD.RETURN_INVALID;
Object originalValue = parameterInfo.getValue();
switch(invalidValueTreatmentMethod) {
case RETURN_INVALID:
throw new KiePMMLInputDataException("Invalid value " + originalValue + " for " + miningField.getName());
case AS_MISSING:
toRemove.add(parameterInfo);
break;
case AS_IS:
break;
case AS_VALUE:
String invalidValueReplacement = miningField.getInvalidValueReplacement();
if (invalidValueReplacement == null) {
throw new KiePMMLInputDataException("Missing required invalidValueReplacement for " + miningField.getName());
} else {
Object requiredValue = miningField.getDataType().getActualValue(invalidValueReplacement);
parameterInfo.setType(miningField.getDataType().getMappedClass());
parameterInfo.setValue(requiredValue);
}
break;
default:
throw new KiePMMLException("Unmanaged INVALID_VALUE_TREATMENT_METHOD " + invalidValueTreatmentMethod);
}
}
use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.
the class PreProcess method manageMissingValues.
/**
* Manage the <b>missing value</b> depending on the <code>INVALID_VALUE_TREATMENT_METHOD</code>
* of the given <code>MiningField</code>, <b>eventually adding default ont to input data</b>
* @param miningField
* @param requestData
*/
static void manageMissingValues(final KiePMMLMiningField miningField, final PMMLRequestData requestData) {
MISSING_VALUE_TREATMENT_METHOD missingValueTreatmentMethod = miningField.getMissingValueTreatmentMethod() != null ? miningField.getMissingValueTreatmentMethod() : MISSING_VALUE_TREATMENT_METHOD.RETURN_INVALID;
switch(missingValueTreatmentMethod) {
case RETURN_INVALID:
throw new KiePMMLInputDataException("Missing required value for " + miningField.getName());
case AS_IS:
case AS_MEAN:
case AS_MODE:
case AS_MEDIAN:
case AS_VALUE:
String missingValueReplacement = miningField.getMissingValueReplacement();
if (missingValueReplacement != null) {
Object requiredValue = miningField.getDataType().getActualValue(missingValueReplacement);
requestData.addRequestParam(miningField.getName(), requiredValue);
}
break;
default:
throw new KiePMMLException("Unmanaged INVALID_VALUE_TREATMENT_METHOD " + missingValueTreatmentMethod);
}
}
Aggregations