Search in sources :

Example 1 with KiePMMLInternalException

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

the class ModelImplementationProvider method getKiePMMLModelWithSourcesCompiled.

/**
 * Method provided only to have <b>drools</b> models working when invoked by a <code>MiningModel</code>
 * Default implementation provided for <b>not-drools</b> models.
 * @param compilationDTO
 * @return
 * @throws KiePMMLInternalException
 */
default KiePMMLModelWithSources getKiePMMLModelWithSourcesCompiled(final CompilationDTO<T> compilationDTO) {
    KiePMMLModelWithSources toReturn = getKiePMMLModelWithSources(compilationDTO);
    final Map<String, String> sourcesMap = ((HasSourcesMap) toReturn).getSourcesMap();
    try {
        compilationDTO.compileAndLoadClass(sourcesMap);
    } catch (Exception e) {
        throw new KiePMMLException(e);
    }
    return toReturn;
}
Also used : KiePMMLModelWithSources(org.kie.pmml.commons.model.KiePMMLModelWithSources) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) HasSourcesMap(org.kie.pmml.commons.model.HasSourcesMap) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 2 with KiePMMLInternalException

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

the class KiePMMLMiningFieldFactory method getIntervalsExpressions.

private static NodeList<Expression> getIntervalsExpressions(final DataField dataField) {
    final NodeList<Expression> toReturn = new NodeList<>();
    if (dataField.hasIntervals()) {
        dataField.getIntervals().forEach(interval -> {
            BlockStmt intervalStmt = getIntervalVariableDeclaration("name", interval);
            Expression toAdd = intervalStmt.getStatement(0).asExpressionStmt().getExpression().asVariableDeclarationExpr().getVariable(0).getInitializer().orElseThrow(() -> new KiePMMLInternalException(String.format("Failed to create initializer " + "for " + "Interval %s", interval)));
            toReturn.add(toAdd);
        });
    }
    return toReturn;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException)

Example 3 with KiePMMLInternalException

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

the class KiePMMLModelFactoryUtils method init.

/**
 * Initialize the given <code>ClassOrInterfaceDeclaration</code> with all the <b>common</b> code needed to
 * generate a <code>KiePMMLModel</code>
 * @param compilationDTO
 * @param modelTemplate
 */
public static void init(final CompilationDTO<? extends Model> compilationDTO, final ClassOrInterfaceDeclaration modelTemplate) {
    final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
    final String name = compilationDTO.getModelName();
    final String generatedClassName = compilationDTO.getSimpleClassName();
    final List<MiningField> miningFields = compilationDTO.getKieMiningFields();
    final List<OutputField> outputFields = compilationDTO.getKieOutputFields();
    final List<TargetField> targetFields = compilationDTO.getKieTargetFields();
    final Expression miningFunctionExpression;
    if (compilationDTO.getMINING_FUNCTION() != null) {
        MINING_FUNCTION miningFunction = compilationDTO.getMINING_FUNCTION();
        miningFunctionExpression = new NameExpr(miningFunction.getClass().getName() + "." + miningFunction.name());
    } else {
        miningFunctionExpression = new NullLiteralExpr();
    }
    final PMML_MODEL pmmlModelEnum = compilationDTO.getPMML_MODEL();
    final NameExpr pmmlMODELExpression = new NameExpr(pmmlModelEnum.getClass().getName() + "." + pmmlModelEnum.name());
    String targetFieldName = compilationDTO.getTargetFieldName();
    final Expression targetFieldExpression;
    if (targetFieldName != null) {
        targetFieldExpression = new StringLiteralExpr(targetFieldName);
    } else {
        targetFieldExpression = new NullLiteralExpr();
    }
    setKiePMMLModelConstructor(generatedClassName, constructorDeclaration, name, miningFields, outputFields, targetFields);
    addTransformationsInClassOrInterfaceDeclaration(modelTemplate, compilationDTO.getTransformationDictionary(), compilationDTO.getLocalTransformations());
    final BlockStmt body = constructorDeclaration.getBody();
    CommonCodegenUtils.setAssignExpressionValue(body, "pmmlMODEL", pmmlMODELExpression);
    CommonCodegenUtils.setAssignExpressionValue(body, "miningFunction", miningFunctionExpression);
    CommonCodegenUtils.setAssignExpressionValue(body, "targetField", targetFieldExpression);
    addGetCreatedKiePMMLMiningFieldsMethod(modelTemplate, compilationDTO.getMiningSchema().getMiningFields(), compilationDTO.getFields());
    MethodCallExpr getCreatedKiePMMLMiningFieldsExpr = new MethodCallExpr();
    getCreatedKiePMMLMiningFieldsExpr.setScope(new ThisExpr());
    getCreatedKiePMMLMiningFieldsExpr.setName(GET_CREATED_KIEPMMLMININGFIELDS);
    CommonCodegenUtils.setAssignExpressionValue(body, "kiePMMLMiningFields", getCreatedKiePMMLMiningFieldsExpr);
    if (compilationDTO.getOutput() != null) {
        addGetCreatedKiePMMLOutputFieldsMethod(modelTemplate, compilationDTO.getOutput().getOutputFields());
        MethodCallExpr getCreatedKiePMMLOutputFieldsExpr = new MethodCallExpr();
        getCreatedKiePMMLOutputFieldsExpr.setScope(new ThisExpr());
        getCreatedKiePMMLOutputFieldsExpr.setName(GET_CREATED_KIEPMMLOUTPUTFIELDS);
        CommonCodegenUtils.setAssignExpressionValue(body, "kiePMMLOutputFields", getCreatedKiePMMLOutputFieldsExpr);
    }
}
Also used : KiePMMLMiningField(org.kie.pmml.commons.model.KiePMMLMiningField) MiningField(org.kie.pmml.api.models.MiningField) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) TargetField(org.kie.pmml.api.models.TargetField) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) KiePMMLOutputField(org.kie.pmml.commons.model.KiePMMLOutputField) OutputField(org.kie.pmml.api.models.OutputField) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) PMML_MODEL(org.kie.pmml.api.enums.PMML_MODEL) MINING_FUNCTION(org.kie.pmml.api.enums.MINING_FUNCTION) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) CommonCodegenUtils.addListPopulationByMethodCallExpr(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.addListPopulationByMethodCallExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 4 with KiePMMLInternalException

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

the class CommonCodegenUtils method addMethod.

/**
 * Add a <code>MethodDeclaration</code> to the class
 * @param methodTemplate
 * @param tableTemplate
 * @param methodName
 * @return
 */
public static MethodDeclaration addMethod(final MethodDeclaration methodTemplate, final ClassOrInterfaceDeclaration tableTemplate, final String methodName) {
    final BlockStmt body = methodTemplate.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_TEMPLATE, methodTemplate.getName())));
    final MethodDeclaration toReturn = tableTemplate.addMethod(methodName).setBody(body);
    toReturn.setModifiers(methodTemplate.getModifiers());
    methodTemplate.getParameters().forEach(toReturn::addParameter);
    toReturn.setType(methodTemplate.getType());
    return toReturn;
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException)

Example 5 with KiePMMLInternalException

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

the class CommonCodegenUtils method populateListInListGetter.

/**
 * Method to be used to populate a <code>List</code> inside a getter method meant to return only that <code>List</code>
 * @param toAdd
 * @param methodDeclaration
 * @param listName
 */
public static void populateListInListGetter(final List<? extends Expression> toAdd, final MethodDeclaration methodDeclaration, final String listName) {
    final BlockStmt body = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_IN_METHOD, methodDeclaration)));
    Optional<ReturnStmt> oldReturn = body.getStatements().parallelStream().filter(statement -> statement instanceof ReturnStmt).map(ReturnStmt.class::cast).findFirst();
    oldReturn.ifPresent(Node::remove);
    toAdd.forEach(expression -> {
        NodeList<Expression> arguments = NodeList.nodeList(expression);
        MethodCallExpr methodCallExpr = new MethodCallExpr();
        methodCallExpr.setScope(new NameExpr(listName));
        methodCallExpr.setName("add");
        methodCallExpr.setArguments(arguments);
        ExpressionStmt expressionStmt = new ExpressionStmt();
        expressionStmt.setExpression(methodCallExpr);
        body.addStatement(expressionStmt);
    });
    body.addStatement(getReturnStmt(listName));
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Node(com.github.javaparser.ast.Node) NameExpr(com.github.javaparser.ast.expr.NameExpr) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

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