use of org.dmg.pmml.CompoundPredicate 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);
}
use of org.dmg.pmml.CompoundPredicate in project drools by kiegroup.
the class KiePMMLAttributeFactoryTest method getCompoundPredicate.
private CompoundPredicate getCompoundPredicate(List<String> values, Array.Type arrayType) {
SimplePredicate simplePredicate1 = getSimplePredicate(PARAM_1, value1, operator1);
SimplePredicate simplePredicate2 = getSimplePredicate(PARAM_2, value2, operator2);
SimpleSetPredicate simpleSetPredicate = getSimpleSetPredicate(values, arrayType, SimpleSetPredicate.BooleanOperator.IS_IN);
CompoundPredicate toReturn = new CompoundPredicate();
toReturn.setBooleanOperator(CompoundPredicate.BooleanOperator.AND);
toReturn.getPredicates().add(0, simplePredicate1);
toReturn.getPredicates().add(1, simplePredicate2);
toReturn.getPredicates().add(2, simpleSetPredicate);
return toReturn;
}
use of org.dmg.pmml.CompoundPredicate in project drools by kiegroup.
the class PMMLModelTestUtils method getRandomCompoundPredicate.
public static CompoundPredicate getRandomCompoundPredicate(List<Field<?>> fields) {
CompoundPredicate toReturn = new CompoundPredicate();
toReturn.setBooleanOperator(getRandomCompoundPredicateAndOrOperator(new Random().nextInt(10)));
IntStream.range(0, fields.size() - 1).forEach(i -> {
toReturn.addPredicates(getRandomSimplePredicate((DataField) fields.get(i)));
});
toReturn.addPredicates(getRandomSimpleSetPredicate((DataField) fields.get(fields.size() - 1)));
return toReturn;
}
use of org.dmg.pmml.CompoundPredicate in project drools by kiegroup.
the class PMMLModelTestUtils method getRandomCompoundPredicate.
public static CompoundPredicate getRandomCompoundPredicate() {
CompoundPredicate toReturn = new CompoundPredicate();
toReturn.setBooleanOperator(getRandomCompoundPredicateAndOrOperator(new Random().nextInt(10)));
IntStream.range(0, 3).forEach(i -> {
toReturn.addPredicates(getRandomSimplePredicate());
});
toReturn.addPredicates(getRandomSimpleSetPredicate());
return toReturn;
}
use of org.dmg.pmml.CompoundPredicate 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);
});
}
Aggregations