Search in sources :

Example 1 with KiePMMLConstant

use of org.kie.pmml.commons.model.expressions.KiePMMLConstant 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 KiePMMLConstant

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

the class KiePMMLDerivedFieldTest method evaluateFromConstant.

@Test
public void evaluateFromConstant() {
    // <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();
    ProcessingDTO processingDTO = getProcessingDTO(Collections.emptyList(), new ArrayList<>());
    Object retrieved = derivedField.evaluate(processingDTO);
    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)

Example 3 with KiePMMLConstant

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

the class KiePMMLDerivedFieldTest 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 : KiePMMLConstant(org.kie.pmml.commons.model.expressions.KiePMMLConstant)

Example 4 with KiePMMLConstant

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

the class KiePMMLOutputFieldTest method evaluateTransformedValueFromConstant.

@Test
public void evaluateTransformedValueFromConstant() {
    // <OutputField name="CUSTOM_FIELD" optype="continuous" dataType="double" feature="transformedValue">
    // <Constant>100.0</Constant>
    // </OutputField>
    final KiePMMLConstant kiePMMLConstant1 = new KiePMMLConstant(PARAM_1, Collections.emptyList(), value1, null);
    final KiePMMLOutputField outputField = KiePMMLOutputField.builder(CUSTOM_FIELD, Collections.emptyList()).withKiePMMLExpression(kiePMMLConstant1).withResultFeature(RESULT_FEATURE.TRANSFORMED_VALUE).build();
    ProcessingDTO processingDTO = getProcessingDTO(Collections.emptyList(), new ArrayList<>(), Collections.emptyList());
    Object retrieved = outputField.evaluate(processingDTO);
    assertEquals(value1, retrieved);
}
Also used : CommonTestingUtility.getProcessingDTO(org.kie.pmml.commons.CommonTestingUtility.getProcessingDTO) KiePMMLConstant(org.kie.pmml.commons.model.expressions.KiePMMLConstant) Test(org.junit.Test)

Example 5 with KiePMMLConstant

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

the class KiePMMLConstantInstanceFactoryTest method getKiePMMLConstant.

@Test
public void getKiePMMLConstant() {
    Object value = 2342.21;
    Constant toConvert = new Constant();
    toConvert.setValue(value);
    KiePMMLConstant retrieved = KiePMMLConstantInstanceFactory.getKiePMMLConstant(toConvert);
    commonVerifyKiePMMLConstant(retrieved, toConvert);
}
Also used : KiePMMLConstant(org.kie.pmml.commons.model.expressions.KiePMMLConstant) InstanceFactoriesTestCommon.commonVerifyKiePMMLConstant(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLConstant) Constant(org.dmg.pmml.Constant) KiePMMLConstant(org.kie.pmml.commons.model.expressions.KiePMMLConstant) InstanceFactoriesTestCommon.commonVerifyKiePMMLConstant(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLConstant) Test(org.junit.Test)

Aggregations

KiePMMLConstant (org.kie.pmml.commons.model.expressions.KiePMMLConstant)19 Test (org.junit.Test)15 CommonTestingUtility.getProcessingDTO (org.kie.pmml.commons.CommonTestingUtility.getProcessingDTO)12 ProcessingDTO (org.kie.pmml.commons.model.ProcessingDTO)12 KiePMMLTestingModel (org.kie.pmml.commons.testingutility.KiePMMLTestingModel)10 PMML4Result (org.kie.api.pmml.PMML4Result)9 KiePMMLOutputField (org.kie.pmml.commons.model.KiePMMLOutputField)9 KiePMMLApply (org.kie.pmml.commons.model.expressions.KiePMMLApply)8 KiePMMLFieldRef (org.kie.pmml.commons.model.expressions.KiePMMLFieldRef)7 KiePMMLDerivedField (org.kie.pmml.commons.transformations.KiePMMLDerivedField)7 KiePMMLTransformationDictionary (org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary)7 KiePMMLDefineFunction (org.kie.pmml.commons.transformations.KiePMMLDefineFunction)4 KiePMMLLocalTransformations (org.kie.pmml.commons.transformations.KiePMMLLocalTransformations)3 KiePMMLParameterField (org.kie.pmml.commons.transformations.KiePMMLParameterField)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1