Search in sources :

Example 1 with KiePMMLDerivedField

use of org.kie.pmml.commons.transformations.KiePMMLDerivedField 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());
}
Also used : IntStream(java.util.stream.IntStream) CLOSURE(org.kie.pmml.api.enums.CLOSURE) KiePMMLConstant(org.kie.pmml.commons.model.expressions.KiePMMLConstant) Arrays(java.util.Arrays) KiePMMLParameterField(org.kie.pmml.commons.transformations.KiePMMLParameterField) KiePMMLMiningField(org.kie.pmml.commons.model.KiePMMLMiningField) KiePMMLInterval(org.kie.pmml.commons.model.expressions.KiePMMLInterval) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) ArrayList(java.util.ArrayList) MiningField(org.kie.pmml.api.models.MiningField) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) KiePMMLTransformationDictionary(org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary) Map(java.util.Map) ProcessingDTO(org.kie.pmml.commons.model.ProcessingDTO) KiePMMLFieldRef(org.kie.pmml.commons.model.expressions.KiePMMLFieldRef) MISSING_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.MISSING_VALUE_TREATMENT_METHOD) INVALID_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD) MINING_FUNCTION(org.kie.pmml.api.enums.MINING_FUNCTION) ParameterInfo(org.kie.api.pmml.ParameterInfo) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) List(java.util.List) KiePMMLApply(org.kie.pmml.commons.model.expressions.KiePMMLApply) Assert.assertNull(org.junit.Assert.assertNull) PMMLRequestData(org.kie.api.pmml.PMMLRequestData) KiePMMLTestingModel(org.kie.pmml.commons.testingutility.KiePMMLTestingModel) Optional(java.util.Optional) Collections(java.util.Collections) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) Assert.assertEquals(org.junit.Assert.assertEquals) KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) PMMLRequestData(org.kie.api.pmml.PMMLRequestData) KiePMMLApply(org.kie.pmml.commons.model.expressions.KiePMMLApply) KiePMMLTestingModel(org.kie.pmml.commons.testingutility.KiePMMLTestingModel) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) ParameterInfo(org.kie.api.pmml.ParameterInfo) KiePMMLConstant(org.kie.pmml.commons.model.expressions.KiePMMLConstant) KiePMMLTransformationDictionary(org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary) ProcessingDTO(org.kie.pmml.commons.model.ProcessingDTO) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) KiePMMLParameterField(org.kie.pmml.commons.transformations.KiePMMLParameterField) KiePMMLFieldRef(org.kie.pmml.commons.model.expressions.KiePMMLFieldRef) Test(org.junit.Test)

Example 2 with KiePMMLDerivedField

use of org.kie.pmml.commons.transformations.KiePMMLDerivedField 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));
        }
    }
}
Also used : KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue)

Example 3 with KiePMMLDerivedField

use of org.kie.pmml.commons.transformations.KiePMMLDerivedField 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);
    });
}
Also used : InstanceFactoriesTestCommon.commonVerifyKiePMMLDerivedField(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDerivedField) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) DefineFunction(org.dmg.pmml.DefineFunction) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) List(java.util.List) TransformationDictionary(org.dmg.pmml.TransformationDictionary) DerivedField(org.dmg.pmml.DerivedField) KiePMMLTransformationDictionary(org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary) PMMLModelTestUtils.getRandomTransformationDictionary(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTransformationDictionary) Optional(java.util.Optional) Collections(java.util.Collections) InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction) Assert.assertEquals(org.junit.Assert.assertEquals) KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) TransformationDictionary(org.dmg.pmml.TransformationDictionary) KiePMMLTransformationDictionary(org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary) PMMLModelTestUtils.getRandomTransformationDictionary(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTransformationDictionary) KiePMMLTransformationDictionary(org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary) InstanceFactoriesTestCommon.commonVerifyKiePMMLDerivedField(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDerivedField) KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) DefineFunction(org.dmg.pmml.DefineFunction) InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction) InstanceFactoriesTestCommon.commonVerifyKiePMMLDerivedField(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDerivedField) DerivedField(org.dmg.pmml.DerivedField) KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) Test(org.junit.Test)

Example 4 with KiePMMLDerivedField

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

the class KiePMMLFieldRefTest method evaluateNull.

@Test
public void evaluateNull() {
    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(), null);
    ProcessingDTO processingDTO = getProcessingDTO(derivedFields, kiePMMLNameValues);
    final Object retrieved = kiePMMLFieldRef.evaluate(processingDTO);
    assertNull(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 5 with KiePMMLDerivedField

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

the class PostProcessTest method populateTransformedOutputFieldWithApplyDerivedFieldFromFieldRef.

@Test
public void populateTransformedOutputFieldWithApplyDerivedFieldFromFieldRef() {
    // <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 derivedField1 = KiePMMLDerivedField.builder(CUSTOM_REF_FIELD, Collections.emptyList(), DATA_TYPE.DOUBLE, OP_TYPE.CONTINUOUS, kiePMMLConstant1).build();
    // <DerivedField name="CUSTOM_FIELD" optype="continuous" dataType="double">
    // <FieldRef>CUSTOM_REF_FIELD</FieldRef>
    // </DerivedField>
    final KiePMMLFieldRef kiePMMLFieldRef = new KiePMMLFieldRef(CUSTOM_REF_FIELD, Collections.emptyList(), null);
    final KiePMMLDerivedField derivedField2 = KiePMMLDerivedField.builder(CUSTOM_FIELD, Collections.emptyList(), DATA_TYPE.DOUBLE, OP_TYPE.CONTINUOUS, kiePMMLFieldRef).build();
    // <Apply function="/">
    // <FieldRef>CUSTOM_FIELD</FieldRef>
    // <Constant>5.0</Constant>
    // </Apply>
    final KiePMMLFieldRef kiePMMLFieldRef2 = new KiePMMLFieldRef(CUSTOM_FIELD, Collections.emptyList(), null);
    final KiePMMLConstant kiePMMLConstant2 = new KiePMMLConstant(PARAM_2, Collections.emptyList(), value2, null);
    KiePMMLApply kiePMMLApply = KiePMMLApply.builder("NAME", Collections.emptyList(), "/").withKiePMMLExpressions(Arrays.asList(kiePMMLFieldRef2, 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()).withDerivedFields(Arrays.asList(derivedField1, derivedField2)).build();
    KiePMMLTestingModel kiePMMLModel1 = testingModelBuilder(outputField).withKiePMMLTransformationDictionary(transformationDictionary).build();
    ProcessingDTO processingDTO1 = buildProcessingDTOWithEmptyNameValues(kiePMMLModel1);
    PMML4Result toUpdate1 = new PMML4Result();
    PostProcess.populateOutputFields(toUpdate1, processingDTO1);
    assertFalse(toUpdate1.getResultVariables().isEmpty());
    assertTrue(toUpdate1.getResultVariables().containsKey(OUTPUT_NAME));
    assertEquals(value1 / value2, toUpdate1.getResultVariables().get(OUTPUT_NAME));
    // From LocalTransformations
    KiePMMLLocalTransformations localTransformations = KiePMMLLocalTransformations.builder("localTransformations", Collections.emptyList()).withDerivedFields(Arrays.asList(derivedField1, derivedField2)).build();
    KiePMMLTestingModel kiePMMLModel2 = testingModelBuilder(outputField).withKiePMMLLocalTransformations(localTransformations).build();
    ProcessingDTO processingDTO2 = buildProcessingDTOWithEmptyNameValues(kiePMMLModel2);
    PMML4Result toUpdate2 = new PMML4Result();
    PostProcess.populateOutputFields(toUpdate2, processingDTO2);
    assertFalse(toUpdate2.getResultVariables().isEmpty());
    assertTrue(toUpdate2.getResultVariables().containsKey(OUTPUT_NAME));
    assertEquals(value1 / value2, toUpdate2.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) KiePMMLOutputField(org.kie.pmml.commons.model.KiePMMLOutputField) KiePMMLTestingModel(org.kie.pmml.commons.testingutility.KiePMMLTestingModel) KiePMMLLocalTransformations(org.kie.pmml.commons.transformations.KiePMMLLocalTransformations) 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)

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