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