use of org.dmg.pmml.SimpleSetPredicate 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.SimpleSetPredicate in project drools by kiegroup.
the class KiePMMLSimpleSetPredicateASTFactory method getBuilderForSimpleSetPredicate.
private KiePMMLDroolsRule.Builder getBuilderForSimpleSetPredicate(final String statusToSet) {
logger.trace("declareRuleFromSimpleSetPredicate {}", statusToSet);
String statusConstraint = StringUtils.isEmpty(predicateASTFactoryData.getParentPath()) ? STATUS_NULL : String.format(STATUS_PATTERN, predicateASTFactoryData.getParentPath());
SimpleSetPredicate simpleSetPredicate = (SimpleSetPredicate) predicateASTFactoryData.getPredicate();
String key = predicateASTFactoryData.getFieldTypeMap().get(simpleSetPredicate.getField().getValue()).getGeneratedType();
String stringValue = (String) simpleSetPredicate.getArray().getValue();
String[] valuesArray = stringValue.split(" ");
List<Object> value = Arrays.stream(valuesArray).map(rawValue -> {
String originalType = predicateASTFactoryData.getFieldTypeMap().get(simpleSetPredicate.getField().getValue()).getOriginalType();
switch(originalType) {
case "string":
return "\"" + rawValue + "\"";
case "double":
return Double.valueOf(rawValue).toString();
default:
return rawValue;
}
}).collect(Collectors.toList());
Map<String, List<Object>> constraints = Collections.singletonMap(key, value);
KiePMMLDroolsRule.Builder toReturn = KiePMMLDroolsRule.builder(predicateASTFactoryData.getCurrentRule(), statusToSet, predicateASTFactoryData.getOutputFields()).withStatusConstraint(statusConstraint);
if (SimpleSetPredicate.BooleanOperator.IS_IN.equals(simpleSetPredicate.getBooleanOperator())) {
toReturn = toReturn.withInConstraints(constraints);
} else {
toReturn = toReturn.withNotInConstraints(constraints);
}
return toReturn;
}
use of org.dmg.pmml.SimpleSetPredicate 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);
}
use of org.dmg.pmml.SimpleSetPredicate in project drools by kiegroup.
the class KiePMMLCompoundPredicateFactoryTest method getCompoundPredicateVariableDeclaration.
@Test
public void getCompoundPredicateVariableDeclaration() throws IOException {
String variableName = "variableName";
SimplePredicate simplePredicate1 = getSimplePredicate(PARAM_1, value1, operator1);
SimplePredicate simplePredicate2 = getSimplePredicate(PARAM_2, value2, operator2);
Array.Type arrayType = Array.Type.STRING;
List<String> values = getStringObjects(arrayType, 4);
SimpleSetPredicate simpleSetPredicate = getSimpleSetPredicate(values, arrayType, SimpleSetPredicate.BooleanOperator.IS_IN);
CompoundPredicate compoundPredicate = new CompoundPredicate();
compoundPredicate.setBooleanOperator(CompoundPredicate.BooleanOperator.AND);
compoundPredicate.getPredicates().add(0, simplePredicate1);
compoundPredicate.getPredicates().add(1, simplePredicate2);
compoundPredicate.getPredicates().add(2, simpleSetPredicate);
DataField dataField1 = new DataField();
dataField1.setName(simplePredicate1.getField());
dataField1.setDataType(DataType.DOUBLE);
DataField dataField2 = new DataField();
dataField2.setName(simplePredicate2.getField());
dataField2.setDataType(DataType.DOUBLE);
DataField dataField3 = new DataField();
dataField3.setName(simpleSetPredicate.getField());
dataField3.setDataType(DataType.DOUBLE);
DataDictionary dataDictionary = new DataDictionary();
dataDictionary.addDataFields(dataField1, dataField2, dataField3);
String booleanOperatorString = BOOLEAN_OPERATOR.class.getName() + "." + BOOLEAN_OPERATOR.byName(compoundPredicate.getBooleanOperator().value()).name();
String valuesString = values.stream().map(valueString -> "\"" + valueString + "\"").collect(Collectors.joining(","));
final List<Field<?>> fields = getFieldsFromDataDictionary(dataDictionary);
BlockStmt retrieved = KiePMMLCompoundPredicateFactory.getCompoundPredicateVariableDeclaration(variableName, compoundPredicate, fields);
String text = getFileContent(TEST_01_SOURCE);
Statement expected = JavaParserUtils.parseBlock(String.format(text, variableName, valuesString, booleanOperatorString));
assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
List<Class<?>> imports = Arrays.asList(KiePMMLCompoundPredicate.class, KiePMMLSimplePredicate.class, KiePMMLSimpleSetPredicate.class, Arrays.class, Collections.class);
commonValidateCompilationWithImports(retrieved, imports);
}
use of org.dmg.pmml.SimpleSetPredicate in project drools by kiegroup.
the class PMMLModelTestUtils method getRandomSimpleSetPredicate.
public static SimpleSetPredicate getRandomSimpleSetPredicate() {
FieldName fieldName = FieldName.create(RandomStringUtils.random(6, true, false));
SimpleSetPredicate toReturn = new SimpleSetPredicate();
toReturn.setField(fieldName);
toReturn.setBooleanOperator(getRandomSimpleSetPredicateOperator());
Array.Type arrayType = getRandomArrayType();
List<String> values = getStringObjects(arrayType, 3);
Array array = getArray(arrayType, values);
toReturn.setArray(array);
return toReturn;
}
Aggregations