Search in sources :

Example 66 with KiePMMLException

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

the class KiePMMLLocalTransformationsFactory method getKiePMMLLocalTransformationsVariableDeclaration.

/**
 * @param localTransformations
 * @return
 */
static BlockStmt getKiePMMLLocalTransformationsVariableDeclaration(final LocalTransformations localTransformations) {
    final MethodDeclaration methodDeclaration = LOCAL_TRANSFORMATIONS_TEMPLATE.getMethodsByName(GETKIEPMMLLOCALTRANSFORMATIONS).get(0).clone();
    final BlockStmt transformationDictionaryBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(transformationDictionaryBody, LOCAL_TRANSFORMATIONS).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, LOCAL_TRANSFORMATIONS, transformationDictionaryBody)));
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, LOCAL_TRANSFORMATIONS, methodDeclaration))).asMethodCallExpr();
    final BlockStmt toReturn = new BlockStmt();
    if (localTransformations.hasDerivedFields()) {
        NodeList<Expression> derivedFields = addDerivedField(toReturn, localTransformations.getDerivedFields());
        getChainedMethodCallExprFrom("withDerivedFields", initializer).setArguments(derivedFields);
    }
    transformationDictionaryBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 67 with KiePMMLException

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

the class KiePMMLNormDiscreteFactory method getNormDiscreteVariableDeclaration.

static BlockStmt getNormDiscreteVariableDeclaration(final String variableName, final NormDiscrete normDiscrete) {
    final MethodDeclaration methodDeclaration = NORMDISCRETE_TEMPLATE.getMethodsByName(GETKIEPMMLNORMDISCRETE).get(0).clone();
    final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, NORM_DISCRETE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, NORM_DISCRETE, toReturn)));
    variableDeclarator.setName(variableName);
    final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, NORM_DISCRETE, toReturn))).asObjectCreationExpr();
    final StringLiteralExpr nameExpr = new StringLiteralExpr(normDiscrete.getField().getValue());
    final Expression mapMissingToExpr = getExpressionForObject(normDiscrete.getMapMissingTo());
    objectCreationExpr.getArguments().set(0, nameExpr);
    objectCreationExpr.getArguments().set(2, new StringLiteralExpr((String) normDiscrete.getValue()));
    objectCreationExpr.getArguments().set(3, mapMissingToExpr);
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 68 with KiePMMLException

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

the class KiePMMLFactoryFactory method getFactorySourceCode.

public static Map<String, String> getFactorySourceCode(String factoryClassName, String packageName, Map<String, Boolean> generatedClassesModelTypeMap) {
    logger.trace("getFactorySourceCode {} {} {}", factoryClassName, packageName, generatedClassesModelTypeMap);
    String fullClassName = packageName + "." + factoryClassName;
    Map<String, String> toReturn = new HashMap<>();
    CompilationUnit templateCU = getFromFileName(KIE_PMML_MODEL_FACTORY_TEMPLATE_JAVA);
    CompilationUnit cloneCU = templateCU.clone();
    cloneCU.setPackageDeclaration(packageName);
    ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(KIE_PMML_MODEL_FACTORY_TEMPLATE).orElseThrow(() -> new RuntimeException(MAIN_CLASS_NOT_FOUND));
    modelTemplate.setName(factoryClassName);
    final FieldDeclaration fieldByName = modelTemplate.getFieldByName(KIE_PMML_MODELS_FIELD).orElseThrow(() -> new KiePMMLException(String.format("Failed to find FieldDeclaration %s in template" + " %s", KIE_PMML_MODELS_FIELD, KIE_PMML_MODEL_FACTORY_TEMPLATE_JAVA)));
    populateKiePmmlFields(fieldByName, generatedClassesModelTypeMap);
    toReturn.put(fullClassName, cloneCU.toString());
    return toReturn;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) HashMap(java.util.HashMap) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration)

Example 69 with KiePMMLException

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

the class KiePMMLNodeFactory method mergeNodeReferences.

/**
 * Adjust the <b>evaluateNode(?)</b> references to the ones declared in the given
 * <code>JavaParserDTO.nodeTemplate</code>
 *
 * @param toPopulate
 * @param nestedNodeNamesDTO
 * @param evaluateNodeInitializer
 */
static void mergeNodeReferences(final JavaParserDTO toPopulate, final NodeNamesDTO nestedNodeNamesDTO, final MethodCallExpr evaluateNodeInitializer) {
    final NodeList<Expression> evaluateNodeReferences = evaluateNodeInitializer.getArguments();
    final String expectedReference = String.format(PACKAGE_CLASS_TEMPLATE, toPopulate.packageName, nestedNodeNamesDTO.nodeClassName);
    Optional<MethodReferenceExpr> found = Optional.empty();
    for (Expression expression : evaluateNodeReferences) {
        if (expectedReference.equals(expression.asMethodReferenceExpr().getScope().toString())) {
            found = Optional.of(expression.asMethodReferenceExpr());
            break;
        }
    }
    final MethodReferenceExpr evaluateNodeReference = found.orElseThrow(() -> new KiePMMLException(String.format(MISSING_METHOD_REFERENCE_TEMPLATE, expectedReference, evaluateNodeInitializer)));
    String identifier = EVALUATE_NODE + nestedNodeNamesDTO.nodeClassName;
    evaluateNodeReference.setScope(new NameExpr(toPopulate.nodeClassName));
    evaluateNodeReference.setIdentifier(identifier);
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) NameExpr(com.github.javaparser.ast.expr.NameExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr)

Example 70 with KiePMMLException

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

the class KiePMMLNodeFactory method getKiePMMLNode.

public static KiePMMLNode getKiePMMLNode(final Node node, final List<Field<?>> fields, final String packageName, final Double missingValuePenalty, final HasClassLoader hasClassLoader) {
    logger.trace("getKiePMMLTreeNode {} {}", packageName, node);
    final KiePMMLNodeFactory.NodeNamesDTO nodeNamesDTO = new KiePMMLNodeFactory.NodeNamesDTO(node, createNodeClassName(), null, missingValuePenalty);
    final Map<String, String> sourcesMap = getKiePMMLNodeSourcesMap(nodeNamesDTO, fields, packageName);
    String fullClassName = packageName + "." + nodeNamesDTO.nodeClassName;
    try {
        Class<?> kiePMMLNodeClass = hasClassLoader.compileAndLoadClass(sourcesMap, fullClassName);
        return (KiePMMLNode) kiePMMLNodeClass.newInstance();
    } catch (Exception e) {
        throw new KiePMMLException(e);
    }
}
Also used : KiePMMLNode(org.kie.pmml.models.tree.model.KiePMMLNode) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) 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