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);
}
}
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);
}
}
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;
}
Aggregations