Search in sources :

Example 11 with KiePMMLInternalException

use of org.kie.pmml.api.exceptions.KiePMMLInternalException in project drools by kiegroup.

the class TestingModelImplementationProvider method getKiePMMLTestModelSourcesMap.

private Map<String, String> getKiePMMLTestModelSourcesMap(final CompilationDTO<TestModel> compilationDTO) {
    String className = compilationDTO.getSimpleClassName();
    CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(className, compilationDTO.getPackageName(), KIE_PMML_TEST_MODEL_TEMPLATE_JAVA, KIE_PMML_TEST_MODEL_TEMPLATE);
    ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(className).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className));
    String modelName = compilationDTO.getModelName();
    final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
    setConstructor(className, constructorDeclaration, modelName);
    Map<String, String> toReturn = new HashMap<>();
    toReturn.put(getFullClassName(cloneCU), cloneCU.toString());
    return toReturn;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) HashMap(java.util.HashMap) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException)

Example 12 with KiePMMLInternalException

use of org.kie.pmml.api.exceptions.KiePMMLInternalException in project drools by kiegroup.

the class KiePMMLNodeFactory method populateEvaluateNode.

/**
 * Populate <b>nodeFunctions, score, predicateFunction</b> <code>VariableDeclarator</code>s initializers of the given <code>BlockStmt</code>
 *
 * @param toPopulate
 * @param nodeNamesDTO
 * @param fields
 * @param isRoot
 */
static void populateEvaluateNode(final JavaParserDTO toPopulate, final NodeNamesDTO nodeNamesDTO, final List<Field<?>> fields, final boolean isRoot) {
    String nodeClassName = nodeNamesDTO.nodeClassName;
    final BlockStmt evaluateNodeBody = isRoot ? toPopulate.evaluateRootNodeBody : toPopulate.getEvaluateNestedNodeMethodDeclaration(nodeClassName).getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_TEMPLATE, EVALUATE_NODE + nodeClassName)));
    // set 'predicate'
    populateEvaluateNodeWithPredicate(evaluateNodeBody, nodeNamesDTO.node.getPredicate(), fields);
    // set 'nodeFunctions'
    final List<String> nestedNodesFullClasses = nodeNamesDTO.getNestedNodesFullClassNames(toPopulate.packageName);
    populateEvaluateNodeWithNodeFunctions(evaluateNodeBody, nestedNodesFullClasses);
    // set 'score'
    populateEvaluateNodeWithScore(evaluateNodeBody, nodeNamesDTO.node.getScore());
    // set 'scoreDistributions'
    if (nodeNamesDTO.node.hasScoreDistributions()) {
        populateEvaluateNodeWithScoreDistributions(evaluateNodeBody, nodeNamesDTO.node.getScoreDistributions());
    }
    // set 'missingValuePenalty'
    if (nodeNamesDTO.missingValuePenalty != null) {
        populateEvaluateNodeWithMissingValuePenalty(evaluateNodeBody, nodeNamesDTO.missingValuePenalty);
    }
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException)

Example 13 with KiePMMLInternalException

use of org.kie.pmml.api.exceptions.KiePMMLInternalException 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 14 with KiePMMLInternalException

use of org.kie.pmml.api.exceptions.KiePMMLInternalException in project drools by kiegroup.

the class KiePMMLTreeModelFactory method setConstructor.

static void setConstructor(final TreeCompilationDTO compilationDTO, final ClassOrInterfaceDeclaration modelTemplate, final String fullNodeClassName) {
    KiePMMLModelFactoryUtils.init(compilationDTO, modelTemplate);
    final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
    final BlockStmt body = constructorDeclaration.getBody();
    // set predicate function
    MethodReferenceExpr nodeReference = new MethodReferenceExpr();
    nodeReference.setScope(new NameExpr(fullNodeClassName));
    nodeReference.setIdentifier("evaluateNode");
    CommonCodegenUtils.setAssignExpressionValue(body, "nodeFunction", nodeReference);
}
Also used : ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr)

Example 15 with KiePMMLInternalException

use of org.kie.pmml.api.exceptions.KiePMMLInternalException 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)

Aggregations

KiePMMLInternalException (org.kie.pmml.api.exceptions.KiePMMLInternalException)23 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)17 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)15 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)14 CompilationUnit (com.github.javaparser.ast.CompilationUnit)7 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)7 Expression (com.github.javaparser.ast.expr.Expression)7 NameExpr (com.github.javaparser.ast.expr.NameExpr)7 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)5 NodeList (com.github.javaparser.ast.NodeList)4 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)4 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)4 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)4 ExplicitConstructorInvocationStmt (com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)4 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)4 HashMap (java.util.HashMap)4 MethodReferenceExpr (com.github.javaparser.ast.expr.MethodReferenceExpr)3 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)3 Map (java.util.Map)3 MINING_FUNCTION (org.kie.pmml.api.enums.MINING_FUNCTION)3