use of org.kie.pmml.models.drools.ast.KiePMMLDroolsRule 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.models.drools.ast.KiePMMLDroolsRule 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());
}
use of org.kie.pmml.models.drools.ast.KiePMMLDroolsRule in project drools by kiegroup.
the class KiePMMLCompoundPredicateASTFactoryTest method declareRuleFromCompoundPredicateSurrogateFinalLeaf.
@Test
public void declareRuleFromCompoundPredicateSurrogateFinalLeaf() {
final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
final List<SimplePredicate> predicates = getSimplePredicates(fieldTypeMap);
String parentPath = "_will play";
String currentRule = "_will play_will play";
String result = "RESULT";
CompoundPredicate compoundPredicate = new CompoundPredicate();
compoundPredicate.setBooleanOperator(CompoundPredicate.BooleanOperator.SURROGATE);
predicates.forEach(compoundPredicate::addPredicates);
final List<KiePMMLDroolsRule> rules = new ArrayList<>();
PredicateASTFactoryData predicateASTFactoryData = getPredicateASTFactoryData(compoundPredicate, Collections.emptyList(), rules, parentPath, currentRule, fieldTypeMap);
KiePMMLCompoundPredicateASTFactory.factory(predicateASTFactoryData).declareRuleFromCompoundPredicate(result, true);
// For each "surrogate" predicate two rules -"TRUE" and "FALSE" - are generated; one more rule is generated for the Compound predicate itself
int expectedRules = (predicates.size() * 2) + 1;
assertEquals(expectedRules, rules.size());
String agendaActivationGroup = String.format(SURROGATE_GROUP_PATTERN, currentRule);
for (KiePMMLDroolsRule retrieved : rules) {
String ruleName = retrieved.getName();
if (ruleName.contains("_surrogate_")) {
String[] ruleNameParts = ruleName.split("_surrogate_");
String generatedTypePart = ruleNameParts[1];
boolean isTrueRule = generatedTypePart.endsWith("_TRUE");
final String generatedType = generatedTypePart.replace("_TRUE", "").replace("_FALSE", "");
final Optional<String> fieldName = fieldTypeMap.entrySet().stream().filter(entry -> generatedType.equals(entry.getValue().getGeneratedType())).map(Map.Entry::getKey).findFirst();
if (fieldName.isPresent()) {
SimplePredicate mappedPredicate = predicates.stream().filter(pred -> fieldName.get().equals(pred.getField().getValue())).findFirst().orElse(null);
assertNotNull(mappedPredicate);
assertNull(retrieved.getStatusConstraint());
assertEquals(agendaActivationGroup, retrieved.getActivationGroup());
assertEquals(agendaActivationGroup, retrieved.getAgendaGroup());
// Those are in a final leaf node
if (isTrueRule) {
assertEquals(DONE, retrieved.getStatusToSet());
assertEquals(result, retrieved.getResult());
assertEquals(ResultCode.OK, retrieved.getResultCode());
} else {
assertEquals(parentPath, retrieved.getStatusToSet());
assertNull(retrieved.getResult());
assertNull(retrieved.getResultCode());
}
}
} else {
assertNotNull(retrieved.getStatusConstraint());
assertEquals(String.format(STATUS_PATTERN, parentPath), retrieved.getStatusConstraint());
assertEquals(agendaActivationGroup, retrieved.getFocusedAgendaGroup());
assertNull(retrieved.getStatusToSet());
assertNull(retrieved.getResult());
assertNull(retrieved.getResultCode());
}
}
}
use of org.kie.pmml.models.drools.ast.KiePMMLDroolsRule in project drools by kiegroup.
the class KiePMMLScorecardModelCharacteristicASTFactoryTest method declareRulesFromCharacteristics.
@Test
public void declareRulesFromCharacteristics() {
Characteristics characteristics = scorecardModel.getCharacteristics();
String parentPath = "_will";
List<KiePMMLDroolsRule> retrieved = getKiePMMLScorecardModelCharacteristicASTFactory().declareRulesFromCharacteristics(characteristics, parentPath, null);
final List<Characteristic> characteristicList = characteristics.getCharacteristics();
List<Attribute> attributes = new ArrayList<>();
AtomicInteger counter = new AtomicInteger(0);
for (int i = 0; i < characteristicList.size(); i++) {
Characteristic characteristic = characteristicList.get(i);
attributes.addAll(characteristic.getAttributes());
for (int j = 0; j < characteristic.getAttributes().size(); j++) {
Attribute attribute = characteristic.getAttributes().get(j);
KiePMMLDroolsRule rule = retrieved.get(counter.incrementAndGet());
int expectedOperatorValuesSize = 1;
Integer expectedAndConstraints = null;
Integer expectedInConstraints = null;
BOOLEAN_OPERATOR expectedOperator = BOOLEAN_OPERATOR.AND;
if (attribute.getPredicate() instanceof SimplePredicate) {
expectedAndConstraints = 1;
}
if (attribute.getPredicate() instanceof CompoundPredicate) {
expectedOperatorValuesSize = ((CompoundPredicate) attribute.getPredicate()).getPredicates().size();
expectedAndConstraints = 1;
}
if (attribute.getPredicate() instanceof SimpleSetPredicate) {
expectedInConstraints = 1;
}
boolean isLastCharacteristic = (i == characteristicList.size() - 1);
String statusToSet = isLastCharacteristic ? DONE : String.format(PATH_PATTERN, parentPath, characteristicList.get(i + 1).getName());
commonValidateRule(rule, attribute, statusToSet, parentPath + "_" + characteristic.getName(), j, isLastCharacteristic, expectedAndConstraints, expectedInConstraints, expectedOperator, null, expectedOperatorValuesSize);
}
}
assertEquals(attributes.size() + 1, retrieved.size());
}
use of org.kie.pmml.models.drools.ast.KiePMMLDroolsRule in project drools by kiegroup.
the class KiePMMLScorecardModelCharacteristicASTFactoryTest method declareRuleFromAttributeWithSimplePredicateLastCharacteristic.
@Test
public void declareRuleFromAttributeWithSimplePredicateLastCharacteristic() {
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 = true;
getKiePMMLScorecardModelCharacteristicASTFactory().declareRuleFromAttribute(attribute, parentPath, attributeIndex, rules, statusToSet, characteristicReasonCode, characteristicBaselineScore, isLastCharacteristic);
assertEquals(1, rules.size());
commonValidateRule(rules.get(0), attribute, statusToSet, parentPath, attributeIndex, isLastCharacteristic, 1, null, BOOLEAN_OPERATOR.AND, "value <= 5.0", 1);
}
Aggregations