Search in sources :

Example 16 with MiningField

use of org.kie.pmml.api.models.MiningField in project drools by kiegroup.

the class KiePMMLApply method evaluate.

@Override
public Object evaluate(final ProcessingDTO processingDTO) {
    if (kiePMMLExpressions == null) {
        return null;
    }
    // <- Insertion order matter
    List<Object> expressionValues = new ArrayList<>();
    MiningField referredByFieldRef = null;
    for (KiePMMLExpression kiePMMLExpression : kiePMMLExpressions) {
        expressionValues.add(kiePMMLExpression.evaluate(processingDTO));
        if (kiePMMLExpression instanceof KiePMMLFieldRef && BUILTIN_FUNCTIONS.isBUILTIN_FUNCTIONS_VALIDATION(function)) {
            String referredField = ((KiePMMLFieldRef) kiePMMLExpression).getName();
            referredByFieldRef = processingDTO.getMiningFields().stream().filter(miningField -> referredField.equals(miningField.getName())).findFirst().orElseThrow(() -> new IllegalArgumentException(String.format("Missing required field %s", referredField)));
        }
    }
    if (BUILTIN_FUNCTIONS.isBUILTIN_FUNCTIONS(function)) {
        BUILTIN_FUNCTIONS builtinFunction = BUILTIN_FUNCTIONS.byName(function);
        return builtinFunction.getValue(expressionValues.toArray(new Object[0]), referredByFieldRef);
    } else {
        final KiePMMLDefineFunction definedFunction = processingDTO.getDefineFunctions().stream().filter(defineFunction -> defineFunction.getName().equals(function)).findFirst().orElseThrow(() -> new IllegalArgumentException("Unknown function " + function));
        return definedFunction.evaluate(processingDTO, expressionValues);
    }
}
Also used : Objects(java.util.Objects) List(java.util.List) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) BUILTIN_FUNCTIONS(org.kie.pmml.api.enums.BUILTIN_FUNCTIONS) StringJoiner(java.util.StringJoiner) KiePMMLExtension(org.kie.pmml.commons.model.KiePMMLExtension) ProcessingDTO(org.kie.pmml.commons.model.ProcessingDTO) AbstractKiePMMLComponent(org.kie.pmml.commons.model.abstracts.AbstractKiePMMLComponent) Collections(java.util.Collections) ArrayList(java.util.ArrayList) INVALID_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD) MiningField(org.kie.pmml.api.models.MiningField) BUILTIN_FUNCTIONS(org.kie.pmml.api.enums.BUILTIN_FUNCTIONS) MiningField(org.kie.pmml.api.models.MiningField) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) ArrayList(java.util.ArrayList)

Example 17 with MiningField

use of org.kie.pmml.api.models.MiningField in project drools by kiegroup.

the class PostProcess method updateTargetValueType.

/**
 * Verify that the returned value has the required type as defined inside <code>DataDictionary/MiningSchema</code>
 * @param model
 * @param toUpdate
 */
static void updateTargetValueType(final KiePMMLModel model, final PMML4Result toUpdate) {
    DATA_TYPE dataType = model.getMiningFields().stream().filter(miningField -> model.getTargetField().equals(miningField.getName())).map(MiningField::getDataType).findFirst().orElseThrow(() -> new KiePMMLException("Failed to find DATA_TYPE for " + model.getTargetField()));
    Object prediction = toUpdate.getResultVariables().get(model.getTargetField());
    if (prediction != null) {
        Object convertedPrediction = dataType.getActualValue(prediction);
        toUpdate.getResultVariables().put(model.getTargetField(), convertedPrediction);
    }
}
Also used : OK(org.kie.pmml.api.enums.ResultCode.OK) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) PMML4Result(org.kie.api.pmml.PMML4Result) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) KiePMMLOutputField(org.kie.pmml.commons.model.KiePMMLOutputField) ArrayList(java.util.ArrayList) MiningField(org.kie.pmml.api.models.MiningField) LinkedHashMap(java.util.LinkedHashMap) PMMLContext(org.kie.pmml.api.runtime.PMMLContext) AbstractMap(java.util.AbstractMap) List(java.util.List) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) Map(java.util.Map) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) ProcessingDTO(org.kie.pmml.commons.model.ProcessingDTO) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) MiningField(org.kie.pmml.api.models.MiningField) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE)

Example 18 with MiningField

use of org.kie.pmml.api.models.MiningField in project drools by kiegroup.

the class PMMLRuntimeInternalImplTest method getKiePMMLModelMock.

private KiePMMLModel getKiePMMLModelMock() {
    KiePMMLModel toReturn = mock(KiePMMLModel.class);
    String targetFieldName = "targetFieldName";
    MiningField miningFieldMock = mock(MiningField.class);
    when(miningFieldMock.getName()).thenReturn(targetFieldName);
    when(miningFieldMock.getDataType()).thenReturn(DATA_TYPE.FLOAT);
    when(toReturn.getName()).thenReturn(MODEL_NAME);
    when(toReturn.getMiningFields()).thenReturn(Collections.singletonList(miningFieldMock));
    when(toReturn.getTargetField()).thenReturn(targetFieldName);
    when(toReturn.getPmmlMODEL()).thenReturn(PMML_MODEL.TEST_MODEL);
    return toReturn;
}
Also used : MiningField(org.kie.pmml.api.models.MiningField) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel)

Aggregations

MiningField (org.kie.pmml.api.models.MiningField)18 Test (org.junit.Test)14 Interval (org.kie.pmml.api.models.Interval)11 INVALID_VALUE_TREATMENT_METHOD (org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD)8 List (java.util.List)7 Collections (java.util.Collections)6 Collectors (java.util.stream.Collectors)6 DATA_TYPE (org.kie.pmml.api.enums.DATA_TYPE)6 Arrays (java.util.Arrays)5 Optional (java.util.Optional)5 IntStream (java.util.stream.IntStream)5 Assert.assertEquals (org.junit.Assert.assertEquals)5 Assert.assertTrue (org.junit.Assert.assertTrue)5 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)4 Expression (com.github.javaparser.ast.expr.Expression)4 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)4 NameExpr (com.github.javaparser.ast.expr.NameExpr)4 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)4 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)4 ArrayList (java.util.ArrayList)4