use of org.kie.pmml.api.enums.ResultCode 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.kie.pmml.api.enums.ResultCode in project drools by kiegroup.
the class PMMLMiningModelEvaluator method getPMML4Result.
PMML4Result getPMML4Result(final KiePMMLMiningModel toEvaluate, final LinkedHashMap<String, KiePMMLNameValueProbabilityMapTuple> inputData, final PMMLContext pmmlContext) {
final MULTIPLE_MODEL_METHOD multipleModelMethod = toEvaluate.getSegmentation().getMultipleModelMethod();
Object result = null;
LinkedHashMap<String, Double> probabilityResultMap = null;
ResultCode resultCode = OK;
final LinkedHashMap<String, KiePMMLNameValue> toUseForPrediction = new LinkedHashMap<>();
final LinkedHashMap<String, List<KiePMMLNameValue>> toUseForProbability = new LinkedHashMap<>();
inputData.forEach((key, value) -> {
toUseForPrediction.put(key, value.predictionValue);
toUseForProbability.put(key, value.probabilityValues);
});
try {
if (MINING_FUNCTION.CLASSIFICATION.equals(toEvaluate.getMiningFunction())) {
result = multipleModelMethod.applyClassification(toUseForPrediction);
probabilityResultMap = multipleModelMethod.applyProbability(toUseForProbability);
} else {
result = multipleModelMethod.applyPrediction(toUseForPrediction);
}
} catch (KieEnumException e) {
logger.warn(e.getMessage());
resultCode = FAIL;
}
pmmlContext.setProbabilityResultMap(probabilityResultMap);
PMML4Result toReturn = new PMML4Result();
toReturn.addResultVariable(toEvaluate.getTargetField(), result);
toReturn.setResultObjectName(toEvaluate.getTargetField());
toReturn.setResultCode(resultCode.getName());
return toReturn;
}
use of org.kie.pmml.api.enums.ResultCode in project drools by kiegroup.
the class PMMLMiningModelEvaluatorTest method getStep.
@Test
public void getStep() {
final String modelName = "MODEL_NAME";
KiePMMLModel modelMock = mock(KiePMMLModel.class);
when(modelMock.getName()).thenReturn(modelName);
final String segmentName = "SEGMENT_NAME";
KiePMMLSegment segmentMock = mock(KiePMMLSegment.class);
when(segmentMock.getName()).thenReturn(segmentName);
when(segmentMock.getModel()).thenReturn(modelMock);
final String resultObjectName = "RESULT_OBJECT_NAME";
final String resultObjectValue = "RESULT_OBJECT_VALUE";
ResultCode resultCode = OK;
PMML4Result pmml4Result = new PMML4Result();
pmml4Result.setResultCode(resultCode.getName());
pmml4Result.setResultObjectName(resultObjectName);
pmml4Result.getResultVariables().put(resultObjectName, resultObjectValue);
PMMLStep retrieved = evaluator.getStep(segmentMock, pmml4Result);
assertNotNull(retrieved);
assertTrue(retrieved instanceof PMMLMiningModelStep);
Map<String, Object> retrievedInfo = retrieved.getInfo();
assertNotNull(retrievedInfo);
assertEquals(segmentName, retrievedInfo.get("SEGMENT"));
assertEquals(modelName, retrievedInfo.get("MODEL"));
assertEquals(resultCode.getName(), retrievedInfo.get("RESULT CODE"));
assertEquals(resultObjectValue, retrievedInfo.get("RESULT"));
resultCode = FAIL;
pmml4Result = new PMML4Result();
pmml4Result.setResultCode(resultCode.getName());
retrieved = evaluator.getStep(segmentMock, pmml4Result);
assertNotNull(retrieved);
assertTrue(retrieved instanceof PMMLMiningModelStep);
retrievedInfo = retrieved.getInfo();
assertNotNull(retrievedInfo);
assertEquals(segmentName, retrievedInfo.get("SEGMENT"));
assertEquals(modelName, retrievedInfo.get("MODEL"));
assertEquals(resultCode.getName(), retrievedInfo.get("RESULT CODE"));
assertFalse(retrievedInfo.containsKey("RESULT"));
}
use of org.kie.pmml.api.enums.ResultCode in project drools by kiegroup.
the class KiePMMLDescrRhsFactoryTest method declareIfThenWithResult.
@Test
public void declareIfThenWithResult() {
String name = "NAME";
String statusToSet = "STATUS_TO_SET";
String result = "RESULT";
ResultCode resultCode = ResultCode.OK;
KiePMMLDroolsRule rule = KiePMMLDroolsRule.builder(name, statusToSet, Collections.emptyList()).withResultCode(resultCode).withResult(result).build();
KiePMMLDescrRhsFactory.factory(ruleBuilder).declareIfThen(rule);
assertNotNull(ruleBuilder.getDescr().getConsequence());
String expectedConsequence = String.format(UPDATE_STATUS_HOLDER_STATUS, statusToSet);
assertEquals(expectedConsequence, ruleBuilder.getDescr().getConsequence());
assertNotNull(ruleBuilder.getDescr().getNamedConsequences());
assertEquals(1, ruleBuilder.getDescr().getNamedConsequences().size());
assertNotNull(ruleBuilder.getDescr().getNamedConsequences().get(BREAK_LABEL));
String retrievedConsequence = ruleBuilder.getDescr().getNamedConsequences().get(BREAK_LABEL).toString();
assertTrue(retrievedConsequence.contains(String.format(UPDATE_STATUS_HOLDER_STATUS, DONE)));
assertTrue(retrievedConsequence.contains(String.format(SET_PMML4_RESULT_CODE, resultCode)));
assertTrue(retrievedConsequence.contains(String.format(ADD_PMML4_RESULT_VARIABLE, result)));
}
use of org.kie.pmml.api.enums.ResultCode in project drools by kiegroup.
the class KiePMMLDescrRhsFactoryTest method declareDefaultThenWithResult.
@Test
public void declareDefaultThenWithResult() {
String name = "NAME";
String statusToSet = "STATUS_TO_SET";
String result = "RESULT";
ResultCode resultCode = ResultCode.OK;
KiePMMLDroolsRule rule = KiePMMLDroolsRule.builder(name, statusToSet, Collections.emptyList()).withResultCode(resultCode).withResult(result).build();
KiePMMLDescrRhsFactory.factory(ruleBuilder).declareDefaultThen(rule);
assertNotNull(ruleBuilder.getDescr().getConsequence());
String retrievedConsequence = ruleBuilder.getDescr().getConsequence().toString();
assertTrue(retrievedConsequence.contains(String.format(UPDATE_STATUS_HOLDER_STATUS, statusToSet)));
assertTrue(retrievedConsequence.contains(String.format(SET_PMML4_RESULT_CODE, resultCode)));
assertTrue(retrievedConsequence.contains(String.format(ADD_PMML4_RESULT_VARIABLE, result)));
assertNotNull(ruleBuilder.getDescr().getNamedConsequences());
assertTrue(ruleBuilder.getDescr().getNamedConsequences().isEmpty());
}
Aggregations