use of org.dmg.pmml.SimplePredicate in project drools by kiegroup.
the class KiePMMLSimplePredicateASTFactory method getBuilderForSimplePredicate.
/**
* This method will create a <b>rule</b> that, in the RHS,
* 1) update the status (used for flowing between rules)
* 2) add <i>outputfields</i> to result variables
* 3) eventually set the value to accumulate
* <p>
* rule "_ResidenceStateScore_1"
* when
* $statusHolder : KiePMMLStatusHolder( status == "_ResidenceStateScore" )
* <p>
* RESIDENCESTATE( value == "KN" )
* then
* <p>
* $statusHolder.setStatus("_ResidenceStateScore_1");
* $statusHolder.accumulate("10.0");
* update($statusHolder);
* <p>
* end
* <p>
* end
* @param statusToSet
* @return
*/
protected KiePMMLDroolsRule.Builder getBuilderForSimplePredicate(final String statusToSet) {
logger.trace("getBuilderForSimplePredicate {}", statusToSet);
String statusConstraint = StringUtils.isEmpty(predicateASTFactoryData.getParentPath()) ? KiePMMLAbstractModelASTFactory.STATUS_NULL : String.format(STATUS_PATTERN, predicateASTFactoryData.getParentPath());
String key = predicateASTFactoryData.getFieldTypeMap().get(((SimplePredicate) predicateASTFactoryData.getPredicate()).getField().getValue()).getGeneratedType();
OPERATOR operator = OPERATOR.byName(((SimplePredicate) predicateASTFactoryData.getPredicate()).getOperator().value());
Object value = KiePMMLASTFactoryUtils.getCorrectlyFormattedObject(((SimplePredicate) predicateASTFactoryData.getPredicate()), predicateASTFactoryData.getFieldTypeMap());
List<KiePMMLFieldOperatorValue> andConstraints = Collections.singletonList(new KiePMMLFieldOperatorValue(key, BOOLEAN_OPERATOR.AND, Collections.singletonList(new KiePMMLOperatorValue(operator, value)), null));
return KiePMMLDroolsRule.builder(predicateASTFactoryData.getCurrentRule(), statusToSet, predicateASTFactoryData.getOutputFields()).withStatusConstraint(statusConstraint).withAndConstraints(andConstraints);
}
use of org.dmg.pmml.SimplePredicate in project drools by kiegroup.
the class KiePMMLCharacteristicFactoryTest 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.SimplePredicate in project drools by kiegroup.
the class KiePMMLAttributeFactoryTest method getAttributeVariableDeclarationWithComplexPartialScore.
@Test
public void getAttributeVariableDeclarationWithComplexPartialScore() throws IOException {
final String variableName = "variableName";
Attribute attribute = new Attribute();
attribute.setReasonCode(REASON_CODE);
Array.Type arrayType = Array.Type.STRING;
List<String> values = getStringObjects(arrayType, 4);
CompoundPredicate compoundPredicate = getCompoundPredicate(values, arrayType);
attribute.setPredicate(compoundPredicate);
attribute.setComplexPartialScore(getComplexPartialScore());
String valuesString = values.stream().map(valueString -> "\"" + valueString + "\"").collect(Collectors.joining(","));
DataDictionary dataDictionary = new DataDictionary();
for (Predicate predicate : compoundPredicate.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);
}
}
BlockStmt retrieved = KiePMMLAttributeFactory.getAttributeVariableDeclaration(variableName, attribute, getFieldsFromDataDictionary(dataDictionary));
String text = getFileContent(TEST_01_SOURCE);
Statement expected = JavaParserUtils.parseBlock(String.format(text, variableName, valuesString));
assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
List<Class<?>> imports = Arrays.asList(KiePMMLAttribute.class, KiePMMLComplexPartialScore.class, KiePMMLCompoundPredicate.class, KiePMMLConstant.class, KiePMMLSimplePredicate.class, KiePMMLSimpleSetPredicate.class, Arrays.class, Collections.class);
commonValidateCompilationWithImports(retrieved, imports);
}
use of org.dmg.pmml.SimplePredicate 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.SimplePredicate in project drools by kiegroup.
the class KiePMMLPredicateInstanceFactoryTest method getKiePMMLPredicate.
@Test
public void getKiePMMLPredicate() {
List<Field<?>> fields = IntStream.range(0, 3).mapToObj(i -> getRandomDataField()).collect(Collectors.toList());
SimplePredicate simplePredicate1 = getRandomSimplePredicate((DataField) fields.get(0));
KiePMMLPredicate retrieved = KiePMMLPredicateInstanceFactory.getKiePMMLPredicate(simplePredicate1, fields);
commonVerifyKiePMMLPredicate(retrieved, simplePredicate1);
SimpleSetPredicate simpleSetPredicate = getRandomSimpleSetPredicate((DataField) fields.get(2));
retrieved = KiePMMLPredicateInstanceFactory.getKiePMMLPredicate(simpleSetPredicate, fields);
commonVerifyKiePMMLPredicate(retrieved, simpleSetPredicate);
final CompoundPredicate compoundPredicate = getRandomCompoundPredicate(fields);
retrieved = KiePMMLPredicateInstanceFactory.getKiePMMLPredicate(compoundPredicate, fields);
commonVerifyKiePMMLPredicate(retrieved, compoundPredicate);
False falsePredicate = new False();
retrieved = KiePMMLPredicateInstanceFactory.getKiePMMLPredicate(falsePredicate, fields);
commonVerifyKiePMMLPredicate(retrieved, falsePredicate);
True truePredicate = new True();
retrieved = KiePMMLPredicateInstanceFactory.getKiePMMLPredicate(truePredicate, fields);
commonVerifyKiePMMLPredicate(retrieved, truePredicate);
}
Aggregations