Search in sources :

Example 26 with DATA_TYPE

use of org.kie.pmml.api.enums.DATA_TYPE in project drools by kiegroup.

the class KiePMMLParameterFieldInstanceFactory method getKiePMMLParameterField.

static KiePMMLParameterField getKiePMMLParameterField(final ParameterField parameterField) {
    DATA_TYPE dataType = parameterField.getDataType() != null ? DATA_TYPE.byName(parameterField.getDataType().value()) : null;
    OP_TYPE opType = parameterField.getOpType() != null ? OP_TYPE.byName(parameterField.getOpType().value()) : null;
    return KiePMMLParameterField.builder(parameterField.getName().getValue(), Collections.emptyList()).withDataType(dataType).withOpType(opType).withDisplayName(parameterField.getDisplayName()).build();
}
Also used : OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE)

Example 27 with DATA_TYPE

use of org.kie.pmml.api.enums.DATA_TYPE in project drools by kiegroup.

the class PreProcessTest method convertInputDataConvertibles.

@Test
public void convertInputDataConvertibles() {
    List<KiePMMLMiningField> miningFields = IntStream.range(0, 3).mapToObj(i -> {
        DATA_TYPE dataType = DATA_TYPE.values()[i];
        return KiePMMLMiningField.builder("FIELD-" + i, null).withDataType(dataType).build();
    }).collect(Collectors.toList());
    PMMLRequestData pmmlRequestData = new PMMLRequestData("123", "modelName");
    pmmlRequestData.addRequestParam("FIELD-0", 123);
    pmmlRequestData.addRequestParam("FIELD-1", "123");
    pmmlRequestData.addRequestParam("FIELD-2", "1.23");
    Map<String, ParameterInfo> mappedRequestParams = pmmlRequestData.getMappedRequestParams();
    assertEquals(123, mappedRequestParams.get("FIELD-0").getValue());
    assertEquals("123", mappedRequestParams.get("FIELD-1").getValue());
    assertEquals("1.23", mappedRequestParams.get("FIELD-2").getValue());
    PreProcess.convertInputData(miningFields, pmmlRequestData);
    assertEquals("123", mappedRequestParams.get("FIELD-0").getValue());
    assertEquals(123, mappedRequestParams.get("FIELD-1").getValue());
    assertEquals(1.23f, mappedRequestParams.get("FIELD-2").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) ParameterInfo(org.kie.api.pmml.ParameterInfo) KiePMMLMiningField(org.kie.pmml.commons.model.KiePMMLMiningField) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) Test(org.junit.Test)

Example 28 with DATA_TYPE

use of org.kie.pmml.api.enums.DATA_TYPE in project drools by kiegroup.

the class PreProcessTest method verifyAddMissingValuesMissingReturnInvalid.

@Test(expected = KiePMMLException.class)
public void verifyAddMissingValuesMissingReturnInvalid() {
    List<KiePMMLMiningField> miningFields = IntStream.range(0, 3).mapToObj(i -> {
        DATA_TYPE dataType = DATA_TYPE.values()[i];
        return KiePMMLMiningField.builder("FIELD-" + i, null).withDataType(dataType).withMissingValueTreatmentMethod(MISSING_VALUE_TREATMENT_METHOD.RETURN_INVALID).build();
    }).collect(Collectors.toList());
    PMMLRequestData pmmlRequestData = new PMMLRequestData("123", "modelName");
    PreProcess.verifyAddMissingValues(miningFields, pmmlRequestData);
}
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) KiePMMLMiningField(org.kie.pmml.commons.model.KiePMMLMiningField) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) Test(org.junit.Test)

Example 29 with DATA_TYPE

use of org.kie.pmml.api.enums.DATA_TYPE in project drools by kiegroup.

the class PostProcess method updateTargetValueType.

/**
 * Verify that the returned value has the required type as defined inside <code>DataDictionary/MiningSchema</code>
 * @param model
 * @param toUpdate
 */
static void updateTargetValueType(final KiePMMLModel model, final PMML4Result toUpdate) {
    DATA_TYPE dataType = model.getMiningFields().stream().filter(miningField -> model.getTargetField().equals(miningField.getName())).map(MiningField::getDataType).findFirst().orElseThrow(() -> new KiePMMLException("Failed to find DATA_TYPE for " + model.getTargetField()));
    Object prediction = toUpdate.getResultVariables().get(model.getTargetField());
    if (prediction != null) {
        Object convertedPrediction = dataType.getActualValue(prediction);
        toUpdate.getResultVariables().put(model.getTargetField(), convertedPrediction);
    }
}
Also used : OK(org.kie.pmml.api.enums.ResultCode.OK) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) PMML4Result(org.kie.api.pmml.PMML4Result) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) KiePMMLOutputField(org.kie.pmml.commons.model.KiePMMLOutputField) ArrayList(java.util.ArrayList) MiningField(org.kie.pmml.api.models.MiningField) LinkedHashMap(java.util.LinkedHashMap) PMMLContext(org.kie.pmml.api.runtime.PMMLContext) AbstractMap(java.util.AbstractMap) List(java.util.List) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) Map(java.util.Map) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) ProcessingDTO(org.kie.pmml.commons.model.ProcessingDTO) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) MiningField(org.kie.pmml.api.models.MiningField) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE)

Example 30 with DATA_TYPE

use of org.kie.pmml.api.enums.DATA_TYPE in project drools by kiegroup.

the class KiePMMLScorecardModelASTFactory method getKiePMMLDroolsAST.

/**
 * Returns the <code>KiePMMLDroolsAST</code> built out of the given parameters.
 * It also <b>populate</b> the <b>fieldNameTypeNameMap</b> with mapping between
 * original field' name and <b>original type/generated type</b> tupla
 *
 * @param fields
 * @param model
 * @param fieldTypeMap
 * @param types
 * @return
 */
public static KiePMMLDroolsAST getKiePMMLDroolsAST(final List<Field<?>> fields, final Scorecard model, final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap, final List<KiePMMLDroolsType> types) {
    logger.trace("getKiePMMLDroolsAST {} {} {}", fields, model, fieldTypeMap);
    DATA_TYPE targetType = getTargetFieldType(fields, model);
    List<OutputField> outputFields = model.getOutput() != null ? model.getOutput().getOutputFields() : Collections.emptyList();
    KiePMMLScorecardModelCharacteristicASTFactory factory = KiePMMLScorecardModelCharacteristicASTFactory.factory(fieldTypeMap, outputFields, targetType);
    if (model.isUseReasonCodes()) {
        factory = factory.withReasonCodes(model.getBaselineScore(), REASONCODE_ALGORITHM.byName(model.getReasonCodeAlgorithm().value()));
    }
    final List<KiePMMLDroolsRule> rules = factory.declareRulesFromCharacteristics(model.getCharacteristics(), "", model.getInitialScore());
    return new KiePMMLDroolsAST(types, rules);
}
Also used : KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) OutputField(org.dmg.pmml.OutputField) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE)

Aggregations

DATA_TYPE (org.kie.pmml.api.enums.DATA_TYPE)36 Test (org.junit.Test)17 OP_TYPE (org.kie.pmml.api.enums.OP_TYPE)15 DataField (org.dmg.pmml.DataField)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)8 Map (java.util.Map)8 INVALID_VALUE_TREATMENT_METHOD (org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD)8 MISSING_VALUE_TREATMENT_METHOD (org.kie.pmml.api.enums.MISSING_VALUE_TREATMENT_METHOD)8 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)8 List (java.util.List)7 MiningField (org.dmg.pmml.MiningField)7 KiePMMLMiningField (org.kie.pmml.commons.model.KiePMMLMiningField)7 KiePMMLInterval (org.kie.pmml.commons.model.expressions.KiePMMLInterval)7 KiePMMLDefineFunction (org.kie.pmml.commons.transformations.KiePMMLDefineFunction)7 KiePMMLParameterField (org.kie.pmml.commons.transformations.KiePMMLParameterField)7 Optional (java.util.Optional)6 Collectors (java.util.stream.Collectors)6 IntStream (java.util.stream.IntStream)6 Assert.assertEquals (org.junit.Assert.assertEquals)6