Search in sources :

Example 61 with KiePMMLException

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

the class KiePMMLRowFactory method getRowVariableDeclaration.

static BlockStmt getRowVariableDeclaration(final String variableName, final Row row) {
    final MethodDeclaration methodDeclaration = ROW_TEMPLATE.getMethodsByName(GETKIEPMMLROW).get(0).clone();
    final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final String columnValuesVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, COLUMN_VALUES);
    final VariableDeclarator columnValuesVariableDeclarator = getVariableDeclarator(toReturn, COLUMN_VALUES).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, ROW, toReturn)));
    columnValuesVariableDeclarator.setName(columnValuesVariableName);
    final MethodCallExpr columnValuesVariableInit = columnValuesVariableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, COLUMN_VALUES, toReturn))).asMethodCallExpr();
    final MethodCallExpr columnValuesVariableScope = columnValuesVariableInit.getScope().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, COLUMN_VALUES, toReturn))).asMethodCallExpr();
    final ArrayCreationExpr columnValuesVariableArray = columnValuesVariableScope.getArguments().get(0).asArrayCreationExpr();
    final ArrayInitializerExpr columnValuesVariableArrayInit = columnValuesVariableArray.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, COLUMN_VALUES, toReturn))).asArrayInitializerExpr();
    Map<String, Object> rowDataMap = getRowDataMap(row);
    NodeList<Expression> arguments = new NodeList<>();
    rowDataMap.entrySet().forEach(entry -> {
        ArrayInitializerExpr argument = new ArrayInitializerExpr();
        NodeList<Expression> values = NodeList.nodeList(new StringLiteralExpr(entry.getKey()), getExpressionForObject(entry.getValue()));
        argument.setValues(values);
        arguments.add(argument);
    });
    columnValuesVariableArrayInit.setValues(arguments);
    final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, ROW).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, ROW, toReturn)));
    variableDeclarator.setName(variableName);
    final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, ROW, toReturn))).asObjectCreationExpr();
    final NameExpr nameExpr = new NameExpr(columnValuesVariableName);
    objectCreationExpr.getArguments().set(0, nameExpr);
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NodeList(com.github.javaparser.ast.NodeList) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) ArrayInitializerExpr(com.github.javaparser.ast.expr.ArrayInitializerExpr) Expression(com.github.javaparser.ast.expr.Expression) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) CommonCodegenUtils.getExpressionForObject(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getExpressionForObject) ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 62 with KiePMMLException

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

the class KiePMMLTextIndexNormalizationFactory method getTextIndexNormalizationVariableDeclaration.

static BlockStmt getTextIndexNormalizationVariableDeclaration(final String variableName, final TextIndexNormalization textIndexNormalization) {
    if (textIndexNormalization.getInlineTable() == null && textIndexNormalization.getTableLocator() != null) {
        throw new UnsupportedOperationException("TableLocator not supported, yet");
    }
    final MethodDeclaration methodDeclaration = TEXTINDEXNORMALIZATION_TEMPLATE.getMethodsByName(GETKIEPMMLTEXTINDEXNORMALIZATION).get(0).clone();
    final BlockStmt textIndexNormalizationBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(textIndexNormalizationBody, TEXTINDEXNORMALIZATION).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TEXTINDEXNORMALIZATION, textIndexNormalizationBody)));
    variableDeclarator.setName(variableName);
    String inlineTableVariableName = String.format("%s_InlineTable", variableName);
    final BlockStmt toReturn = new BlockStmt();
    BlockStmt toAdd = getInlineTableVariableDeclaration(inlineTableVariableName, textIndexNormalization.getInlineTable());
    toAdd.getStatements().forEach(toReturn::addStatement);
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TEXTINDEXNORMALIZATION, textIndexNormalizationBody))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    final StringLiteralExpr nameExpr = new StringLiteralExpr(variableName);
    builder.setArgument(0, nameExpr);
    getChainedMethodCallExprFrom("withInField", initializer).setArgument(0, getExpressionForObject(textIndexNormalization.getInField()));
    getChainedMethodCallExprFrom("withOutField", initializer).setArgument(0, getExpressionForObject(textIndexNormalization.getOutField()));
    getChainedMethodCallExprFrom("withKiePMMLInlineTable", initializer).setArgument(0, new NameExpr(inlineTableVariableName));
    getChainedMethodCallExprFrom("withRegexField", initializer).setArgument(0, getExpressionForObject(textIndexNormalization.getRegexField()));
    getChainedMethodCallExprFrom("withRecursive", initializer).setArgument(0, getExpressionForObject(textIndexNormalization.isRecursive()));
    BooleanLiteralExpr isCaseSensitiveExpression = textIndexNormalization.isCaseSensitive() != null ? (BooleanLiteralExpr) getExpressionForObject(textIndexNormalization.isCaseSensitive()) : new BooleanLiteralExpr(false);
    getChainedMethodCallExprFrom("withIsCaseSensitive", initializer).setArgument(0, isCaseSensitiveExpression);
    getChainedMethodCallExprFrom("withMaxLevenshteinDistance", initializer).setArgument(0, getExpressionForObject(textIndexNormalization.getMaxLevenshteinDistance()));
    Expression wordSeparatorCharacterREExpression;
    if (textIndexNormalization.getWordSeparatorCharacterRE() != null) {
        String wordSeparatorCharacterRE = StringEscapeUtils.escapeJava(textIndexNormalization.getWordSeparatorCharacterRE());
        wordSeparatorCharacterREExpression = new StringLiteralExpr(wordSeparatorCharacterRE);
    } else {
        wordSeparatorCharacterREExpression = new NullLiteralExpr();
    }
    getChainedMethodCallExprFrom("withWordSeparatorCharacterRE", initializer).setArgument(0, wordSeparatorCharacterREExpression);
    BooleanLiteralExpr tokenizeExpression = textIndexNormalization.isTokenize() != null ? (BooleanLiteralExpr) getExpressionForObject(textIndexNormalization.isTokenize()) : new BooleanLiteralExpr(false);
    getChainedMethodCallExprFrom("withTokenize", initializer).setArgument(0, tokenizeExpression);
    textIndexNormalizationBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 63 with KiePMMLException

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

the class KiePMMLTruePredicateFactory method getTruePredicateVariableDeclaration.

static BlockStmt getTruePredicateVariableDeclaration(final String variableName, final True truePredicate) {
    final MethodDeclaration methodDeclaration = TRUEPREDICATE_TEMPLATE.getMethodsByName(GETKIEPMMLTRUEPREDICATE).get(0).clone();
    final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, TRUEPREDICATE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TRUEPREDICATE, toReturn)));
    variableDeclarator.setName(variableName);
    final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TRUEPREDICATE, toReturn))).asObjectCreationExpr();
    final StringLiteralExpr nameExpr = new StringLiteralExpr(variableName);
    objectCreationExpr.getArguments().set(0, nameExpr);
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) 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 64 with KiePMMLException

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

the class KiePMMLFieldColumnPairFactory method getFieldColumnPairVariableDeclaration.

static BlockStmt getFieldColumnPairVariableDeclaration(final String variableName, final FieldColumnPair fieldColumnPair) {
    final MethodDeclaration methodDeclaration = FIELDCOLUMNPAIR_TEMPLATE.getMethodsByName(GETKIEPMMLFIELDCOLUMNPAIR).get(0).clone();
    final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, FIELDCOLUMNPAIR).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, FIELDCOLUMNPAIR, toReturn)));
    variableDeclarator.setName(variableName);
    final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, FIELDCOLUMNPAIR, toReturn))).asObjectCreationExpr();
    objectCreationExpr.getArguments().set(0, new StringLiteralExpr(fieldColumnPair.getField().getValue()));
    objectCreationExpr.getArguments().set(2, new StringLiteralExpr(fieldColumnPair.getColumn()));
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) 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) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)

Example 65 with KiePMMLException

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

the class KiePMMLInlineTableFactory method getInlineTableVariableDeclaration.

static BlockStmt getInlineTableVariableDeclaration(final String variableName, final InlineTable inlineTable) {
    final MethodDeclaration methodDeclaration = INLINETABLE_TEMPLATE.getMethodsByName(GETKIEPMMLINLINETABLE).get(0).clone();
    final BlockStmt inlineTableBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(inlineTableBody, INLINETABLE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, INLINETABLE, inlineTableBody)));
    variableDeclarator.setName(variableName);
    final BlockStmt toReturn = new BlockStmt();
    int counter = 0;
    final NodeList<Expression> arguments = new NodeList<>();
    for (Row row : inlineTable.getRows()) {
        String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
        arguments.add(new NameExpr(nestedVariableName));
        BlockStmt toAdd = getRowVariableDeclaration(nestedVariableName, row);
        toAdd.getStatements().forEach(toReturn::addStatement);
        counter++;
    }
    final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, INLINETABLE, toReturn))).asObjectCreationExpr();
    objectCreationExpr.getArguments().set(0, new StringLiteralExpr(variableName));
    objectCreationExpr.getArguments().get(2).asMethodCallExpr().setArguments(arguments);
    inlineTableBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NodeList(com.github.javaparser.ast.NodeList) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Expression(com.github.javaparser.ast.expr.Expression) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) Row(org.dmg.pmml.Row)

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