Search in sources :

Example 11 with KiePMMLDerivedField

use of org.kie.pmml.commons.transformations.KiePMMLDerivedField in project drools by kiegroup.

the class PostProcessTest method populateTransformedOutputFieldWithApplyDefineFunctionFromFieldRef.

@Test
public void populateTransformedOutputFieldWithApplyDefineFunctionFromFieldRef() {
    // <DerivedField name="CUSTOM_REF_FIELD" optype="continuous" dataType="double">
    // <Constant>100.0</Constant>
    // </DerivedField>
    final KiePMMLConstant kiePMMLConstant1 = new KiePMMLConstant(PARAM_1, Collections.emptyList(), value1, null);
    final KiePMMLDerivedField derivedField = KiePMMLDerivedField.builder(CUSTOM_REF_FIELD, Collections.emptyList(), DATA_TYPE.DOUBLE, OP_TYPE.CONTINUOUS, kiePMMLConstant1).build();
    // <DefineFunction name="CUSTOM_FUNCTION" optype="continuous" dataType="double">
    // <FieldRef>CUSTOM_REF_FIELD</FieldRef>
    // </DefineFunction>
    final KiePMMLFieldRef kiePMMLFieldRef1 = new KiePMMLFieldRef(CUSTOM_REF_FIELD, Collections.emptyList(), null);
    final KiePMMLDefineFunction defineFunction = new KiePMMLDefineFunction(CUSTOM_FUNCTION, Collections.emptyList(), DATA_TYPE.DOUBLE, OP_TYPE.CONTINUOUS, Collections.emptyList(), kiePMMLFieldRef1);
    // <Apply function="CUSTOM_FUNCTION">
    // <Constant>5.0</Constant>
    // </Apply>
    final KiePMMLConstant kiePMMLConstant2 = new KiePMMLConstant(PARAM_2, Collections.emptyList(), value2, null);
    KiePMMLApply kiePMMLApply = KiePMMLApply.builder("NAME", Collections.emptyList(), CUSTOM_FUNCTION).withKiePMMLExpressions(Collections.singletonList(kiePMMLConstant2)).build();
    KiePMMLOutputField outputField = KiePMMLOutputField.builder(OUTPUT_NAME, Collections.emptyList()).withResultFeature(RESULT_FEATURE.TRANSFORMED_VALUE).withKiePMMLExpression(kiePMMLApply).build();
    // From TransformationDictionary
    KiePMMLTransformationDictionary transformationDictionary = KiePMMLTransformationDictionary.builder("transformationDictionary", Collections.emptyList()).withDefineFunctions(Collections.singletonList(defineFunction)).withDerivedFields(Collections.singletonList(derivedField)).build();
    KiePMMLTestingModel kiePMMLModel = testingModelBuilder(outputField).withKiePMMLTransformationDictionary(transformationDictionary).build();
    ProcessingDTO processingDTO = buildProcessingDTOWithEmptyNameValues(kiePMMLModel);
    PMML4Result toUpdate = new PMML4Result();
    PostProcess.populateOutputFields(toUpdate, processingDTO);
    assertFalse(toUpdate.getResultVariables().isEmpty());
    assertTrue(toUpdate.getResultVariables().containsKey(OUTPUT_NAME));
    assertEquals(value1, toUpdate.getResultVariables().get(OUTPUT_NAME));
}
Also used : CommonTestingUtility.getProcessingDTO(org.kie.pmml.commons.CommonTestingUtility.getProcessingDTO) ProcessingDTO(org.kie.pmml.commons.model.ProcessingDTO) PMML4Result(org.kie.api.pmml.PMML4Result) KiePMMLApply(org.kie.pmml.commons.model.expressions.KiePMMLApply) KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) KiePMMLOutputField(org.kie.pmml.commons.model.KiePMMLOutputField) KiePMMLTestingModel(org.kie.pmml.commons.testingutility.KiePMMLTestingModel) KiePMMLFieldRef(org.kie.pmml.commons.model.expressions.KiePMMLFieldRef) KiePMMLConstant(org.kie.pmml.commons.model.expressions.KiePMMLConstant) KiePMMLTransformationDictionary(org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary) Test(org.junit.Test)

Example 12 with KiePMMLDerivedField

use of org.kie.pmml.commons.transformations.KiePMMLDerivedField in project drools by kiegroup.

the class KiePMMLApplyTest method evaluateFromBuiltIn.

@Test
public void evaluateFromBuiltIn() {
    // <Apply function="/">
    // <Constant>33.0</Constant>
    // <Constant>27.0</Constant>
    // </Apply>
    final KiePMMLConstant kiePMMLConstant1 = new KiePMMLConstant("NAME-1", Collections.emptyList(), value1, null);
    final KiePMMLConstant kiePMMLConstant2 = new KiePMMLConstant("NAME-1", Collections.emptyList(), value2, null);
    KiePMMLApply kiePMMLApply = KiePMMLApply.builder("NAME", Collections.emptyList(), "/").withKiePMMLExpressions(Arrays.asList(kiePMMLConstant1, kiePMMLConstant2)).build();
    ProcessingDTO processingDTO = getProcessingDTO(Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
    Object retrieved = kiePMMLApply.evaluate(processingDTO);
    assertEquals(expected, retrieved);
    // 
    // <Apply function="/">
    // <Constant>33.0</Constant>
    // <FieldRef>FIELD_NAME</FieldRef>
    // </Apply>
    // Apply with a Constant and a FieldRef: returns kiePMMLConstant1 divided evaluation of FieldRef from
    // kiePMMLNameValues
    final KiePMMLFieldRef kiePMMLFieldRef = new KiePMMLFieldRef(FIELD_NAME, Collections.emptyList(), null);
    kiePMMLApply = KiePMMLApply.builder("NAME", Collections.emptyList(), "/").withKiePMMLExpressions(Arrays.asList(kiePMMLConstant1, kiePMMLFieldRef)).build();
    List<KiePMMLNameValue> kiePMMLNameValues = Collections.singletonList(new KiePMMLNameValue(FIELD_NAME, value2));
    processingDTO = getProcessingDTO(Collections.emptyList(), Collections.emptyList(), kiePMMLNameValues, Collections.emptyList());
    retrieved = kiePMMLApply.evaluate(processingDTO);
    assertEquals(expected, retrieved);
    // Apply with a Constant and a FieldRef: returns kiePMMLConstant1 divided evaluation of FieldRef from
    // derivedFields
    final KiePMMLDerivedField kiePMMLDerivedField = KiePMMLDerivedField.builder(FIELD_NAME, Collections.emptyList(), DATA_TYPE.DOUBLE, OP_TYPE.CONTINUOUS, kiePMMLConstant2).build();
    final List<KiePMMLDerivedField> derivedFields = Collections.singletonList(kiePMMLDerivedField);
    kiePMMLNameValues = Collections.singletonList(new KiePMMLNameValue("UNKNOWN", "WRONG"));
    processingDTO = getProcessingDTO(Collections.emptyList(), derivedFields, kiePMMLNameValues, Collections.emptyList());
    retrieved = kiePMMLApply.evaluate(processingDTO);
    assertEquals(expected, retrieved);
    // <Apply function="isMissing">
    // <FieldRef>FIELD_NAME</FieldRef>
    // </Apply>
    // Apply with FieldRef: returns true with missing input
    kiePMMLApply = KiePMMLApply.builder("NAME", Collections.emptyList(), "isMissing").withKiePMMLExpressions(Collections.singletonList(kiePMMLFieldRef)).build();
    processingDTO = getProcessingDTO(Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.singletonList(getReferredByFieldRef(FIELD_NAME)));
    retrieved = kiePMMLApply.evaluate(processingDTO);
    assertTrue(retrieved instanceof Boolean);
    assertTrue((boolean) retrieved);
    // Apply with FieldRef: returns false with corresponding input
    kiePMMLApply = KiePMMLApply.builder("NAME", Collections.emptyList(), "isMissing").withKiePMMLExpressions(Collections.singletonList(kiePMMLFieldRef)).build();
    kiePMMLNameValues = Collections.singletonList(new KiePMMLNameValue(FIELD_NAME, value2));
    processingDTO = getProcessingDTO(Collections.emptyList(), Collections.emptyList(), kiePMMLNameValues, Collections.singletonList(getReferredByFieldRef(FIELD_NAME)));
    retrieved = kiePMMLApply.evaluate(processingDTO);
    assertTrue(retrieved instanceof Boolean);
    assertFalse((boolean) retrieved);
}
Also used : CommonTestingUtility.getProcessingDTO(org.kie.pmml.commons.CommonTestingUtility.getProcessingDTO) ProcessingDTO(org.kie.pmml.commons.model.ProcessingDTO) KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) Test(org.junit.Test)

Example 13 with KiePMMLDerivedField

use of org.kie.pmml.commons.transformations.KiePMMLDerivedField in project drools by kiegroup.

the class KiePMMLFieldRefTest method evaluateFromDerivedFields.

@Test
public void evaluateFromDerivedFields() {
    final Object value = 234.45;
    final KiePMMLConstant kiePMMLConstant = new KiePMMLConstant("NAME", Collections.emptyList(), value, null);
    final KiePMMLDerivedField kiePMMLDerivedField = KiePMMLDerivedField.builder(FIELD_NAME, Collections.emptyList(), DATA_TYPE.DOUBLE, OP_TYPE.CONTINUOUS, kiePMMLConstant).build();
    final List<KiePMMLDerivedField> derivedFields = Collections.singletonList(kiePMMLDerivedField);
    final List<KiePMMLNameValue> kiePMMLNameValues = Collections.singletonList(new KiePMMLNameValue("UNKNOWN", "WRONG"));
    final KiePMMLFieldRef kiePMMLFieldRef = new KiePMMLFieldRef(FIELD_NAME, Collections.emptyList(), null);
    ProcessingDTO processingDTO = getProcessingDTO(derivedFields, kiePMMLNameValues);
    final Object retrieved = kiePMMLFieldRef.evaluate(processingDTO);
    assertEquals(value, retrieved);
}
Also used : CommonTestingUtility.getProcessingDTO(org.kie.pmml.commons.CommonTestingUtility.getProcessingDTO) ProcessingDTO(org.kie.pmml.commons.model.ProcessingDTO) KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) Test(org.junit.Test)

Example 14 with KiePMMLDerivedField

use of org.kie.pmml.commons.transformations.KiePMMLDerivedField in project drools by kiegroup.

the class KiePMMLFieldRefTest method evaluateFromMapMissingTo.

@Test
public void evaluateFromMapMissingTo() {
    final String value = "234.45";
    final KiePMMLConstant kiePMMLConstant = new KiePMMLConstant("NAME", Collections.emptyList(), "WRONG-CONSTANT", null);
    final KiePMMLDerivedField kiePMMLDerivedField = KiePMMLDerivedField.builder("ANOTHER_FIELD", Collections.emptyList(), DATA_TYPE.DOUBLE, OP_TYPE.CONTINUOUS, kiePMMLConstant).build();
    final List<KiePMMLDerivedField> derivedFields = Collections.singletonList(kiePMMLDerivedField);
    final List<KiePMMLNameValue> kiePMMLNameValues = Collections.singletonList(new KiePMMLNameValue("UNKNOWN", "WRONG"));
    final KiePMMLFieldRef kiePMMLFieldRef = new KiePMMLFieldRef(FIELD_NAME, Collections.emptyList(), value);
    ProcessingDTO processingDTO = getProcessingDTO(derivedFields, kiePMMLNameValues);
    final Object retrieved = kiePMMLFieldRef.evaluate(processingDTO);
    assertEquals(value, retrieved);
}
Also used : CommonTestingUtility.getProcessingDTO(org.kie.pmml.commons.CommonTestingUtility.getProcessingDTO) ProcessingDTO(org.kie.pmml.commons.model.ProcessingDTO) KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) Test(org.junit.Test)

Example 15 with KiePMMLDerivedField

use of org.kie.pmml.commons.transformations.KiePMMLDerivedField in project drools by kiegroup.

the class KiePMMLAttributeTest method getDerivedFields.

private List<KiePMMLDerivedField> getDerivedFields() {
    // <DerivedField name="PARAM_1" optype="continuous" dataType="double">
    // <Constant>100.0</Constant>
    // </DerivedField>
    final KiePMMLConstant kiePMMLConstant1 = new KiePMMLConstant(PARAM_1, Collections.emptyList(), value1, null);
    final KiePMMLDerivedField derivedField1 = KiePMMLDerivedField.builder(PARAM_1, Collections.emptyList(), DATA_TYPE.DOUBLE, OP_TYPE.CONTINUOUS, kiePMMLConstant1).build();
    // <DerivedField name="PARAM_1" optype="continuous" dataType="double">
    // <Constant>5.0</Constant>
    // </DerivedField>
    final KiePMMLConstant kiePMMLConstant2 = new KiePMMLConstant(PARAM_2, Collections.emptyList(), value2, null);
    final KiePMMLDerivedField derivedField2 = KiePMMLDerivedField.builder(PARAM_2, Collections.emptyList(), DATA_TYPE.DOUBLE, OP_TYPE.CONTINUOUS, kiePMMLConstant2).build();
    return Arrays.asList(derivedField1, derivedField2);
}
Also used : KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) KiePMMLConstant(org.kie.pmml.commons.model.expressions.KiePMMLConstant)

Aggregations

KiePMMLDerivedField (org.kie.pmml.commons.transformations.KiePMMLDerivedField)15 Test (org.junit.Test)12 ProcessingDTO (org.kie.pmml.commons.model.ProcessingDTO)9 CommonTestingUtility.getProcessingDTO (org.kie.pmml.commons.CommonTestingUtility.getProcessingDTO)8 KiePMMLConstant (org.kie.pmml.commons.model.expressions.KiePMMLConstant)7 KiePMMLNameValue (org.kie.pmml.commons.model.tuples.KiePMMLNameValue)6 KiePMMLTransformationDictionary (org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary)6 KiePMMLApply (org.kie.pmml.commons.model.expressions.KiePMMLApply)5 KiePMMLFieldRef (org.kie.pmml.commons.model.expressions.KiePMMLFieldRef)5 KiePMMLTestingModel (org.kie.pmml.commons.testingutility.KiePMMLTestingModel)5 PMML4Result (org.kie.api.pmml.PMML4Result)4 KiePMMLOutputField (org.kie.pmml.commons.model.KiePMMLOutputField)4 KiePMMLLocalTransformations (org.kie.pmml.commons.transformations.KiePMMLLocalTransformations)4 Collections (java.util.Collections)3 List (java.util.List)3 Optional (java.util.Optional)3 DerivedField (org.dmg.pmml.DerivedField)3 Assert.assertEquals (org.junit.Assert.assertEquals)3 Assert.assertTrue (org.junit.Assert.assertTrue)3 KiePMMLDefineFunction (org.kie.pmml.commons.transformations.KiePMMLDefineFunction)3