Search in sources :

Example 71 with KiePMMLException

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);
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 72 with KiePMMLException

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);
    }
}
Also used : KiePMMLTreeModel(org.kie.pmml.models.tree.model.KiePMMLTreeModel) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 73 with KiePMMLException

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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) HasClassLoaderMock(org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) KiePMMLTreeModel(org.kie.pmml.models.tree.model.KiePMMLTreeModel) TreeModel(org.dmg.pmml.tree.TreeModel) Expression(com.github.javaparser.ast.expr.Expression) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) MINING_FUNCTION(org.kie.pmml.api.enums.MINING_FUNCTION) Test(org.junit.Test)

Example 74 with KiePMMLException

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);
    }
}
Also used : KiePMMLInputDataException(org.kie.pmml.api.exceptions.KiePMMLInputDataException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) INVALID_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD)

Example 75 with KiePMMLException

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);
    }
}
Also used : MISSING_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.MISSING_VALUE_TREATMENT_METHOD) KiePMMLInputDataException(org.kie.pmml.api.exceptions.KiePMMLInputDataException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

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