Search in sources :

Example 1 with ResultCode

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));
// 
}
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 2 with ResultCode

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;
}
Also used : PMML4Result(org.kie.api.pmml.PMML4Result) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) KieEnumException(org.kie.pmml.api.exceptions.KieEnumException) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) ResultCode(org.kie.pmml.api.enums.ResultCode) MULTIPLE_MODEL_METHOD(org.kie.pmml.models.mining.model.enums.MULTIPLE_MODEL_METHOD)

Example 3 with ResultCode

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"));
}
Also used : PMML4Result(org.kie.api.pmml.PMML4Result) PMMLStep(org.kie.pmml.api.models.PMMLStep) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) KiePMMLSegment(org.kie.pmml.models.mining.model.segmentation.KiePMMLSegment) ResultCode(org.kie.pmml.api.enums.ResultCode) PMMLContextTest(org.kie.pmml.commons.testingutility.PMMLContextTest) Test(org.junit.Test)

Example 4 with ResultCode

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)));
}
Also used : KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) ResultCode(org.kie.pmml.api.enums.ResultCode) Test(org.junit.Test)

Example 5 with ResultCode

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());
}
Also used : KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) ResultCode(org.kie.pmml.api.enums.ResultCode) Test(org.junit.Test)

Aggregations

ResultCode (org.kie.pmml.api.enums.ResultCode)5 Test (org.junit.Test)4 KiePMMLDroolsRule (org.kie.pmml.models.drools.ast.KiePMMLDroolsRule)3 PMML4Result (org.kie.api.pmml.PMML4Result)2 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 StringJoiner (java.util.StringJoiner)1 OutputField (org.dmg.pmml.OutputField)1 KieEnumException (org.kie.pmml.api.exceptions.KieEnumException)1 PMMLStep (org.kie.pmml.api.models.PMMLStep)1 KiePMMLModel (org.kie.pmml.commons.model.KiePMMLModel)1 KiePMMLNameValue (org.kie.pmml.commons.model.tuples.KiePMMLNameValue)1 PMMLContextTest (org.kie.pmml.commons.testingutility.PMMLContextTest)1 MULTIPLE_MODEL_METHOD (org.kie.pmml.models.mining.model.enums.MULTIPLE_MODEL_METHOD)1 KiePMMLSegment (org.kie.pmml.models.mining.model.segmentation.KiePMMLSegment)1