Search in sources :

Example 1 with BUILTIN_FUNCTIONS

use of org.kie.pmml.api.enums.BUILTIN_FUNCTIONS 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)

Aggregations

ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Objects (java.util.Objects)1 StringJoiner (java.util.StringJoiner)1 BUILTIN_FUNCTIONS (org.kie.pmml.api.enums.BUILTIN_FUNCTIONS)1 INVALID_VALUE_TREATMENT_METHOD (org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD)1 MiningField (org.kie.pmml.api.models.MiningField)1 KiePMMLExtension (org.kie.pmml.commons.model.KiePMMLExtension)1 ProcessingDTO (org.kie.pmml.commons.model.ProcessingDTO)1 AbstractKiePMMLComponent (org.kie.pmml.commons.model.abstracts.AbstractKiePMMLComponent)1 KiePMMLDefineFunction (org.kie.pmml.commons.transformations.KiePMMLDefineFunction)1