use of org.dmg.pmml.CompoundPredicate in project drools by kiegroup.
the class KiePMMLCompoundPredicateASTFactory method declareRuleFromCompoundPredicate.
/**
* @param result
* @param isFinalLeaf
*/
public void declareRuleFromCompoundPredicate(final Object result, final boolean isFinalLeaf) {
logger.trace("declareRuleFromCompoundPredicate {} {}", result, isFinalLeaf);
CompoundPredicate compoundPredicate = (CompoundPredicate) predicateASTFactoryData.getPredicate();
switch(compoundPredicate.getBooleanOperator()) {
case SURROGATE:
final String agendaActivationGroup = String.format(KiePMMLAbstractModelASTFactory.SURROGATE_GROUP_PATTERN, predicateASTFactoryData.getCurrentRule());
declareRuleFromCompoundPredicateSurrogate(agendaActivationGroup, null);
KiePMMLCompoundPredicateWithResultASTFactory.declareRuleFromCompoundPredicateSurrogate(predicateASTFactoryData, agendaActivationGroup, result, isFinalLeaf);
break;
case AND:
declareRuleFromCompoundPredicateAndOrXor(result, isFinalLeaf);
break;
case OR:
declareRuleFromCompoundPredicateAndOrXor(result, isFinalLeaf);
break;
case XOR:
declareRuleFromCompoundPredicateAndOrXor(result, isFinalLeaf);
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 KiePMMLASTFactoryUtilsTest method populateKiePMMLFieldOperatorValueListWithCompoundPredicates.
@Test
public void populateKiePMMLFieldOperatorValueListWithCompoundPredicates() {
final List<KiePMMLFieldOperatorValue> toPopulate = new ArrayList<>();
KiePMMLASTFactoryUtils.populateKiePMMLFieldOperatorValueListWithCompoundPredicates(toPopulate, compoundPredicates, fieldTypeMap);
assertFalse(toPopulate.isEmpty());
// one entry is for "AND" compounds and the other is for "OR" ones
assertEquals(2, toPopulate.size());
final Map<CompoundPredicate.BooleanOperator, List<CompoundPredicate>> partitionedCompoundPredicates = compoundPredicates.stream().collect(Collectors.groupingBy(CompoundPredicate::getBooleanOperator));
partitionedCompoundPredicates.forEach((booleanOperator, compoundPredicates) -> {
final KiePMMLFieldOperatorValue operatorValue = toPopulate.stream().filter(kiePMMLFieldOperatorValue -> kiePMMLFieldOperatorValue.getOperator().equals(BOOLEAN_OPERATOR.byName(booleanOperator.value()))).findFirst().orElseThrow(() -> new RuntimeException("Failed toRetrieve KiePMMLFieldOperatorValue for " + "BooleanOperator " + booleanOperator));
final List<KiePMMLFieldOperatorValue> nestedKiePMMLFieldOperatorValues = operatorValue.getNestedKiePMMLFieldOperatorValues();
final List<Predicate> nestedPredicates = compoundPredicates.stream().flatMap(compoundPredicate -> compoundPredicate.getPredicates().stream()).collect(Collectors.toList());
assertEquals(nestedPredicates.size(), nestedKiePMMLFieldOperatorValues.size());
nestedKiePMMLFieldOperatorValues.forEach(new Consumer<KiePMMLFieldOperatorValue>() {
@Override
public void accept(KiePMMLFieldOperatorValue kiePMMLFieldOperatorValue) {
assertEquals(1, kiePMMLFieldOperatorValue.getKiePMMLOperatorValues().size());
final KiePMMLOperatorValue kiePMMLOperatorValue = kiePMMLFieldOperatorValue.getKiePMMLOperatorValues().get(0);
SimplePredicate simplePredicate = nestedPredicates.stream().map(predicate -> (SimplePredicate) predicate).filter(predicate -> predicate.getField().getValue().equals(getOriginalPredicateName(kiePMMLFieldOperatorValue.getName()))).findFirst().orElseThrow(() -> new RuntimeException("Failed to find SimplePredicate for " + kiePMMLFieldOperatorValue.getName()));
commonVerifyKiePMMLOperatorValue(kiePMMLOperatorValue, simplePredicate);
nestedPredicates.remove(simplePredicate);
}
});
assertTrue(nestedPredicates.isEmpty());
});
}
use of org.dmg.pmml.CompoundPredicate in project drools by kiegroup.
the class KiePMMLASTFactoryUtilsTest method getConstraintEntriesFromXOrCompoundPredicateWrongSize.
@Test(expected = KiePMMLException.class)
public void getConstraintEntriesFromXOrCompoundPredicateWrongSize() {
CompoundPredicate compoundPredicate = new CompoundPredicate();
compoundPredicate.setBooleanOperator(CompoundPredicate.BooleanOperator.XOR);
compoundPredicate.getPredicates().addAll(simplePredicates);
KiePMMLASTFactoryUtils.getConstraintEntriesFromXOrCompoundPredicate(compoundPredicate, fieldTypeMap);
}
use of org.dmg.pmml.CompoundPredicate in project drools by kiegroup.
the class KiePMMLASTFactoryUtilsTest method getConstraintEntriesFromXOrCompoundPredicateWrongOperator.
@Test(expected = KiePMMLException.class)
public void getConstraintEntriesFromXOrCompoundPredicateWrongOperator() {
CompoundPredicate compoundPredicate = new CompoundPredicate();
compoundPredicate.setBooleanOperator(CompoundPredicate.BooleanOperator.AND);
KiePMMLASTFactoryUtils.getConstraintEntriesFromXOrCompoundPredicate(compoundPredicate, fieldTypeMap);
}
use of org.dmg.pmml.CompoundPredicate in project drools by kiegroup.
the class KiePMMLCompoundPredicateASTFactoryTest method declareRuleFromCompoundPredicateSurrogateNotFinalLeaf.
@Test
public void declareRuleFromCompoundPredicateSurrogateNotFinalLeaf() {
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, false);
// 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 not in a final leaf node
if (isTrueRule) {
assertEquals(currentRule, retrieved.getStatusToSet());
assertNull(retrieved.getResult());
assertNull(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