Search in sources :

Example 6 with OutputField

use of org.dmg.pmml.OutputField in project drools by kiegroup.

the class KiePMMLUtilTest method populateMissingOutputFieldDataType.

@Test
public void populateMissingOutputFieldDataType() {
    Random random = new Random();
    List<String> fieldNames = IntStream.range(0, 6).mapToObj(i -> RandomStringUtils.random(6, true, false)).collect(Collectors.toList());
    List<DataField> dataFields = fieldNames.stream().map(fieldName -> {
        DataField toReturn = new DataField();
        toReturn.setName(FieldName.create(fieldName));
        DataType dataType = DataType.values()[random.nextInt(DataType.values().length)];
        toReturn.setDataType(dataType);
        return toReturn;
    }).collect(Collectors.toList());
    List<MiningField> miningFields = IntStream.range(0, dataFields.size() - 1).mapToObj(dataFields::get).map(dataField -> {
        MiningField toReturn = new MiningField();
        toReturn.setName(FieldName.create(dataField.getName().getValue()));
        toReturn.setUsageType(MiningField.UsageType.ACTIVE);
        return toReturn;
    }).collect(Collectors.toList());
    DataField lastDataField = dataFields.get(dataFields.size() - 1);
    MiningField targetMiningField = new MiningField();
    targetMiningField.setName(FieldName.create(lastDataField.getName().getValue()));
    targetMiningField.setUsageType(MiningField.UsageType.TARGET);
    miningFields.add(targetMiningField);
    // Following OutputFields should be populated based on "ResultFeature.PROBABILITY"
    List<OutputField> outputFields = IntStream.range(0, 3).mapToObj(i -> {
        OutputField toReturn = new OutputField();
        toReturn.setName(FieldName.create(RandomStringUtils.random(6, true, false)));
        toReturn.setResultFeature(ResultFeature.PROBABILITY);
        return toReturn;
    }).collect(Collectors.toList());
    // Following OutputField should be populated based on "ResultFeature.PREDICTED_VALUE"
    OutputField targetOutputField = new OutputField();
    targetOutputField.setName(FieldName.create(RandomStringUtils.random(6, true, false)));
    targetOutputField.setResultFeature(ResultFeature.PREDICTED_VALUE);
    outputFields.add(targetOutputField);
    // Following OutputField should be populated based on "TargetField" property
    OutputField targetingOutputField = new OutputField();
    targetingOutputField.setName(FieldName.create(RandomStringUtils.random(6, true, false)));
    targetingOutputField.setTargetField(FieldName.create(targetMiningField.getName().getValue()));
    outputFields.add(targetingOutputField);
    outputFields.forEach(outputField -> assertNull(outputField.getDataType()));
    IntStream.range(0, 2).forEach(i -> {
        OutputField toAdd = new OutputField();
        toAdd.setName(FieldName.create(RandomStringUtils.random(6, true, false)));
        DataType dataType = DataType.values()[random.nextInt(DataType.values().length)];
        toAdd.setDataType(dataType);
        outputFields.add(toAdd);
    });
    KiePMMLUtil.populateMissingOutputFieldDataType(outputFields, miningFields, dataFields);
    outputFields.forEach(outputField -> assertNotNull(outputField.getDataType()));
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Model(org.dmg.pmml.Model) OutputField(org.dmg.pmml.OutputField) Random(java.util.Random) ResultFeature(org.dmg.pmml.ResultFeature) FieldName(org.dmg.pmml.FieldName) MODELNAME_TEMPLATE(org.kie.pmml.compiler.commons.utils.KiePMMLUtil.MODELNAME_TEMPLATE) OpType(org.dmg.pmml.OpType) Charset(java.nio.charset.Charset) MiningFunction(org.dmg.pmml.MiningFunction) MiningField(org.dmg.pmml.MiningField) MiningModel(org.dmg.pmml.mining.MiningModel) PMML(org.dmg.pmml.PMML) Targets(org.dmg.pmml.Targets) Assert.assertNotNull(org.junit.Assert.assertNotNull) DataType(org.dmg.pmml.DataType) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) JAXBException(javax.xml.bind.JAXBException) Target(org.dmg.pmml.Target) StandardCharsets(java.nio.charset.StandardCharsets) SEGMENTID_TEMPLATE(org.kie.pmml.compiler.commons.utils.KiePMMLUtil.SEGMENTID_TEMPLATE) DataField(org.dmg.pmml.DataField) SEGMENTMODELNAME_TEMPLATE(org.kie.pmml.compiler.commons.utils.KiePMMLUtil.SEGMENTMODELNAME_TEMPLATE) List(java.util.List) Segment(org.dmg.pmml.mining.Segment) TARGETFIELD_TEMPLATE(org.kie.pmml.compiler.commons.utils.KiePMMLUtil.TARGETFIELD_TEMPLATE) Assert.assertNull(org.junit.Assert.assertNull) FileUtils.getFileInputStream(org.kie.test.util.filesystem.FileUtils.getFileInputStream) Assert.assertFalse(org.junit.Assert.assertFalse) SAXException(org.xml.sax.SAXException) KiePMMLUtil.getMiningTargetFields(org.kie.pmml.compiler.commons.utils.KiePMMLUtil.getMiningTargetFields) Optional(java.util.Optional) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) BufferedReader(java.io.BufferedReader) MathContext(org.dmg.pmml.MathContext) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) MiningField(org.dmg.pmml.MiningField) Random(java.util.Random) DataField(org.dmg.pmml.DataField) DataType(org.dmg.pmml.DataType) OutputField(org.dmg.pmml.OutputField) Test(org.junit.Test)

Example 7 with OutputField

use of org.dmg.pmml.OutputField in project drools by kiegroup.

the class KiePMMLUtilTest method populateMissingPredictedOutputFieldTarget.

@Test
public void populateMissingPredictedOutputFieldTarget() throws Exception {
    final InputStream inputStream = getFileInputStream(NO_OUTPUT_FIELD_TARGET_NAME_SAMPLE);
    final PMML pmml = org.jpmml.model.PMMLUtil.unmarshal(inputStream);
    final Model toPopulate = pmml.getModels().get(0);
    final OutputField outputField = toPopulate.getOutput().getOutputFields().get(0);
    assertEquals(ResultFeature.PREDICTED_VALUE, outputField.getResultFeature());
    assertNull(outputField.getTargetField());
    KiePMMLUtil.populateMissingPredictedOutputFieldTarget(toPopulate);
    final MiningField targetField = getMiningTargetFields(toPopulate.getMiningSchema().getMiningFields()).get(0);
    assertNotNull(outputField.getTargetField());
    assertEquals(targetField.getName(), outputField.getTargetField());
}
Also used : MiningField(org.dmg.pmml.MiningField) FileUtils.getFileInputStream(org.kie.test.util.filesystem.FileUtils.getFileInputStream) InputStream(java.io.InputStream) Model(org.dmg.pmml.Model) MiningModel(org.dmg.pmml.mining.MiningModel) PMML(org.dmg.pmml.PMML) OutputField(org.dmg.pmml.OutputField) Test(org.junit.Test)

Example 8 with OutputField

use of org.dmg.pmml.OutputField in project drools by kiegroup.

the class KiePMMLOutputFieldInstanceFactoryTest method getKiePMMLOutputField.

@Test
public void getKiePMMLOutputField() {
    OutputField toConvert = getRandomOutputField();
    KiePMMLOutputField retrieved = KiePMMLOutputFieldInstanceFactory.getKiePMMLOutputField(toConvert);
    commonVerifyKiePMMLOutputField(retrieved, toConvert);
}
Also used : InstanceFactoriesTestCommon.commonVerifyKiePMMLOutputField(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLOutputField) KiePMMLOutputField(org.kie.pmml.commons.model.KiePMMLOutputField) OutputField(org.dmg.pmml.OutputField) InstanceFactoriesTestCommon.commonVerifyKiePMMLOutputField(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLOutputField) KiePMMLOutputField(org.kie.pmml.commons.model.KiePMMLOutputField) PMMLModelTestUtils.getRandomOutputField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomOutputField) Test(org.junit.Test)

Example 9 with OutputField

use of org.dmg.pmml.OutputField in project drools by kiegroup.

the class KiePMMLDescrRhsFactoryTest method commonDeclareThen.

@Test
public void commonDeclareThen() {
    String ruleName = "RULENAME";
    String statusToSet = "STATUSTOSET";
    String outputFieldName = "OUTPUTFIELDNAME";
    Object result = "RESULT";
    OutputField outputField = new OutputField();
    outputField.setName(FieldName.create(outputFieldName));
    outputField.setResultFeature(ResultFeature.PREDICTED_VALUE);
    List<OutputField> outputFields = Collections.singletonList(outputField);
    KiePMMLDroolsRule.Builder builder = KiePMMLDroolsRule.builder(ruleName, statusToSet, outputFields);
    KiePMMLDroolsRule rule = builder.build();
    StringJoiner joiner = new StringJoiner("");
    KiePMMLDescrRhsFactory.factory(ruleBuilder).commonDeclareThen(rule, joiner);
    String retrieved = joiner.toString();
    assertTrue(retrieved.isEmpty());
    // 
    ResultCode resultCode = ResultCode.OK;
    builder = builder.withResultCode(resultCode);
    rule = builder.build();
    joiner = new StringJoiner("");
    KiePMMLDescrRhsFactory.factory(ruleBuilder).commonDeclareThen(rule, joiner);
    retrieved = joiner.toString();
    String expected = String.format(SET_PMML4_RESULT_CODE, resultCode);
    assertTrue(retrieved.contains(expected));
    // 
    builder = builder.withResult(result);
    rule = builder.build();
    joiner = new StringJoiner("");
    KiePMMLDescrRhsFactory.factory(ruleBuilder).commonDeclareThen(rule, joiner);
    retrieved = joiner.toString();
    expected = String.format(ADD_PMML4_RESULT_VARIABLE, result);
    assertTrue(retrieved.contains(expected));
    expected = String.format(ADD_PMML4_OUTPUT_FIELD, outputFieldName, result);
    assertTrue(retrieved.contains(expected));
    // 
    String focusedAgendaGroup = "FOCUSEDAGENDAGROUP";
    builder = builder.withFocusedAgendaGroup(focusedAgendaGroup);
    rule = builder.build();
    joiner = new StringJoiner("");
    KiePMMLDescrRhsFactory.factory(ruleBuilder).commonDeclareThen(rule, joiner);
    retrieved = joiner.toString();
    expected = String.format(FOCUS_AGENDA_GROUP, focusedAgendaGroup);
    assertTrue(retrieved.contains(expected));
// 
}
Also used : OutputField(org.dmg.pmml.OutputField) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) StringJoiner(java.util.StringJoiner) ResultCode(org.kie.pmml.api.enums.ResultCode) Test(org.junit.Test)

Example 10 with OutputField

use of org.dmg.pmml.OutputField in project drools by kiegroup.

the class KiePMMLDescrRhsFactoryTest method commonDeclareOutputFields.

@Test
public void commonDeclareOutputFields() {
    String outputFieldName = "OUTPUTFIELDNAME";
    Object result = "RESULT";
    OutputField outputField = new OutputField();
    outputField.setName(FieldName.create(outputFieldName));
    outputField.setResultFeature(ResultFeature.PREDICTED_VALUE);
    List<OutputField> outputFields = Collections.singletonList(outputField);
    StringJoiner joiner = new StringJoiner("");
    KiePMMLDescrRhsFactory.factory(ruleBuilder).commonDeclareOutputFields(outputFields, result, joiner);
    String retrieved = joiner.toString();
    String expected = String.format(ADD_PMML4_OUTPUT_FIELD, outputFieldName, result);
    assertEquals(expected, retrieved);
}
Also used : OutputField(org.dmg.pmml.OutputField) StringJoiner(java.util.StringJoiner) Test(org.junit.Test)

Aggregations

OutputField (org.dmg.pmml.OutputField)28 Test (org.junit.Test)10 DataField (org.dmg.pmml.DataField)9 MiningField (org.dmg.pmml.MiningField)9 MiningSchema (org.dmg.pmml.MiningSchema)7 Output (org.dmg.pmml.Output)7 PMML (org.dmg.pmml.PMML)7 DataDictionary (org.dmg.pmml.DataDictionary)4 FieldName (org.dmg.pmml.FieldName)4 ResultFeature (org.dmg.pmml.ResultFeature)4 MiningModel (org.dmg.pmml.mining.MiningModel)4 DATA_TYPE (org.kie.pmml.api.enums.DATA_TYPE)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 Collectors (java.util.stream.Collectors)3 Model (org.dmg.pmml.Model)3 OpType (org.dmg.pmml.OpType)3 RegressionModel (org.dmg.pmml.regression.RegressionModel)3