Search in sources :

Example 1 with OPERATOR

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

the class KiePMMLScorecardModelCharacteristicASTFactoryTest method commonValidateKiePMMLOperatorValue.

private void commonValidateKiePMMLOperatorValue(KiePMMLOperatorValue toValidate, SimplePredicate simplePredicate) {
    OPERATOR expectedOperator = OPERATOR.byName(simplePredicate.getOperator().value());
    assertEquals(expectedOperator, toValidate.getOperator());
    Object expectedValue = getExpectedValue(simplePredicate);
    assertEquals(expectedValue, toValidate.getValue());
}
Also used : BOOLEAN_OPERATOR(org.kie.pmml.api.enums.BOOLEAN_OPERATOR) OPERATOR(org.kie.pmml.api.enums.OPERATOR)

Example 2 with OPERATOR

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

the class KiePMMLOperatorValueTest method getConstraintsAsString.

@Test
public void getConstraintsAsString() {
    OPERATOR operator = OPERATOR.LESS_THAN;
    Object value = 234;
    KiePMMLOperatorValue kiePMMLOperatorValue = new KiePMMLOperatorValue(operator, value);
    String retrieved = kiePMMLOperatorValue.getConstraintsAsString();
    String expected = String.format(VALUE_CONSTRAINT_PATTERN, operator.getOperator(), value);
    assertEquals(expected, retrieved);
}
Also used : OPERATOR(org.kie.pmml.api.enums.OPERATOR) Test(org.junit.Test)

Example 3 with OPERATOR

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

the class KiePMMLSimplePredicateFactory method getSimplePredicateVariableDeclaration.

static BlockStmt getSimplePredicateVariableDeclaration(final String variableName, final SimplePredicate simplePredicate, final List<Field<?>> fields) {
    final MethodDeclaration methodDeclaration = SIMPLE_PREDICATE_TEMPLATE.getMethodsByName(GETKIEPMMLSIMPLEPREDICATE).get(0).clone();
    final BlockStmt simplePredicateBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(simplePredicateBody, SIMPLE_PREDICATE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, SIMPLE_PREDICATE, simplePredicateBody)));
    variableDeclarator.setName(variableName);
    final BlockStmt toReturn = new BlockStmt();
    final OPERATOR operator = OPERATOR.byName(simplePredicate.getOperator().value());
    final NameExpr operatorExpr = new NameExpr(OPERATOR.class.getName() + "." + operator.name());
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, SIMPLE_PREDICATE, simplePredicateBody))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    builder.setArgument(0, new StringLiteralExpr(simplePredicate.getField().getValue()));
    builder.setArgument(2, operatorExpr);
    DataType dataType = getDataType(fields, simplePredicate.getField().getValue());
    Object actualValue = DATA_TYPE.byName(dataType.value()).getActualValue(simplePredicate.getValue());
    getChainedMethodCallExprFrom("withValue", initializer).setArgument(0, getExpressionForObject(actualValue));
    simplePredicateBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : OPERATOR(org.kie.pmml.api.enums.OPERATOR) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) 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) DataType(org.dmg.pmml.DataType) ModelUtils.getDataType(org.kie.pmml.compiler.api.utils.ModelUtils.getDataType) CommonCodegenUtils.getExpressionForObject(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getExpressionForObject) 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 OPERATOR

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

the class KiePMMLSimplePredicateASTFactory method getBuilderForSimplePredicate.

/**
 * This method will create a <b>rule</b> that, in the RHS,
 * 1) update the status (used for flowing between rules)
 * 2) add <i>outputfields</i> to result variables
 * 3) eventually set the value to accumulate
 * <p>
 * rule "_ResidenceStateScore_1"
 * when
 * $statusHolder : KiePMMLStatusHolder( status == "_ResidenceStateScore" )
 * <p>
 * RESIDENCESTATE( value == "KN" )
 * then
 * <p>
 * $statusHolder.setStatus("_ResidenceStateScore_1");
 * $statusHolder.accumulate("10.0");
 * update($statusHolder);
 * <p>
 * end
 * <p>
 * end
 * @param statusToSet
 * @return
 */
protected KiePMMLDroolsRule.Builder getBuilderForSimplePredicate(final String statusToSet) {
    logger.trace("getBuilderForSimplePredicate {}", statusToSet);
    String statusConstraint = StringUtils.isEmpty(predicateASTFactoryData.getParentPath()) ? KiePMMLAbstractModelASTFactory.STATUS_NULL : String.format(STATUS_PATTERN, predicateASTFactoryData.getParentPath());
    String key = predicateASTFactoryData.getFieldTypeMap().get(((SimplePredicate) predicateASTFactoryData.getPredicate()).getField().getValue()).getGeneratedType();
    OPERATOR operator = OPERATOR.byName(((SimplePredicate) predicateASTFactoryData.getPredicate()).getOperator().value());
    Object value = KiePMMLASTFactoryUtils.getCorrectlyFormattedObject(((SimplePredicate) predicateASTFactoryData.getPredicate()), predicateASTFactoryData.getFieldTypeMap());
    List<KiePMMLFieldOperatorValue> andConstraints = Collections.singletonList(new KiePMMLFieldOperatorValue(key, BOOLEAN_OPERATOR.AND, Collections.singletonList(new KiePMMLOperatorValue(operator, value)), null));
    return KiePMMLDroolsRule.builder(predicateASTFactoryData.getCurrentRule(), statusToSet, predicateASTFactoryData.getOutputFields()).withStatusConstraint(statusConstraint).withAndConstraints(andConstraints);
}
Also used : KiePMMLOperatorValue(org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue) BOOLEAN_OPERATOR(org.kie.pmml.api.enums.BOOLEAN_OPERATOR) OPERATOR(org.kie.pmml.api.enums.OPERATOR) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) SimplePredicate(org.dmg.pmml.SimplePredicate)

Example 5 with OPERATOR

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

the class KiePMMLOperatorValueTest method buildConstraintsString.

@Test
public void buildConstraintsString() {
    OPERATOR operator = OPERATOR.LESS_THAN;
    Object value = 234;
    KiePMMLOperatorValue kiePMMLOperatorValue = new KiePMMLOperatorValue(operator, value);
    String retrieved = kiePMMLOperatorValue.buildConstraintsString();
    String expected = String.format(VALUE_CONSTRAINT_PATTERN, operator.getOperator(), value);
    assertEquals(expected, retrieved);
}
Also used : OPERATOR(org.kie.pmml.api.enums.OPERATOR) Test(org.junit.Test)

Aggregations

OPERATOR (org.kie.pmml.api.enums.OPERATOR)5 Test (org.junit.Test)2 BOOLEAN_OPERATOR (org.kie.pmml.api.enums.BOOLEAN_OPERATOR)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 DataType (org.dmg.pmml.DataType)1 SimplePredicate (org.dmg.pmml.SimplePredicate)1 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)1 ModelUtils.getDataType (org.kie.pmml.compiler.api.utils.ModelUtils.getDataType)1 CommonCodegenUtils.getExpressionForObject (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getExpressionForObject)1 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)1 KiePMMLFieldOperatorValue (org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue)1 KiePMMLOperatorValue (org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue)1