use of org.dmg.pmml.SimplePredicate in project drools by kiegroup.
the class KiePMMLCompoundPredicateASTFactoryTest method declareRuleFromCompoundPredicateAndOrXorFinalLeaf.
@Test
public void declareRuleFromCompoundPredicateAndOrXorFinalLeaf() {
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";
for (CompoundPredicate.BooleanOperator operator : CompoundPredicate.BooleanOperator.values()) {
if (operator.equals(CompoundPredicate.BooleanOperator.SURROGATE)) {
continue;
}
CompoundPredicate compoundPredicate = new CompoundPredicate();
compoundPredicate.setBooleanOperator(operator);
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);
assertEquals(1, rules.size());
final KiePMMLDroolsRule retrieved = rules.get(0);
assertNotNull(retrieved);
assertEquals(currentRule, retrieved.getName());
assertEquals(DONE, retrieved.getStatusToSet());
assertEquals(String.format(STATUS_PATTERN, parentPath), retrieved.getStatusConstraint());
assertEquals(result, retrieved.getResult());
assertEquals(ResultCode.OK, retrieved.getResultCode());
switch(compoundPredicate.getBooleanOperator()) {
case AND:
assertNotNull(retrieved.getAndConstraints());
break;
case OR:
assertNotNull(retrieved.getOrConstraints());
break;
case XOR:
assertNotNull(retrieved.getXorConstraints());
break;
default:
}
}
}
use of org.dmg.pmml.SimplePredicate in project drools by kiegroup.
the class KiePMMLCompoundPredicateWithResultASTFactory method declareRuleFromCompoundPredicateSurrogate.
/**
* Method to be invoked when <b>compoundPredicate.getBooleanOperator()</b> is <code>SURROGATE</code>.
* Throws exception otherwise
* @param predicateASTFactoryData
* @param agendaActivationGroup
* @param result
* @param isFinalLeaf
*/
public static void declareRuleFromCompoundPredicateSurrogate(final PredicateASTFactoryData predicateASTFactoryData, final String agendaActivationGroup, final Object result, boolean isFinalLeaf) {
logger.trace("declareRuleFromCompoundPredicateSurrogate {} {} {} {}", predicateASTFactoryData, agendaActivationGroup, result, isFinalLeaf);
// 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, result, isFinalLeaf);
});
}
use of org.dmg.pmml.SimplePredicate 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.SimplePredicate in project drools by kiegroup.
the class KiePMMLASTFactoryUtilsTest method commonVerifyKiePMMLFieldOperatorValue.
private void commonVerifyKiePMMLFieldOperatorValue(final KiePMMLFieldOperatorValue toVerify) {
final SimplePredicate simplePredicate = getSimplePredicate(toVerify.getName());
commonVerifyKiePMMLFieldOperatorValue(toVerify, simplePredicate);
}
use of org.dmg.pmml.SimplePredicate in project drools by kiegroup.
the class KiePMMLCharacteristicFactoryTest method getAttributeVariableDeclarationWithComplexPartialScore.
@Test
public void getAttributeVariableDeclarationWithComplexPartialScore() throws IOException {
final String variableName = "variableName";
Array.Type arrayType = Array.Type.STRING;
List<String> values1 = getStringObjects(arrayType, 4);
Attribute attribute1 = getAttribute(values1, 1);
List<String> values2 = getStringObjects(arrayType, 4);
Attribute attribute2 = getAttribute(values2, 2);
CompoundPredicate compoundPredicate1 = (CompoundPredicate) attribute1.getPredicate();
CompoundPredicate compoundPredicate2 = (CompoundPredicate) attribute2.getPredicate();
DataDictionary dataDictionary = new DataDictionary();
for (Predicate predicate : compoundPredicate1.getPredicates()) {
DataField toAdd = null;
if (predicate instanceof SimplePredicate) {
toAdd = new DataField();
toAdd.setName(((SimplePredicate) predicate).getField());
toAdd.setDataType(DataType.DOUBLE);
} else if (predicate instanceof SimpleSetPredicate) {
toAdd = new DataField();
toAdd.setName(((SimpleSetPredicate) predicate).getField());
toAdd.setDataType(DataType.DOUBLE);
}
if (toAdd != null) {
dataDictionary.addDataFields(toAdd);
}
}
for (Predicate predicate : compoundPredicate2.getPredicates()) {
DataField toAdd = null;
if (predicate instanceof SimplePredicate) {
toAdd = new DataField();
toAdd.setName(((SimplePredicate) predicate).getField());
toAdd.setDataType(DataType.DOUBLE);
} else if (predicate instanceof SimpleSetPredicate) {
toAdd = new DataField();
toAdd.setName(((SimpleSetPredicate) predicate).getField());
toAdd.setDataType(DataType.DOUBLE);
}
if (toAdd != null) {
dataDictionary.addDataFields(toAdd);
}
}
String valuesString1 = values1.stream().map(valueString -> "\"" + valueString + "\"").collect(Collectors.joining(","));
String valuesString2 = values2.stream().map(valueString -> "\"" + valueString + "\"").collect(Collectors.joining(","));
Characteristic characteristic = new Characteristic();
characteristic.addAttributes(attribute1, attribute2);
characteristic.setBaselineScore(22);
characteristic.setReasonCode(REASON_CODE);
BlockStmt retrieved = KiePMMLCharacteristicFactory.getCharacteristicVariableDeclaration(variableName, characteristic, getFieldsFromDataDictionary(dataDictionary));
String text = getFileContent(TEST_01_SOURCE);
Statement expected = JavaParserUtils.parseBlock(String.format(text, variableName, valuesString1, valuesString2, characteristic.getBaselineScore(), characteristic.getReasonCode()));
assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
List<Class<?>> imports = Arrays.asList(KiePMMLAttribute.class, KiePMMLCharacteristic.class, KiePMMLComplexPartialScore.class, KiePMMLCompoundPredicate.class, KiePMMLConstant.class, KiePMMLSimplePredicate.class, KiePMMLSimpleSetPredicate.class, Arrays.class, Collections.class);
commonValidateCompilationWithImports(retrieved, imports);
}
Aggregations