PMMLMiningModelEvaluator implements PMMLModelEvaluator<KiePMMLMiningModel> { PMML4Result getPMML4Result(final KiePMMLMiningModel toEvaluate, final LinkedHashMap<String, KiePMMLNameValue> inputData) { final MULTIPLE_MODEL_METHOD multipleModelMethod = toEvaluate.getSegmentation().getMultipleModelMethod(); Object prediction = null; ResultCode resultCode = OK; try { prediction = multipleModelMethod.apply(inputData); } catch (KieEnumException e) { logger.warn(e.getMessage()); resultCode = FAIL; } PMML4Result toReturn = new PMML4Result(); toReturn.addResultVariable(toEvaluate.getTargetField(), prediction); toReturn.setResultObjectName(toEvaluate.getTargetField()); toReturn.setResultCode(resultCode.getName()); toEvaluate.getOutputFieldsMap().forEach(toReturn::addResultVariable); return toReturn; }  @Override PMML_MODEL getPMMLModelType(); @Override PMML4Result evaluate(final KieBase knowledgeBase,
                                final KiePMMLMiningModel model,
                                final PMMLContext pmmlContext);  }
@Test public void getPMML4ResultOK() { String name = "NAME"; String targetField = "TARGET"; String prediction = "FIRST_VALUE"; final Map<String, Object> outputFieldsMap = new HashMap<>(); IntStream.range(0,3).forEach(index -> { outputFieldsMap.put("KEY_" + index, "OBJECT_" + index); }); KiePMMLSegmentation kiePMMLSegmentation = KiePMMLSegmentation.builder("SEGM_1", Collections.emptyList(), SELECT_FIRST).build(); KiePMMLMiningModel kiePMMLMiningModel = KiePMMLMiningModel.builder(name, Collections.emptyList(), MINING_FUNCTION.ASSOCIATION_RULES) .withTargetField(targetField) .withSegmentation(kiePMMLSegmentation) .withOutputFieldsMap(outputFieldsMap) .build(); final LinkedHashMap<String, KiePMMLNameValue> inputData = new LinkedHashMap<>(); inputData.put("FIRST_KEY", new KiePMMLNameValue("FIRST_NAME", prediction)); inputData.put("SECOND_KEY", new KiePMMLNameValue("SECOND_NAME", "SECOND_VALUE")); PMML4Result retrieved = evaluator.getPMML4Result(kiePMMLMiningModel, inputData); assertNotNull(retrieved); assertEquals(OK.getName(), retrieved.getResultCode()); assertEquals(targetField, retrieved.getResultObjectName()); final Map<String, Object> resultVariables = retrieved.getResultVariables(); assertTrue(resultVariables.containsKey(targetField)); assertEquals(prediction, resultVariables.get(targetField)); outputFieldsMap.forEach((key, value) -> { assertTrue(resultVariables.containsKey(key)); assertEquals(value, resultVariables.get(key)); }); }
@Test public void getPMML4ResultFAIL() { String name = "NAME"; String targetField = "TARGET"; final Map<String, Object> outputFieldsMap = new HashMap<>(); IntStream.range(0,3).forEach(index -> { outputFieldsMap.put("KEY_" + index, "OBJECT_" + index); }); KiePMMLSegmentation kiePMMLSegmentation = KiePMMLSegmentation.builder("SEGM_1", Collections.emptyList(), AVERAGE).build(); KiePMMLMiningModel kiePMMLMiningModel = KiePMMLMiningModel.builder(name, Collections.emptyList(), MINING_FUNCTION.ASSOCIATION_RULES) .withTargetField(targetField) .withSegmentation(kiePMMLSegmentation) .withOutputFieldsMap(outputFieldsMap) .build(); final LinkedHashMap<String, KiePMMLNameValue> inputData = new LinkedHashMap<>(); inputData.put("FIRST_KEY", new KiePMMLNameValue("FIRST_NAME", "FIRST_VALUE")); inputData.put("SECOND_KEY", new KiePMMLNameValue("SECOND_NAME", "SECOND_VALUE")); PMML4Result retrieved = evaluator.getPMML4Result(kiePMMLMiningModel, inputData); assertNotNull(retrieved); assertEquals(FAIL.getName(), retrieved.getResultCode()); assertEquals(targetField, retrieved.getResultObjectName()); final Map<String, Object> resultVariables = retrieved.getResultVariables(); assertTrue(resultVariables.containsKey(targetField)); assertNull(resultVariables.get(targetField)); outputFieldsMap.forEach((key, value) -> { assertTrue(resultVariables.containsKey(key)); assertEquals(value, resultVariables.get(key)); }); }