Search in sources :

Example 1 with KiePMMLReasonCodeAndValue

use of org.kie.pmml.models.drools.tuples.KiePMMLReasonCodeAndValue in project drools by kiegroup.

the class KiePMMLScorecardModelCharacteristicASTFactoryTest method declareRuleFromAttributeWithSimplePredicateUseReasonCodesTrue.

@Test
public void declareRuleFromAttributeWithSimplePredicateUseReasonCodesTrue() {
    Attribute attribute = getSimplePredicateAttribute();
    final String parentPath = "parent_path";
    final int attributeIndex = 2;
    final List<KiePMMLDroolsRule> rules = new ArrayList<>();
    final String statusToSet = "status_to_set";
    final String characteristicReasonCode = "REASON_CODE";
    final double characteristicBaselineScore = 12;
    final boolean isLastCharacteristic = false;
    getKiePMMLScorecardModelCharacteristicASTFactory().withReasonCodes(null, REASONCODE_ALGORITHM.POINTS_ABOVE).declareRuleFromAttribute(attribute, parentPath, attributeIndex, rules, statusToSet, characteristicReasonCode, characteristicBaselineScore, isLastCharacteristic);
    assertEquals(1, rules.size());
    KiePMMLDroolsRule toValidate = rules.get(0);
    commonValidateRule(toValidate, attribute, statusToSet, parentPath, attributeIndex, isLastCharacteristic, 1, null, BOOLEAN_OPERATOR.AND, "value <= 5.0", 1);
    KiePMMLReasonCodeAndValue retrieved = toValidate.getReasonCodeAndValue();
    assertNotNull(retrieved);
    assertEquals(characteristicReasonCode, retrieved.getReasonCode());
    double expected = attribute.getPartialScore().doubleValue() - characteristicBaselineScore;
    assertEquals(expected, retrieved.getValue(), 0);
}
Also used : Attribute(org.dmg.pmml.scorecard.Attribute) ArrayList(java.util.ArrayList) KiePMMLReasonCodeAndValue(org.kie.pmml.models.drools.tuples.KiePMMLReasonCodeAndValue) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) Test(org.junit.Test)

Example 2 with KiePMMLReasonCodeAndValue

use of org.kie.pmml.models.drools.tuples.KiePMMLReasonCodeAndValue in project drools by kiegroup.

the class KiePMMLScorecardModelCharacteristicASTFactory method declareRuleFromAttribute.

protected void declareRuleFromAttribute(final Attribute attribute, final String parentPath, final int attributeIndex, final List<KiePMMLDroolsRule> rules, final String statusToSet, final String characteristicReasonCode, final Number characteristicBaselineScore, final boolean isLastCharacteristic) {
    logger.trace("declareRuleFromAttribute {} {}", attribute, parentPath);
    final Predicate predicate = attribute.getPredicate();
    // an XOR compound predicate
    if (predicate instanceof False) {
        return;
    }
    String currentRule = String.format(PATH_PATTERN, parentPath, attributeIndex);
    KiePMMLReasonCodeAndValue reasonCodeAndValue = getKiePMMLReasonCodeAndValue(attribute, characteristicReasonCode, characteristicBaselineScore);
    PredicateASTFactoryData predicateASTFactoryData = new PredicateASTFactoryData(predicate, outputFields, rules, parentPath, currentRule, fieldTypeMap);
    KiePMMLPredicateASTFactory.factory(predicateASTFactoryData).declareRuleFromPredicate(attribute.getPartialScore(), statusToSet, reasonCodeAndValue, isLastCharacteristic);
}
Also used : KiePMMLReasonCodeAndValue(org.kie.pmml.models.drools.tuples.KiePMMLReasonCodeAndValue) False(org.dmg.pmml.False) PredicateASTFactoryData(org.kie.pmml.models.drools.ast.factories.PredicateASTFactoryData) Predicate(org.dmg.pmml.Predicate)

Example 3 with KiePMMLReasonCodeAndValue

use of org.kie.pmml.models.drools.tuples.KiePMMLReasonCodeAndValue in project drools by kiegroup.

the class KiePMMLScorecardModelCharacteristicASTFactoryTest method getKiePMMLReasonCodeAndValueUseReasonCodesTrue.

@Test
public void getKiePMMLReasonCodeAndValueUseReasonCodesTrue() {
    String characteristicReasonCode = "CHARACTERISTIC_REASON_CODE";
    String attributeReasonCode = "ATTRIBUTE_REASON_CODE";
    double baselineScore = 13;
    double characteristicBaselineScore = 24.45;
    double attributePartialScore = 13.17;
    Attribute attribute = new Attribute();
    attribute.setPartialScore(attributePartialScore);
    KiePMMLReasonCodeAndValue retrieved = getKiePMMLScorecardModelCharacteristicASTFactory().withReasonCodes(baselineScore, REASONCODE_ALGORITHM.POINTS_ABOVE).getKiePMMLReasonCodeAndValue(attribute, characteristicReasonCode, null);
    assertNotNull(retrieved);
    assertEquals(characteristicReasonCode, retrieved.getReasonCode());
    double expected = attributePartialScore - baselineScore;
    assertEquals(expected, retrieved.getValue(), 0);
    retrieved = getKiePMMLScorecardModelCharacteristicASTFactory().withReasonCodes(baselineScore, REASONCODE_ALGORITHM.POINTS_ABOVE).getKiePMMLReasonCodeAndValue(attribute, characteristicReasonCode, characteristicBaselineScore);
    assertNotNull(retrieved);
    assertEquals(characteristicReasonCode, retrieved.getReasonCode());
    expected = attributePartialScore - characteristicBaselineScore;
    assertEquals(expected, retrieved.getValue(), 0);
    attribute.setReasonCode(attributeReasonCode);
    retrieved = getKiePMMLScorecardModelCharacteristicASTFactory().withReasonCodes(baselineScore, REASONCODE_ALGORITHM.POINTS_ABOVE).getKiePMMLReasonCodeAndValue(attribute, characteristicReasonCode, characteristicBaselineScore);
    assertNotNull(retrieved);
    assertEquals(attributeReasonCode, retrieved.getReasonCode());
    assertEquals(expected, retrieved.getValue(), 0);
}
Also used : Attribute(org.dmg.pmml.scorecard.Attribute) KiePMMLReasonCodeAndValue(org.kie.pmml.models.drools.tuples.KiePMMLReasonCodeAndValue) Test(org.junit.Test)

Example 4 with KiePMMLReasonCodeAndValue

use of org.kie.pmml.models.drools.tuples.KiePMMLReasonCodeAndValue in project drools by kiegroup.

the class KiePMMLDescrRhsFactory method commonDeclareThen.

protected void commonDeclareThen(final KiePMMLDroolsRule rule, final StringJoiner joiner) {
    if (rule.getFocusedAgendaGroup() != null) {
        joiner.add(String.format(FOCUS_AGENDA_GROUP, rule.getFocusedAgendaGroup()));
    }
    if (rule.getToAccumulate() != null) {
        joiner.add(String.format(UPDATE_STATUS_HOLDER_ACCUMULATE, rule.getToAccumulate()));
    }
    if (rule.isAccumulationResult()) {
        joiner.add(RETURN_ACCUMULATION);
    }
    if (rule.getResultCode() != null) {
        joiner.add(String.format(SET_PMML4_RESULT_CODE, rule.getResultCode()));
    }
    if (rule.getResult() != null) {
        joiner.add(String.format(ADD_PMML4_RESULT_VARIABLE, rule.getResult()));
    }
    if (rule.getReasonCodeAndValue() != null) {
        final KiePMMLReasonCodeAndValue reasonCodeAndValue = rule.getReasonCodeAndValue();
        joiner.add(String.format(ADD_OUTPUTFIELD_VALUE, reasonCodeAndValue.getReasonCode(), reasonCodeAndValue.getValue()));
    }
    if (rule.getOutputFields() != null) {
        if (rule.getResult() != null) {
            commonDeclareOutputFields(rule.getOutputFields(), rule.getResult(), joiner);
        } else if (rule.isAccumulationResult()) {
            commonDeclareOutputFields(rule.getOutputFields(), "$statusHolder.getAccumulator()", joiner);
        }
    }
}
Also used : KiePMMLReasonCodeAndValue(org.kie.pmml.models.drools.tuples.KiePMMLReasonCodeAndValue)

Example 5 with KiePMMLReasonCodeAndValue

use of org.kie.pmml.models.drools.tuples.KiePMMLReasonCodeAndValue in project drools by kiegroup.

the class KiePMMLCompoundPredicateWithAccumulationASTFactory method declareRuleFromCompoundPredicateSurrogate.

/**
 * Method to be invoked when <b>compoundPredicate.getBooleanOperator()</b> is <code>SURROGATE</code>.
 * Throws exception otherwise
 * @param predicateASTFactoryData
 * @param agendaActivationGroup
 * @param toAccumulate
 * @param statusToSet
 * @param reasonCodeAndValue
 * @param isLastCharacteristic
 */
public static void declareRuleFromCompoundPredicateSurrogate(final PredicateASTFactoryData predicateASTFactoryData, final String agendaActivationGroup, final Number toAccumulate, final String statusToSet, final KiePMMLReasonCodeAndValue reasonCodeAndValue, final boolean isLastCharacteristic) {
    logger.trace("declareRuleFromCompoundPredicateSurrogate {} {} {} {} {}", predicateASTFactoryData, agendaActivationGroup, toAccumulate, statusToSet, isLastCharacteristic);
    // Managing only SimplePredicates for the moment being
    CompoundPredicate compoundPredicate = (CompoundPredicate) predicateASTFactoryData.getPredicate();
    final List<Predicate> simplePredicates = compoundPredicate.getPredicates().stream().filter(predicate -> predicate instanceof SimplePredicate).collect(Collectors.toList());
    simplePredicates.forEach(predicate -> {
        SimplePredicate simplePredicate = (SimplePredicate) predicate;
        PredicateASTFactoryData newPredicateASTFactoryData = predicateASTFactoryData.cloneWithPredicate(simplePredicate);
        KiePMMLSimplePredicateASTFactory.factory(newPredicateASTFactoryData).declareRuleFromSimplePredicateSurrogate(agendaActivationGroup, toAccumulate, statusToSet, reasonCodeAndValue, isLastCharacteristic);
    });
}
Also used : CompoundPredicate(org.dmg.pmml.CompoundPredicate) List(java.util.List) Predicate(org.dmg.pmml.Predicate) SimplePredicate(org.dmg.pmml.SimplePredicate) Logger(org.slf4j.Logger) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) ResultCode(org.kie.pmml.api.enums.ResultCode) KiePMMLReasonCodeAndValue(org.kie.pmml.models.drools.tuples.KiePMMLReasonCodeAndValue) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) CompoundPredicate(org.dmg.pmml.CompoundPredicate) SimplePredicate(org.dmg.pmml.SimplePredicate) CompoundPredicate(org.dmg.pmml.CompoundPredicate) Predicate(org.dmg.pmml.Predicate) SimplePredicate(org.dmg.pmml.SimplePredicate)

Aggregations

KiePMMLReasonCodeAndValue (org.kie.pmml.models.drools.tuples.KiePMMLReasonCodeAndValue)5 Predicate (org.dmg.pmml.Predicate)2 Attribute (org.dmg.pmml.scorecard.Attribute)2 Test (org.junit.Test)2 KiePMMLDroolsRule (org.kie.pmml.models.drools.ast.KiePMMLDroolsRule)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 CompoundPredicate (org.dmg.pmml.CompoundPredicate)1 False (org.dmg.pmml.False)1 SimplePredicate (org.dmg.pmml.SimplePredicate)1 ResultCode (org.kie.pmml.api.enums.ResultCode)1 PredicateASTFactoryData (org.kie.pmml.models.drools.ast.factories.PredicateASTFactoryData)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1