Search in sources :

Example 1 with RESULT_FEATURE

use of org.kie.pmml.api.enums.RESULT_FEATURE in project drools by kiegroup.

the class KiePMMLOutputFieldInstanceFactory method getKiePMMLOutputField.

public static KiePMMLOutputField getKiePMMLOutputField(final OutputField outputField) {
    String name = outputField.getName() != null ? outputField.getName().getValue() : "" + outputField.hashCode();
    final String targetField = outputField.getTargetField() != null ? outputField.getTargetField().getValue() : null;
    final RESULT_FEATURE resultFeature = outputField.getResultFeature() != null ? RESULT_FEATURE.byName(outputField.getResultFeature().value()) : null;
    final DATA_TYPE dataType = outputField.getDataType() != null ? DATA_TYPE.byName(outputField.getDataType().value()) : null;
    final KiePMMLExpression kiePMMLExpression = outputField.getExpression() != null ? getKiePMMLExpression(outputField.getExpression()) : null;
    final KiePMMLOutputField.Builder builder = KiePMMLOutputField.builder(name, Collections.emptyList()).withResultFeature(resultFeature).withTargetField(targetField).withValue(outputField.getValue()).withDataType(dataType).withRank(outputField.getRank()).withKiePMMLExpression(kiePMMLExpression);
    return builder.build();
}
Also used : KiePMMLOutputField(org.kie.pmml.commons.model.KiePMMLOutputField) RESULT_FEATURE(org.kie.pmml.api.enums.RESULT_FEATURE) KiePMMLExpression(org.kie.pmml.commons.model.expressions.KiePMMLExpression) KiePMMLExpressionInstanceFactory.getKiePMMLExpression(org.kie.pmml.compiler.commons.factories.KiePMMLExpressionInstanceFactory.getKiePMMLExpression) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE)

Example 2 with RESULT_FEATURE

use of org.kie.pmml.api.enums.RESULT_FEATURE in project drools by kiegroup.

the class ModelUtilsTest method convertToKieOutputField.

@Test
public void convertToKieOutputField() {
    final OutputField toConvert = getRandomOutputField();
    org.kie.pmml.api.models.OutputField retrieved = ModelUtils.convertToKieOutputField(toConvert, null);
    assertNotNull(retrieved);
    assertEquals(toConvert.getName().getValue(), retrieved.getName());
    OP_TYPE expectedOpType = OP_TYPE.byName(toConvert.getOpType().value());
    assertEquals(expectedOpType, retrieved.getOpType());
    DATA_TYPE expectedDataType = DATA_TYPE.byName(toConvert.getDataType().value());
    assertEquals(expectedDataType, retrieved.getDataType());
    assertEquals(toConvert.getTargetField().getValue(), retrieved.getTargetField());
    RESULT_FEATURE expectedResultFeature = RESULT_FEATURE.byName(toConvert.getResultFeature().value());
    assertEquals(expectedResultFeature, retrieved.getResultFeature());
    toConvert.setOpType(null);
    toConvert.setTargetField(null);
    retrieved = ModelUtils.convertToKieOutputField(toConvert, null);
    assertNull(retrieved.getOpType());
    assertNull(retrieved.getTargetField());
}
Also used : RESULT_FEATURE(org.kie.pmml.api.enums.RESULT_FEATURE) OutputField(org.dmg.pmml.OutputField) PMMLModelTestUtils.getRandomOutputField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomOutputField) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) Test(org.junit.Test)

Example 3 with RESULT_FEATURE

use of org.kie.pmml.api.enums.RESULT_FEATURE in project drools by kiegroup.

the class KiePMMLOutputFieldFactory method getOutputFieldVariableDeclaration.

static BlockStmt getOutputFieldVariableDeclaration(final String variableName, final OutputField outputField) {
    final MethodDeclaration methodDeclaration = OUTPUTFIELD_TEMPLATE.getMethodsByName(GETKIEPMMLOUTPUTFIELD).get(0).clone();
    final BlockStmt outputFieldBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(outputFieldBody, OUTPUTFIELD).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, OUTPUTFIELD, outputFieldBody)));
    variableDeclarator.setName(variableName);
    final BlockStmt toReturn = new BlockStmt();
    final Expression expressionExpr;
    if (outputField.getExpression() != null) {
        String nestedVariableName = String.format("%s_Expression", variableName);
        BlockStmt toAdd = getKiePMMLExpressionBlockStmt(nestedVariableName, outputField.getExpression());
        toAdd.getStatements().forEach(toReturn::addStatement);
        expressionExpr = new NameExpr(nestedVariableName);
    } else {
        expressionExpr = new NullLiteralExpr();
    }
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, OUTPUTFIELD, toReturn))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    final StringLiteralExpr nameExpr = new StringLiteralExpr(outputField.getName().getValue());
    final RESULT_FEATURE resultFeature = RESULT_FEATURE.byName(outputField.getResultFeature().value());
    final NameExpr resultFeatureExpr = new NameExpr(RESULT_FEATURE.class.getName() + "." + resultFeature.name());
    final Expression targetFieldExpr = outputField.getTargetField() != null ? getExpressionForObject(outputField.getTargetField().getValue()) : new NullLiteralExpr();
    final Expression valueExpr = outputField.getValue() != null ? getExpressionForObject(outputField.getValue()) : new NullLiteralExpr();
    final Expression dataTypeExpression = getExpressionForDataType(outputField.getDataType());
    final Expression rankExpr = outputField.getRank() != null ? getExpressionForObject(outputField.getRank()) : new NullLiteralExpr();
    builder.setArgument(0, nameExpr);
    getChainedMethodCallExprFrom("withResultFeature", initializer).setArgument(0, resultFeatureExpr);
    getChainedMethodCallExprFrom("withTargetField", initializer).setArgument(0, targetFieldExpr);
    getChainedMethodCallExprFrom("withValue", initializer).setArgument(0, valueExpr);
    getChainedMethodCallExprFrom("withDataType", initializer).setArgument(0, dataTypeExpression);
    getChainedMethodCallExprFrom("withRank", initializer).setArgument(0, rankExpr);
    getChainedMethodCallExprFrom("withKiePMMLExpression", initializer).setArgument(0, expressionExpr);
    outputFieldBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) RESULT_FEATURE(org.kie.pmml.api.enums.RESULT_FEATURE) KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) 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) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 4 with RESULT_FEATURE

use of org.kie.pmml.api.enums.RESULT_FEATURE in project drools by kiegroup.

the class ModelUtils method convertToKieOutputField.

/**
 * Return a <code>org.kie.pmml.api.models.OutputField</code> out of a <code>org.dmg.pmml.OutputField</code> one
 * @param toConvert
 * @param field - this may be <code>null</code>
 * @return
 */
public static org.kie.pmml.api.models.OutputField convertToKieOutputField(final OutputField toConvert, final Field<?> field) {
    final String name = toConvert.getName() != null ? toConvert.getName().getValue() : null;
    final OP_TYPE opType = toConvert.getOpType() != null ? OP_TYPE.byName(toConvert.getOpType().value()) : null;
    final DATA_TYPE dataFieldDataType = field != null ? DATA_TYPE.byName(field.getDataType().value()) : null;
    final DATA_TYPE dataType = toConvert.getDataType() != null ? DATA_TYPE.byName(toConvert.getDataType().value()) : dataFieldDataType;
    final String targetField = toConvert.getTargetField() != null ? toConvert.getTargetField().getValue() : null;
    final RESULT_FEATURE resultFeature = toConvert.getResultFeature() != null ? RESULT_FEATURE.byName(toConvert.getResultFeature().value()) : null;
    final List<String> allowedValues = field instanceof DataField ? convertDataFieldValues(((DataField) field).getValues()) : null;
    return new org.kie.pmml.api.models.OutputField(name, opType, dataType, targetField, resultFeature, allowedValues);
}
Also used : DataField(org.dmg.pmml.DataField) RESULT_FEATURE(org.kie.pmml.api.enums.RESULT_FEATURE) OutputField(org.dmg.pmml.OutputField) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE)

Aggregations

RESULT_FEATURE (org.kie.pmml.api.enums.RESULT_FEATURE)4 DATA_TYPE (org.kie.pmml.api.enums.DATA_TYPE)3 OutputField (org.dmg.pmml.OutputField)2 OP_TYPE (org.kie.pmml.api.enums.OP_TYPE)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 Expression (com.github.javaparser.ast.expr.Expression)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 DataField (org.dmg.pmml.DataField)1 Test (org.junit.Test)1 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)1 KiePMMLOutputField (org.kie.pmml.commons.model.KiePMMLOutputField)1 KiePMMLExpression (org.kie.pmml.commons.model.expressions.KiePMMLExpression)1 PMMLModelTestUtils.getRandomOutputField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomOutputField)1 KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt (org.kie.pmml.compiler.commons.codegenfactories.KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt)1 KiePMMLExpressionInstanceFactory.getKiePMMLExpression (org.kie.pmml.compiler.commons.factories.KiePMMLExpressionInstanceFactory.getKiePMMLExpression)1