use of org.kie.pmml.commons.model.tuples.KiePMMLNameValue in project drools by kiegroup.
the class CommonCodegenUtils method getFilteredKiePMMLNameValueExpression.
/**
* Returns
* <pre>
* Optional<KiePMMLNameValue> kiePMMLNameValue = (<i>kiePMMLNameValueListParam</i>)
* .stream()
* .filter((KiePMMLNameValue kpmmlnv) -> Objects.equals("(<i>fieldNameToRef</i>)", kpmmlnv.getName()))
* .findFirst();
* </pre>
* <p>
* expression, where <b>kiePMMLNameValueListParam</b> is the name of the
* <code>List<KiePMMLNameValue></code> parameter, and
* <b>fieldNameToRef</b> is the name of the field to find, in the containing method
* @param kiePMMLNameValueListParam
* @param fieldNameToRef
* @param stringLiteralComparison if <code>true</code>, equals comparison is made on the String, e.g Objects
* .equals("(<i>fieldNameToRef</i>)", kpmmlnv.getName())),
* otherwise, is done on object reference, e.g Objects.equals((<i>fieldNameToRef</i>), kpmmlnv.getName())). In
* this latter case, a <i>fieldNameToRef</i> variable is
* expected to exists
* @return
*/
public static ExpressionStmt getFilteredKiePMMLNameValueExpression(final String kiePMMLNameValueListParam, final String fieldNameToRef, boolean stringLiteralComparison) {
// kpmmlnv.getName()
MethodCallExpr argumentBodyExpressionArgument2 = new MethodCallExpr("getName");
argumentBodyExpressionArgument2.setScope(new NameExpr(LAMBDA_PARAMETER_NAME));
// Objects.equals(fieldNameToRef, kpmmlnv.getName())
MethodCallExpr argumentBodyExpression = new MethodCallExpr("equals");
Expression equalsComparisonExpression;
if (stringLiteralComparison) {
equalsComparisonExpression = new StringLiteralExpr(fieldNameToRef);
} else {
equalsComparisonExpression = new NameExpr(fieldNameToRef);
}
argumentBodyExpression.setArguments(NodeList.nodeList(equalsComparisonExpression, argumentBodyExpressionArgument2));
argumentBodyExpression.setScope(new NameExpr(Objects.class.getName()));
ExpressionStmt argumentBody = new ExpressionStmt(argumentBodyExpression);
// (KiePMMLNameValue kpmmlnv) -> Objects.equals(fieldNameToRef, kpmmlnv.getName())
Parameter argumentParameter = new Parameter(parseClassOrInterfaceType(KiePMMLNameValue.class.getName()), LAMBDA_PARAMETER_NAME);
LambdaExpr argument = new LambdaExpr();
//
argument.setEnclosingParameters(true).setParameters(NodeList.nodeList(argumentParameter));
// (KiePMMLNameValue kpmmlnv) ->
// Objects.equals(fieldNameToRef, kpmmlnv.getName())
argument.setBody(argumentBody);
// kiePMMLNameValueListParam.stream()
MethodCallExpr initializerScopeScope = new MethodCallExpr("stream");
initializerScopeScope.setScope(new NameExpr(kiePMMLNameValueListParam));
// kiePMMLNameValueListParam.stream().filter((KiePMMLNameValue kpmmlnv) -> Objects.equals(fieldNameToRef,
// kpmmlnv.getName()))
MethodCallExpr initializerScope = new MethodCallExpr("filter");
initializerScope.setScope(initializerScopeScope);
initializerScope.setArguments(NodeList.nodeList(argument));
// kiePMMLNameValueListParam.stream().filter((KiePMMLNameValue kpmmlnv) -> Objects.equals(fieldNameToRef,
// kpmmlnv.getName())).findFirst()
MethodCallExpr initializer = new MethodCallExpr("findFirst");
initializer.setScope(initializerScope);
// Optional<KiePMMLNameValue> kiePMMLNameValue
VariableDeclarator variableDeclarator = new VariableDeclarator(getTypedClassOrInterfaceTypeByTypeNames(Optional.class.getName(), Collections.singletonList(KiePMMLNameValue.class.getName())), OPTIONAL_FILTERED_KIEPMMLNAMEVALUE_NAME);
// Optional<KiePMMLNameValue> kiePMMLNameValue = kiePMMLNameValueListParam.stream().filter((KiePMMLNameValue
// kpmmlnv) -> Objects.equals(fieldNameToRef, kpmmlnv.getName())).findFirst()
variableDeclarator.setInitializer(initializer);
//
VariableDeclarationExpr variableDeclarationExpr = new VariableDeclarationExpr(NodeList.nodeList(variableDeclarator));
ExpressionStmt toReturn = new ExpressionStmt();
toReturn.setExpression(variableDeclarationExpr);
return toReturn;
}
use of org.kie.pmml.commons.model.tuples.KiePMMLNameValue in project drools by kiegroup.
the class PreProcessTest method executeTransformations.
@Test
public void executeTransformations() {
// <DefineFunction name="CUSTOM_FUNCTION" optype="continuous" dataType="double">
// <ParameterField name="PARAM_1" />
// <ParameterField name="PARAM_2" />
// <Apply function="/">
// <FieldRef>PARAM_1</FieldRef>
// <FieldRef>PARAM_2</FieldRef>
// </Apply>
// </DefineFunction>
final KiePMMLParameterField kiePMMLParameterField1 = KiePMMLParameterField.builder(PARAM_1, Collections.emptyList()).build();
final KiePMMLParameterField kiePMMLParameterField2 = KiePMMLParameterField.builder(PARAM_2, Collections.emptyList()).build();
final KiePMMLFieldRef kiePMMLFieldRef1 = new KiePMMLFieldRef(PARAM_1, Collections.emptyList(), null);
final KiePMMLFieldRef kiePMMLFieldRef2 = new KiePMMLFieldRef(PARAM_2, Collections.emptyList(), null);
final KiePMMLApply kiePMMLApplyRef = KiePMMLApply.builder("NAMEREF", Collections.emptyList(), "/").withKiePMMLExpressions(Arrays.asList(kiePMMLFieldRef1, kiePMMLFieldRef2)).build();
final KiePMMLDefineFunction defineFunction = new KiePMMLDefineFunction(CUSTOM_FUNCTION, Collections.emptyList(), DATA_TYPE.DOUBLE, OP_TYPE.CONTINUOUS, Arrays.asList(kiePMMLParameterField1, kiePMMLParameterField2), kiePMMLApplyRef);
// <DerivedField name="CUSTOM_REF_FIELD" optype="continuous" dataType="double">
// <Apply function="CUSTOM_FUNCTION">
// <FieldRef>INPUT_FIELD</FieldRef>
// <Constant>5.0</Constant>
// </Apply>
// </DerivedField>
final KiePMMLFieldRef kiePMMLFieldRef3 = new KiePMMLFieldRef(INPUT_FIELD, Collections.emptyList(), null);
final KiePMMLConstant kiePMMLConstant1 = new KiePMMLConstant(PARAM_2, Collections.emptyList(), value2, null);
final KiePMMLApply kiePMMLApply = KiePMMLApply.builder("NAME", Collections.emptyList(), CUSTOM_FUNCTION).withKiePMMLExpressions(Arrays.asList(kiePMMLFieldRef3, kiePMMLConstant1)).build();
final KiePMMLDerivedField derivedField = KiePMMLDerivedField.builder(CUSTOM_REF_FIELD, Collections.emptyList(), DATA_TYPE.DOUBLE, OP_TYPE.CONTINUOUS, kiePMMLApply).build();
// From TransformationDictionary
KiePMMLTransformationDictionary transformationDictionary = KiePMMLTransformationDictionary.builder("transformationDictionary", Collections.emptyList()).withDefineFunctions(Collections.singletonList(defineFunction)).withDerivedFields(Collections.singletonList(derivedField)).build();
KiePMMLTestingModel kiePMMLModel = KiePMMLTestingModel.builder("TESTINGMODEL", Collections.emptyList(), MINING_FUNCTION.REGRESSION).withKiePMMLTransformationDictionary(transformationDictionary).build();
//
final PMMLRequestData pmmlRequestData = new PMMLRequestData();
pmmlRequestData.addRequestParam(INPUT_FIELD, value1);
Map<String, ParameterInfo> mappedRequestParams = pmmlRequestData.getMappedRequestParams();
final List<KiePMMLNameValue> kiePMMLNameValues = PreProcess.getKiePMMLNameValuesFromParameterInfos(mappedRequestParams.values());
Optional<KiePMMLNameValue> retrieved = kiePMMLNameValues.stream().filter(kiePMMLNameValue -> kiePMMLNameValue.getName().equals(INPUT_FIELD)).findFirst();
assertTrue(retrieved.isPresent());
assertEquals(value1, retrieved.get().getValue());
ProcessingDTO processingDTO = new ProcessingDTO(kiePMMLModel, kiePMMLNameValues);
PreProcess.executeTransformations(processingDTO, pmmlRequestData);
mappedRequestParams = pmmlRequestData.getMappedRequestParams();
Object expected = value1 / value2;
assertTrue(mappedRequestParams.containsKey(CUSTOM_REF_FIELD));
assertEquals(expected, mappedRequestParams.get(CUSTOM_REF_FIELD).getValue());
retrieved = kiePMMLNameValues.stream().filter(kiePMMLNameValue -> kiePMMLNameValue.getName().equals(CUSTOM_REF_FIELD)).findFirst();
assertTrue(retrieved.isPresent());
assertEquals(expected, retrieved.get().getValue());
}
use of org.kie.pmml.commons.model.tuples.KiePMMLNameValue in project drools by kiegroup.
the class PreProcess method executeTransformations.
/**
* Execute <b>transformations</b> on input data.
* @param processingDTO
* @param requestData
* @see <a href="http://dmg.org/pmml/v4-4/Transformations.html">Transformations</a>
* @see
* <a href="http://dmg.org/pmml/v4-4/Transformations.html#xsdElement_LocalTransformations">LocalTransformations</a>
*/
static void executeTransformations(final ProcessingDTO processingDTO, final PMMLRequestData requestData) {
logger.debug("executeTransformations {} {}", processingDTO, requestData);
for (KiePMMLDerivedField derivedField : processingDTO.getDerivedFields()) {
Object derivedValue = derivedField.evaluate(processingDTO);
if (derivedValue != null) {
requestData.addRequestParam(derivedField.getName(), derivedValue);
processingDTO.addKiePMMLNameValue(new KiePMMLNameValue(derivedField.getName(), derivedValue));
}
}
}
use of org.kie.pmml.commons.model.tuples.KiePMMLNameValue in project drools by kiegroup.
the class PMMLMiningModelEvaluator method getPMML4Result.
PMML4Result getPMML4Result(final KiePMMLMiningModel toEvaluate, final LinkedHashMap<String, KiePMMLNameValueProbabilityMapTuple> inputData, final PMMLContext pmmlContext) {
final MULTIPLE_MODEL_METHOD multipleModelMethod = toEvaluate.getSegmentation().getMultipleModelMethod();
Object result = null;
LinkedHashMap<String, Double> probabilityResultMap = null;
ResultCode resultCode = OK;
final LinkedHashMap<String, KiePMMLNameValue> toUseForPrediction = new LinkedHashMap<>();
final LinkedHashMap<String, List<KiePMMLNameValue>> toUseForProbability = new LinkedHashMap<>();
inputData.forEach((key, value) -> {
toUseForPrediction.put(key, value.predictionValue);
toUseForProbability.put(key, value.probabilityValues);
});
try {
if (MINING_FUNCTION.CLASSIFICATION.equals(toEvaluate.getMiningFunction())) {
result = multipleModelMethod.applyClassification(toUseForPrediction);
probabilityResultMap = multipleModelMethod.applyProbability(toUseForProbability);
} else {
result = multipleModelMethod.applyPrediction(toUseForPrediction);
}
} catch (KieEnumException e) {
logger.warn(e.getMessage());
resultCode = FAIL;
}
pmmlContext.setProbabilityResultMap(probabilityResultMap);
PMML4Result toReturn = new PMML4Result();
toReturn.addResultVariable(toEvaluate.getTargetField(), result);
toReturn.setResultObjectName(toEvaluate.getTargetField());
toReturn.setResultCode(resultCode.getName());
return toReturn;
}
use of org.kie.pmml.commons.model.tuples.KiePMMLNameValue in project drools by kiegroup.
the class PMMLMiningModelEvaluatorTest method getKiePMMLNameRawObject.
@Test
public void getKiePMMLNameRawObject() {
final Object rawObject = "OBJ";
final PMML4Result pmml4Result = getPMML4Result(rawObject);
RAW_OBJECT_METHODS.forEach(multipleModelMethod -> {
KiePMMLNameValue retrieved = evaluator.getKiePMMLNameValue(pmml4Result, multipleModelMethod, 34.2);
assertEquals(pmml4Result.getResultObjectName(), retrieved.getName());
assertNotNull(retrieved.getValue());
assertEquals(rawObject, retrieved.getValue());
});
}
Aggregations