use of org.kie.pmml.commons.transformations.KiePMMLDefineFunction in project drools by kiegroup.
the class KiePMMLDefineFunctionInstanceFactory method getKiePMMLDefineFunction.
static KiePMMLDefineFunction getKiePMMLDefineFunction(final DefineFunction defineFunction) {
final List<KiePMMLParameterField> kiePMMLParameterFields = getKiePMMLParameterFields(defineFunction.getParameterFields());
DATA_TYPE dataType = defineFunction.getDataType() != null ? DATA_TYPE.byName(defineFunction.getDataType().value()) : null;
OP_TYPE opType = defineFunction.getOpType() != null ? OP_TYPE.byName(defineFunction.getOpType().value()) : null;
return new KiePMMLDefineFunction(defineFunction.getName(), getKiePMMLExtensions(defineFunction.getExtensions()), dataType, opType, kiePMMLParameterFields, getKiePMMLExpression(defineFunction.getExpression()));
}
use of org.kie.pmml.commons.transformations.KiePMMLDefineFunction 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.transformations.KiePMMLDefineFunction in project drools by kiegroup.
the class KiePMMLTransformationDictionaryInstanceFactoryTest method getKiePMMLTransformationDictionary.
@Test
public void getKiePMMLTransformationDictionary() {
final TransformationDictionary toConvert = getRandomTransformationDictionary();
KiePMMLTransformationDictionary retrieved = KiePMMLTransformationDictionaryInstanceFactory.getKiePMMLTransformationDictionary(toConvert, Collections.emptyList());
assertNotNull(retrieved);
List<DerivedField> derivedFields = toConvert.getDerivedFields();
List<KiePMMLDerivedField> derivedFieldsToVerify = retrieved.getDerivedFields();
assertEquals(derivedFields.size(), derivedFieldsToVerify.size());
derivedFields.forEach(derivedFieldSource -> {
Optional<KiePMMLDerivedField> derivedFieldToVerify = derivedFieldsToVerify.stream().filter(param -> param.getName().equals(derivedFieldSource.getName().getValue())).findFirst();
assertTrue(derivedFieldToVerify.isPresent());
commonVerifyKiePMMLDerivedField(derivedFieldToVerify.get(), derivedFieldSource);
});
List<DefineFunction> defineFunctions = toConvert.getDefineFunctions();
List<KiePMMLDefineFunction> defineFunctionsToVerify = retrieved.getDefineFunctions();
assertEquals(defineFunctions.size(), defineFunctionsToVerify.size());
defineFunctions.forEach(defineFunctionSource -> {
Optional<KiePMMLDefineFunction> defineFunctionToVerify = defineFunctionsToVerify.stream().filter(param -> param.getName().equals(defineFunctionSource.getName())).findFirst();
assertTrue(defineFunctionToVerify.isPresent());
commonVerifyKiePMMLDefineFunction(defineFunctionToVerify.get(), defineFunctionSource);
});
}
use of org.kie.pmml.commons.transformations.KiePMMLDefineFunction in project drools by kiegroup.
the class KiePMMLApplyTest method evaluateFromDefineFunction.
@Test
public void evaluateFromDefineFunction() {
Double valueA = 33.0;
Double valueB = 27.0;
// <Apply function="CUSTOM_FUNCTION">
// <Constant>33.0</Constant>
// <Constant>27.0</Constant>
// </Apply>
// <DefineFunction name="CUSTOM_FUNCTION" optype="continuous" dataType="double">
// <ParameterField name="PARAM_1"/>
// <ParameterField field="PARAM_2"/>
// <Apply function="/">
// <Constant>100.0</Constant>
// <Constant>5.0</Constant>
// </Apply>
// </DefineFunction>
final KiePMMLConstant kiePMMLConstant1 = new KiePMMLConstant("NAME-1", Collections.emptyList(), valueA, null);
final KiePMMLConstant kiePMMLConstant2 = new KiePMMLConstant("NAME-1", Collections.emptyList(), valueB, null);
KiePMMLApply kiePMMLApply = KiePMMLApply.builder("NAME", Collections.emptyList(), CUSTOM_FUNCTION).withKiePMMLExpressions(Arrays.asList(kiePMMLConstant1, kiePMMLConstant2)).build();
List<KiePMMLDefineFunction> defineFunctions = Collections.singletonList(getDefineFunctionApplyFromConstant());
ProcessingDTO processingDTO = getProcessingDTO(defineFunctions, Collections.emptyList(), new ArrayList<>(), Collections.emptyList());
Object retrieved = kiePMMLApply.evaluate(processingDTO);
assertEquals(expected, retrieved);
//
// Apply with a Constant and a FieldRef: returns kiePMMLConstant1 divided evaluation of FieldRef from
// kiePMMLNameValues
// <Apply function="CUSTOM_FUNCTION">
// <Constant>33.0</Constant>
// <Constant>27.0</Constant>
// </Apply>
// <DefineFunction name="CUSTOM_FUNCTION" optype="continuous" dataType="double">
// <ParameterField name="PARAM_1"/>
// <ParameterField field="PARAM_2"/>
// <Apply function="/">
// <Constant>100.0</Constant>
// <FieldRef field="PARAM_2"/>
// </Apply>
// </DefineFunction>
defineFunctions = Collections.singletonList(getDefineFunctionApplyFromFieldRef());
processingDTO = getProcessingDTO(defineFunctions, Collections.emptyList(), new ArrayList<>(), Collections.emptyList());
retrieved = kiePMMLApply.evaluate(processingDTO);
Double locallyExpected = value1 / valueB;
assertEquals(locallyExpected, retrieved);
//
// Apply invoking another custom function
// <Apply function="OUTER_FUNCTION">
// <Constant>33.0</Constant>
// <Constant>27.0</Constant>
// </Apply>
// <DefineFunction name="OUTER_FUNCTION" optype="continuous" dataType="double">
// <ParameterField name="PARAM_1"/>
// <ParameterField field="PARAM_2"/>
// <Apply function="CUSTOM_FUNCTION">
// <FieldRef field="PARAM_1"/>
// <FieldRef field="PARAM_2"/>
// </Apply>
// </DefineFunction>
// <DefineFunction name="CUSTOM_FUNCTION" optype="continuous" dataType="double">
// <ParameterField name="PARAM_1"/>
// <ParameterField field="PARAM_2"/>
// <Apply function="/">
// <Constant>100.0</Constant>
// <FieldRef field="PARAM_2"/>
// </Apply>
// </DefineFunction>
kiePMMLApply = KiePMMLApply.builder("NAME", Collections.emptyList(), OUTER_FUNCTION).withKiePMMLExpressions(Arrays.asList(kiePMMLConstant1, kiePMMLConstant2)).build();
defineFunctions = Arrays.asList(getDefineFunctionApplyFromFieldRef(), getDefineFunctionApplyFromCustomFunction());
processingDTO = getProcessingDTO(defineFunctions, Collections.emptyList(), new ArrayList<>(), Collections.emptyList());
retrieved = kiePMMLApply.evaluate(processingDTO);
assertEquals(locallyExpected, retrieved);
}
use of org.kie.pmml.commons.transformations.KiePMMLDefineFunction in project drools by kiegroup.
the class KiePMMLApplyTest method getDefineFunctionApplyFromConstant.
private KiePMMLDefineFunction getDefineFunctionApplyFromConstant() {
// <DefineFunction name="CUSTOM_FUNCTION" optype="continuous" dataType="double">
// <ParameterField name="PARAM_1"/>
// <ParameterField field="PARAM_2"/>
// <Apply function="/">
// <Constant>100.0</Constant>
// <Constant>5.0</Constant>
// </Apply>
// </DefineFunction>
final KiePMMLConstant kiePMMLConstant1 = new KiePMMLConstant(PARAM_1, Collections.emptyList(), value1, null);
final KiePMMLConstant kiePMMLConstant2 = new KiePMMLConstant(PARAM_2, Collections.emptyList(), value2, null);
KiePMMLApply kiePMMLApply = KiePMMLApply.builder("NAME", Collections.emptyList(), "/").withKiePMMLExpressions(Arrays.asList(kiePMMLConstant1, kiePMMLConstant2)).build();
final KiePMMLParameterField parameterField1 = KiePMMLParameterField.builder(PARAM_1, Collections.emptyList()).build();
final KiePMMLParameterField parameterField2 = KiePMMLParameterField.builder(PARAM_2, Collections.emptyList()).build();
return new KiePMMLDefineFunction(CUSTOM_FUNCTION, Collections.emptyList(), null, OP_TYPE.CONTINUOUS, Arrays.asList(parameterField1, parameterField2), kiePMMLApply);
}
Aggregations