Search in sources :

Example 31 with ProcessingDTO

use of org.kie.pmml.commons.model.ProcessingDTO in project drools by kiegroup.

the class PostProcessTest method populateTransformedOutputFieldWithApplyDerivedFieldFromConstant.

@Test
public void populateTransformedOutputFieldWithApplyDerivedFieldFromConstant() {
    // <DerivedField name="CUSTOM_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_FIELD, Collections.emptyList(), DATA_TYPE.DOUBLE, OP_TYPE.CONTINUOUS, kiePMMLConstant1).build();
    // <Apply function="/">
    // <FieldRef>CUSTOM_FIELD</FieldRef>
    // <Constant>5.0</Constant>
    // </Apply>
    final KiePMMLFieldRef kiePMMLFieldRef = 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(kiePMMLFieldRef, 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(Collections.singletonList(derivedField)).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(Collections.singletonList(derivedField)).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)

Example 32 with ProcessingDTO

use of org.kie.pmml.commons.model.ProcessingDTO 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 33 with ProcessingDTO

use of org.kie.pmml.commons.model.ProcessingDTO in project drools by kiegroup.

the class PostProcessTest method executeTargets.

@Test
public void executeTargets() {
    // Build model
    String TARGET_NAME = "TARGET_NAME";
    String FIELD_NAME = "FIELD_NAME";
    TargetField targetField = new TargetField(Collections.emptyList(), null, FIELD_NAME, null, 4.34, null, null, null);
    KiePMMLTarget kiePMMLTarget = KiePMMLTarget.builder(TARGET_NAME, Collections.emptyList(), targetField).build();
    List<KiePMMLTarget> kiePMMLTargets = Arrays.asList(kiePMMLTarget, KiePMMLTarget.builder("NEW_TARGET", Collections.emptyList(), new TargetField(Collections.emptyList(), null, "NEW_TARGET", null, null, null, null, null)).build());
    KiePMMLTestingModel model = KiePMMLTestingModel.builder("TESTINGMODEL", Collections.emptyList(), MINING_FUNCTION.REGRESSION).withKiePMMLTargets(kiePMMLTargets).build();
    // Build PMML4Result
    PMML4Result toModify = new PMML4Result();
    toModify.setResultCode(ResultCode.FAIL.getName());
    toModify.addResultVariable(FIELD_NAME, 4.33);
    assertEquals(4.33, toModify.getResultVariables().get(FIELD_NAME));
    ProcessingDTO processingDTO = getProcessingDTO(model, new ArrayList<>());
    PostProcess.executeTargets(toModify, processingDTO);
    assertEquals(4.33, toModify.getResultVariables().get(FIELD_NAME));
    toModify.setResultCode(ResultCode.OK.getName());
    PostProcess.executeTargets(toModify, processingDTO);
    assertEquals(4.33, toModify.getResultVariables().get(FIELD_NAME));
    toModify.setResultObjectName(FIELD_NAME);
    PostProcess.executeTargets(toModify, processingDTO);
    assertEquals(4.34, toModify.getResultVariables().get(FIELD_NAME));
}
Also used : CommonTestingUtility.getProcessingDTO(org.kie.pmml.commons.CommonTestingUtility.getProcessingDTO) ProcessingDTO(org.kie.pmml.commons.model.ProcessingDTO) PMML4Result(org.kie.api.pmml.PMML4Result) KiePMMLTestingModel(org.kie.pmml.commons.testingutility.KiePMMLTestingModel) TargetField(org.kie.pmml.api.models.TargetField) KiePMMLTarget(org.kie.pmml.commons.model.KiePMMLTarget) Test(org.junit.Test)

Example 34 with ProcessingDTO

use of org.kie.pmml.commons.model.ProcessingDTO in project drools by kiegroup.

the class KiePMMLDefineFunctionTest method evaluateFromApply.

@Test
public void evaluateFromApply() {
    // <DefineFunction name="CUSTOM_FUNCTION" optype="continuous" dataType="double">
    // <ParameterField name="PARAM_1"/>
    // <ParameterField field="PARAM_2"/>
    // <Apply function="/">
    // <FieldRef>PARAM_1</FieldRef>
    // <FieldRef>PARAM_2</FieldRef>
    // </Apply>
    // </DefineFunction>
    final KiePMMLFieldRef kiePMMLFieldRef1 = new KiePMMLFieldRef(PARAM_1, Collections.emptyList(), null);
    final KiePMMLFieldRef kiePMMLFieldRef2 = new KiePMMLFieldRef(PARAM_2, Collections.emptyList(), null);
    final KiePMMLApply kiePMMLApply = KiePMMLApply.builder("NAME", Collections.emptyList(), "/").withKiePMMLExpressions(Arrays.asList(kiePMMLFieldRef1, kiePMMLFieldRef2)).build();
    final KiePMMLParameterField parameterField1 = KiePMMLParameterField.builder(PARAM_1, Collections.emptyList()).build();
    final KiePMMLParameterField parameterField2 = KiePMMLParameterField.builder(PARAM_2, Collections.emptyList()).build();
    final KiePMMLDefineFunction defineFunction = new KiePMMLDefineFunction(CUSTOM_FUNCTION, Collections.emptyList(), DATA_TYPE.DOUBLE, OP_TYPE.CONTINUOUS, Arrays.asList(parameterField1, parameterField2), kiePMMLApply);
    ProcessingDTO processingDTO = getProcessingDTO(new ArrayList<>());
    Object retrieved = defineFunction.evaluate(processingDTO, Arrays.asList(value1, value2));
    Object expected = value1 / value2;
    assertEquals(expected, retrieved);
}
Also used : CommonTestingUtility.getProcessingDTO(org.kie.pmml.commons.CommonTestingUtility.getProcessingDTO) ProcessingDTO(org.kie.pmml.commons.model.ProcessingDTO) KiePMMLApply(org.kie.pmml.commons.model.expressions.KiePMMLApply) KiePMMLFieldRef(org.kie.pmml.commons.model.expressions.KiePMMLFieldRef) Test(org.junit.Test)

Example 35 with ProcessingDTO

use of org.kie.pmml.commons.model.ProcessingDTO in project drools by kiegroup.

the class KiePMMLDefineFunctionTest method evaluateFromConstant.

@Test
public void evaluateFromConstant() {
    // <DefineFunction name="CUSTOM_FUNCTION" optype="continuous" dataType="double">
    // <Constant>100.0</Constant>
    // </DefineFunction>
    final KiePMMLConstant kiePMMLConstant1 = new KiePMMLConstant(PARAM_1, Collections.emptyList(), value1, null);
    final KiePMMLDefineFunction defineFunction = new KiePMMLDefineFunction(CUSTOM_FUNCTION, Collections.emptyList(), null, OP_TYPE.CONTINUOUS, Collections.emptyList(), kiePMMLConstant1);
    ProcessingDTO processingDTO = getProcessingDTO(Collections.emptyList());
    Object retrieved = defineFunction.evaluate(processingDTO, Collections.emptyList());
    assertEquals(value1, retrieved);
}
Also used : CommonTestingUtility.getProcessingDTO(org.kie.pmml.commons.CommonTestingUtility.getProcessingDTO) ProcessingDTO(org.kie.pmml.commons.model.ProcessingDTO) KiePMMLConstant(org.kie.pmml.commons.model.expressions.KiePMMLConstant) Test(org.junit.Test)

Aggregations

ProcessingDTO (org.kie.pmml.commons.model.ProcessingDTO)48 Test (org.junit.Test)44 CommonTestingUtility.getProcessingDTO (org.kie.pmml.commons.CommonTestingUtility.getProcessingDTO)41 KiePMMLNameValue (org.kie.pmml.commons.model.tuples.KiePMMLNameValue)19 PMML4Result (org.kie.api.pmml.PMML4Result)15 KiePMMLTestingModel (org.kie.pmml.commons.testingutility.KiePMMLTestingModel)15 KiePMMLOutputField (org.kie.pmml.commons.model.KiePMMLOutputField)13 KiePMMLFieldRef (org.kie.pmml.commons.model.expressions.KiePMMLFieldRef)13 KiePMMLConstant (org.kie.pmml.commons.model.expressions.KiePMMLConstant)12 KiePMMLApply (org.kie.pmml.commons.model.expressions.KiePMMLApply)11 KiePMMLDerivedField (org.kie.pmml.commons.transformations.KiePMMLDerivedField)9 KiePMMLTransformationDictionary (org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary)7 KiePMMLDefineFunction (org.kie.pmml.commons.transformations.KiePMMLDefineFunction)6 Collections (java.util.Collections)4 List (java.util.List)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 IntStream (java.util.stream.IntStream)3