use of org.dmg.pmml.CompoundPredicate in project drools by kiegroup.
the class KiePMMLCompoundPredicateASTFactory method declareRuleFromCompoundPredicate.
/**
* @param toAccumulate
* @param statusToSet
* @param reasonCodeAndValue
* @param isLastCharacteristic
*/
public void declareRuleFromCompoundPredicate(final Number toAccumulate, final String statusToSet, final KiePMMLReasonCodeAndValue reasonCodeAndValue, boolean isLastCharacteristic) {
logger.trace("declareRuleFromCompoundPredicate {} {} {}", toAccumulate, statusToSet, isLastCharacteristic);
CompoundPredicate compoundPredicate = (CompoundPredicate) predicateASTFactoryData.getPredicate();
switch(compoundPredicate.getBooleanOperator()) {
case SURROGATE:
final String agendaActivationGroup = String.format(KiePMMLAbstractModelASTFactory.SURROGATE_GROUP_PATTERN, predicateASTFactoryData.getCurrentRule());
declareRuleFromCompoundPredicateSurrogate(agendaActivationGroup, statusToSet);
KiePMMLCompoundPredicateWithAccumulationASTFactory.declareRuleFromCompoundPredicateSurrogate(predicateASTFactoryData, agendaActivationGroup, toAccumulate, statusToSet, reasonCodeAndValue, isLastCharacteristic);
break;
case AND:
declareRuleFromCompoundPredicateAndOrXor(toAccumulate, statusToSet, reasonCodeAndValue, isLastCharacteristic);
break;
case OR:
declareRuleFromCompoundPredicateAndOrXor(toAccumulate, statusToSet, reasonCodeAndValue, isLastCharacteristic);
break;
case XOR:
declareRuleFromCompoundPredicateAndOrXor(toAccumulate, statusToSet, reasonCodeAndValue, isLastCharacteristic);
break;
default:
throw new IllegalStateException(String.format("Unknown CompoundPredicate.booleanOperator %st", compoundPredicate.getBooleanOperator()));
}
}
use of org.dmg.pmml.CompoundPredicate 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);
});
}
use of org.dmg.pmml.CompoundPredicate in project drools by kiegroup.
the class KiePMMLASTFactoryUtilsTest method getConstraintEntriesFromXOrCompoundPredicate.
@Test
public void getConstraintEntriesFromXOrCompoundPredicate() {
CompoundPredicate compoundPredicate = new CompoundPredicate();
compoundPredicate.setBooleanOperator(CompoundPredicate.BooleanOperator.XOR);
List<Predicate> predicates = IntStream.range(0, 2).mapToObj(index -> simplePredicates.get(index)).collect(Collectors.toList());
compoundPredicate.getPredicates().addAll(predicates);
List<KiePMMLFieldOperatorValue> retrieved = KiePMMLASTFactoryUtils.getConstraintEntriesFromXOrCompoundPredicate(compoundPredicate, fieldTypeMap);
assertNotNull(retrieved);
assertEquals(predicates.size(), retrieved.size());
commonVerifyKiePMMLFieldOperatorValueList(retrieved, null);
}
use of org.dmg.pmml.CompoundPredicate in project drools by kiegroup.
the class KiePMMLScorecardModelCharacteristicASTFactoryTest method getCompoundPredicate.
private CompoundPredicate getCompoundPredicate() {
CompoundPredicate toReturn = new CompoundPredicate();
toReturn.setBooleanOperator(CompoundPredicate.BooleanOperator.AND);
final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
toReturn.addPredicates(getSimplePredicate(fieldName, DataType.DOUBLE, 5.0, SimplePredicate.Operator.GREATER_OR_EQUAL, fieldTypeMap), getSimplePredicate(fieldName, DataType.DOUBLE, 12.0, SimplePredicate.Operator.LESS_THAN, fieldTypeMap));
return toReturn;
}
use of org.dmg.pmml.CompoundPredicate 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());
}
}
}
Aggregations