Search in sources :

Example 6 with KiePMMLDefineFunction

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

the class KiePMMLDefineFunctionInstanceFactoryTest method getKiePMMLDefineFunction.

@Test
public void getKiePMMLDefineFunction() {
    final String functionName = "functionName";
    final DefineFunction toConvert = getDefineFunction(functionName);
    KiePMMLDefineFunction retrieved = KiePMMLDefineFunctionInstanceFactory.getKiePMMLDefineFunction(toConvert);
    commonVerifyKiePMMLDefineFunction(retrieved, toConvert);
}
Also used : 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) PMMLModelTestUtils.getDefineFunction(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDefineFunction) InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction) Test(org.junit.Test)

Example 7 with KiePMMLDefineFunction

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

the class PostProcessTest method populateTransformedOutputFieldWithApplyDefineFunctionFromApply.

@Test
public void populateTransformedOutputFieldWithApplyDefineFunctionFromApply() {
    // <DefineFunction name="CUSTOM_FUNCTION" optype="continuous" dataType="double">
    // <ParameterField name="PARAM_1" />
    // <Apply function="/">
    // <FieldRef>PARAM_1</FieldRef>
    // <Constant>5.0</Constant>
    // </Apply>
    // </DefineFunction>
    final KiePMMLParameterField kiePMMLParameterField = KiePMMLParameterField.builder(PARAM_1, Collections.emptyList()).build();
    final KiePMMLFieldRef kiePMMLFieldRef1 = new KiePMMLFieldRef(PARAM_1, Collections.emptyList(), null);
    final KiePMMLConstant kiePMMLConstant2 = new KiePMMLConstant(PARAM_2, Collections.emptyList(), value2, null);
    final KiePMMLApply kiePMMLApplyRef = KiePMMLApply.builder("NAMEREF", Collections.emptyList(), "/").withKiePMMLExpressions(Arrays.asList(kiePMMLFieldRef1, kiePMMLConstant2)).build();
    final KiePMMLDefineFunction defineFunction = new KiePMMLDefineFunction(CUSTOM_FUNCTION, Collections.emptyList(), DATA_TYPE.DOUBLE, OP_TYPE.CONTINUOUS, Collections.singletonList(kiePMMLParameterField), kiePMMLApplyRef);
    // <Apply function="CUSTOM_FUNCTION">
    // <Constant>100.0</Constant>
    // </Apply>
    final KiePMMLConstant kiePMMLConstant3 = new KiePMMLConstant(PARAM_2, Collections.emptyList(), value1, null);
    KiePMMLApply kiePMMLApply = KiePMMLApply.builder("NAME", Collections.emptyList(), CUSTOM_FUNCTION).withKiePMMLExpressions(Collections.singletonList(kiePMMLConstant3)).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)).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 / value2, 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) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) KiePMMLOutputField(org.kie.pmml.commons.model.KiePMMLOutputField) KiePMMLTestingModel(org.kie.pmml.commons.testingutility.KiePMMLTestingModel) KiePMMLParameterField(org.kie.pmml.commons.transformations.KiePMMLParameterField) 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 8 with KiePMMLDefineFunction

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

the class InstanceFactoriesTestCommon method commonVerifyKiePMMLDefineFunction.

static void commonVerifyKiePMMLDefineFunction(KiePMMLDefineFunction toVerify, DefineFunction source) {
    assertNotNull(toVerify);
    assertEquals(source.getName(), toVerify.getName());
    DATA_TYPE expectedDataType = DATA_TYPE.byName(source.getDataType().value());
    assertEquals(expectedDataType, toVerify.getDataType());
    OP_TYPE expectedOpType = OP_TYPE.byName(source.getOpType().value());
    assertEquals(expectedOpType, toVerify.getOpType());
    commonVerifyKiePMMLExpression(toVerify.getKiePMMLExpression(), source.getExpression());
    List<ParameterField> sourcesParameterFields = source.getParameterFields();
    List<KiePMMLParameterField> toVerifyList = toVerify.getParameterFields();
    assertEquals(sourcesParameterFields.size(), toVerifyList.size());
    sourcesParameterFields.forEach(paramSource -> {
        Optional<KiePMMLParameterField> parameterToVerify = toVerifyList.stream().filter(param -> param.getName().equals(paramSource.getName().getValue())).findFirst();
        assertTrue(parameterToVerify.isPresent());
        commonVerifyKiePMMLParameterField(parameterToVerify.get(), paramSource);
    });
}
Also used : Interval(org.dmg.pmml.Interval) KiePMMLParameterField(org.kie.pmml.commons.transformations.KiePMMLParameterField) FieldColumnPair(org.dmg.pmml.FieldColumnPair) KiePMMLMiningField(org.kie.pmml.commons.model.KiePMMLMiningField) NormDiscrete(org.dmg.pmml.NormDiscrete) KiePMMLInterval(org.kie.pmml.commons.model.expressions.KiePMMLInterval) KiePMMLTruePredicate(org.kie.pmml.commons.model.predicates.KiePMMLTruePredicate) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) Row(org.dmg.pmml.Row) KiePMMLOutputField(org.kie.pmml.commons.model.KiePMMLOutputField) KiePMMLInlineTable(org.kie.pmml.commons.model.expressions.KiePMMLInlineTable) KiePMMLLinearNorm(org.kie.pmml.commons.model.expressions.KiePMMLLinearNorm) KiePMMLNormContinuous(org.kie.pmml.commons.model.expressions.KiePMMLNormContinuous) DiscretizeBin(org.dmg.pmml.DiscretizeBin) KiePMMLRow(org.kie.pmml.commons.model.expressions.KiePMMLRow) Assert.fail(org.junit.Assert.fail) TextIndexNormalization(org.dmg.pmml.TextIndexNormalization) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate) True(org.dmg.pmml.True) KiePMMLDiscretizeBin(org.kie.pmml.commons.model.expressions.KiePMMLDiscretizeBin) CompoundPredicate(org.dmg.pmml.CompoundPredicate) KiePMMLFieldColumnPair(org.kie.pmml.commons.model.expressions.KiePMMLFieldColumnPair) KiePMMLTargetValue(org.kie.pmml.commons.model.KiePMMLTargetValue) KiePMMLSimpleSetPredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimpleSetPredicate) NormContinuous(org.dmg.pmml.NormContinuous) KiePMMLDiscretize(org.kie.pmml.commons.model.expressions.KiePMMLDiscretize) DataField(org.dmg.pmml.DataField) KiePMMLTextIndexNormalization(org.kie.pmml.commons.model.expressions.KiePMMLTextIndexNormalization) List(java.util.List) KiePMMLApply(org.kie.pmml.commons.model.expressions.KiePMMLApply) Optional(java.util.Optional) ParameterField(org.dmg.pmml.ParameterField) KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) IntStream(java.util.stream.IntStream) KiePMMLConstant(org.kie.pmml.commons.model.expressions.KiePMMLConstant) Predicate(org.dmg.pmml.Predicate) DefineFunction(org.dmg.pmml.DefineFunction) InlineTable(org.dmg.pmml.InlineTable) OutputField(org.dmg.pmml.OutputField) KiePMMLMapValues(org.kie.pmml.commons.model.expressions.KiePMMLMapValues) KiePMMLNormDiscrete(org.kie.pmml.commons.model.expressions.KiePMMLNormDiscrete) DerivedField(org.dmg.pmml.DerivedField) CAST_INTEGER(org.kie.pmml.api.enums.CAST_INTEGER) Discretize(org.dmg.pmml.Discretize) KiePMMLFieldRef(org.kie.pmml.commons.model.expressions.KiePMMLFieldRef) MiningField(org.dmg.pmml.MiningField) KiePMMLExpression(org.kie.pmml.commons.model.expressions.KiePMMLExpression) TargetValue(org.dmg.pmml.TargetValue) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) Expression(org.dmg.pmml.Expression) False(org.dmg.pmml.False) KiePMMLSimplePredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate) Assert.assertNotNull(org.junit.Assert.assertNotNull) TextIndex(org.dmg.pmml.TextIndex) FieldRef(org.dmg.pmml.FieldRef) KiePMMLCompoundPredicate(org.kie.pmml.commons.model.predicates.KiePMMLCompoundPredicate) Assert.assertTrue(org.junit.Assert.assertTrue) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) Target(org.dmg.pmml.Target) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) KiePMMLPredicate(org.kie.pmml.commons.model.predicates.KiePMMLPredicate) Array(org.dmg.pmml.Array) SimplePredicate(org.dmg.pmml.SimplePredicate) KiePMMLTarget(org.kie.pmml.commons.model.KiePMMLTarget) KiePMMLTextIndex(org.kie.pmml.commons.model.expressions.KiePMMLTextIndex) LinearNorm(org.dmg.pmml.LinearNorm) MapValues(org.dmg.pmml.MapValues) Apply(org.dmg.pmml.Apply) EXPRESSION_NOT_MANAGED(org.kie.pmml.commons.Constants.EXPRESSION_NOT_MANAGED) Constant(org.dmg.pmml.Constant) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLFalsePredicate(org.kie.pmml.commons.model.predicates.KiePMMLFalsePredicate) Assert.assertEquals(org.junit.Assert.assertEquals) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) KiePMMLParameterField(org.kie.pmml.commons.transformations.KiePMMLParameterField) KiePMMLParameterField(org.kie.pmml.commons.transformations.KiePMMLParameterField) ParameterField(org.dmg.pmml.ParameterField)

Example 9 with KiePMMLDefineFunction

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

the class KiePMMLApply method evaluate.

@Override
public Object evaluate(final ProcessingDTO processingDTO) {
    if (kiePMMLExpressions == null) {
        return null;
    }
    // <- Insertion order matter
    List<Object> expressionValues = new ArrayList<>();
    MiningField referredByFieldRef = null;
    for (KiePMMLExpression kiePMMLExpression : kiePMMLExpressions) {
        expressionValues.add(kiePMMLExpression.evaluate(processingDTO));
        if (kiePMMLExpression instanceof KiePMMLFieldRef && BUILTIN_FUNCTIONS.isBUILTIN_FUNCTIONS_VALIDATION(function)) {
            String referredField = ((KiePMMLFieldRef) kiePMMLExpression).getName();
            referredByFieldRef = processingDTO.getMiningFields().stream().filter(miningField -> referredField.equals(miningField.getName())).findFirst().orElseThrow(() -> new IllegalArgumentException(String.format("Missing required field %s", referredField)));
        }
    }
    if (BUILTIN_FUNCTIONS.isBUILTIN_FUNCTIONS(function)) {
        BUILTIN_FUNCTIONS builtinFunction = BUILTIN_FUNCTIONS.byName(function);
        return builtinFunction.getValue(expressionValues.toArray(new Object[0]), referredByFieldRef);
    } else {
        final KiePMMLDefineFunction definedFunction = processingDTO.getDefineFunctions().stream().filter(defineFunction -> defineFunction.getName().equals(function)).findFirst().orElseThrow(() -> new IllegalArgumentException("Unknown function " + function));
        return definedFunction.evaluate(processingDTO, expressionValues);
    }
}
Also used : Objects(java.util.Objects) List(java.util.List) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) BUILTIN_FUNCTIONS(org.kie.pmml.api.enums.BUILTIN_FUNCTIONS) StringJoiner(java.util.StringJoiner) KiePMMLExtension(org.kie.pmml.commons.model.KiePMMLExtension) ProcessingDTO(org.kie.pmml.commons.model.ProcessingDTO) AbstractKiePMMLComponent(org.kie.pmml.commons.model.abstracts.AbstractKiePMMLComponent) Collections(java.util.Collections) ArrayList(java.util.ArrayList) INVALID_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD) MiningField(org.kie.pmml.api.models.MiningField) BUILTIN_FUNCTIONS(org.kie.pmml.api.enums.BUILTIN_FUNCTIONS) MiningField(org.kie.pmml.api.models.MiningField) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) ArrayList(java.util.ArrayList)

Example 10 with KiePMMLDefineFunction

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

the class PostProcessTest method populateTransformedOutputFieldWithApplyDefineFunctionFromConstant.

@Test
public void populateTransformedOutputFieldWithApplyDefineFunctionFromConstant() {
    // <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(), DATA_TYPE.DOUBLE, OP_TYPE.CONTINUOUS, Collections.emptyList(), kiePMMLConstant1);
    // <Apply function="CUSTOM_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(), CUSTOM_FUNCTION).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()).withDefineFunctions(Collections.singletonList(defineFunction)).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) 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)

Aggregations

KiePMMLDefineFunction (org.kie.pmml.commons.transformations.KiePMMLDefineFunction)13 Test (org.junit.Test)7 KiePMMLParameterField (org.kie.pmml.commons.transformations.KiePMMLParameterField)7 ProcessingDTO (org.kie.pmml.commons.model.ProcessingDTO)6 KiePMMLApply (org.kie.pmml.commons.model.expressions.KiePMMLApply)5 KiePMMLConstant (org.kie.pmml.commons.model.expressions.KiePMMLConstant)5 KiePMMLFieldRef (org.kie.pmml.commons.model.expressions.KiePMMLFieldRef)5 KiePMMLTransformationDictionary (org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary)5 List (java.util.List)4 CommonTestingUtility.getProcessingDTO (org.kie.pmml.commons.CommonTestingUtility.getProcessingDTO)4 KiePMMLOutputField (org.kie.pmml.commons.model.KiePMMLOutputField)4 KiePMMLTestingModel (org.kie.pmml.commons.testingutility.KiePMMLTestingModel)4 KiePMMLDerivedField (org.kie.pmml.commons.transformations.KiePMMLDerivedField)4 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 Optional (java.util.Optional)3 DefineFunction (org.dmg.pmml.DefineFunction)3 PMML4Result (org.kie.api.pmml.PMML4Result)3 IntStream (java.util.stream.IntStream)2 DerivedField (org.dmg.pmml.DerivedField)2