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()));
}
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());
}
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);
}
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));
//
}
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);
}
Aggregations