use of org.dmg.pmml.SimpleSetPredicate in project drools by kiegroup.
the class KiePMMLSimpleSetPredicateASTFactoryTest method declareRuleFromSimpleSetPredicateIsNotInFinalLeaf.
@Test
public void declareRuleFromSimpleSetPredicateIsNotInFinalLeaf() {
final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
List<String> values = Arrays.asList("3", "8.5");
final SimpleSetPredicate simpleSetPredicate = getSimpleSetPredicate("input2", values, SimpleSetPredicate.BooleanOperator.IS_NOT_IN, fieldTypeMap);
String parentPath = "_classA";
String currentRule = "_classA_classC";
String result = "classC";
String declaredType = fieldTypeMap.get("input2").getGeneratedType();
final List<KiePMMLDroolsRule> rules = new ArrayList<>();
String statusToSet = DONE;
PredicateASTFactoryData predicateASTFactoryData = getPredicateASTFactoryData(simpleSetPredicate, Collections.emptyList(), rules, parentPath, currentRule, fieldTypeMap);
KiePMMLSimpleSetPredicateASTFactory.factory(predicateASTFactoryData).declareRuleFromSimpleSetPredicate(result, true);
assertEquals(1, rules.size());
final KiePMMLDroolsRule retrieved = rules.get(0);
assertNotNull(retrieved);
assertEquals(currentRule, retrieved.getName());
assertEquals(statusToSet, retrieved.getStatusToSet());
assertEquals(String.format(STATUS_PATTERN, parentPath), retrieved.getStatusConstraint());
assertEquals(ResultCode.OK, retrieved.getResultCode());
assertEquals(result, retrieved.getResult());
assertNotNull(retrieved.getNotInConstraints());
final Map<String, List<Object>> notInConstraints = retrieved.getNotInConstraints();
assertEquals(1, notInConstraints.size());
assertTrue(notInConstraints.containsKey(declaredType));
final List<Object> retrievedValues = notInConstraints.get(declaredType);
List<String> originalPredicateValues = Arrays.asList(((String) simpleSetPredicate.getArray().getValue()).split(" "));
assertEquals(originalPredicateValues.size(), retrievedValues.size());
retrievedValues.forEach(retrievedValue -> {
assertTrue(originalPredicateValues.contains(retrievedValue));
});
}
use of org.dmg.pmml.SimpleSetPredicate 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.SimpleSetPredicate 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.SimpleSetPredicate in project jpmml-sparkml by jpmml.
the class TreeModelCompactor method handleNodePush.
private void handleNodePush(Node node) {
String id = node.getId();
String score = node.getScore();
if (id != null) {
throw new IllegalArgumentException();
}
if (node.hasNodes()) {
List<Node> children = node.getNodes();
if (children.size() != 2 || score != null) {
throw new IllegalArgumentException();
}
Node firstChild = children.get(0);
Node secondChild = children.get(1);
Predicate firstPredicate = firstChild.getPredicate();
Predicate secondPredicate = secondChild.getPredicate();
predicate: if (firstPredicate instanceof SimplePredicate && secondPredicate instanceof SimplePredicate) {
SimplePredicate firstSimplePredicate = (SimplePredicate) firstPredicate;
SimplePredicate secondSimplePredicate = (SimplePredicate) secondPredicate;
SimplePredicate.Operator firstOperator = firstSimplePredicate.getOperator();
SimplePredicate.Operator secondOperator = secondSimplePredicate.getOperator();
if (!(firstSimplePredicate.getField()).equals(secondSimplePredicate.getField())) {
throw new IllegalArgumentException();
}
if ((SimplePredicate.Operator.EQUAL).equals(firstOperator) && (SimplePredicate.Operator.EQUAL).equals(secondOperator)) {
if (!isCategoricalField(firstSimplePredicate.getField())) {
break predicate;
}
secondChild.setPredicate(new True());
} else {
if (!(firstSimplePredicate.getValue()).equals(secondSimplePredicate.getValue())) {
throw new IllegalArgumentException();
}
if ((SimplePredicate.Operator.NOT_EQUAL).equals(firstOperator) && (SimplePredicate.Operator.EQUAL).equals(secondOperator)) {
swapChildren(children);
firstChild = children.get(0);
secondChild = children.get(1);
} else if ((SimplePredicate.Operator.EQUAL).equals(firstOperator) && (SimplePredicate.Operator.NOT_EQUAL).equals(secondOperator)) {
// Ignored
} else if ((SimplePredicate.Operator.LESS_OR_EQUAL).equals(firstOperator) && (SimplePredicate.Operator.GREATER_THAN).equals(secondOperator)) {
// Ignored
} else {
throw new IllegalArgumentException();
}
secondChild.setPredicate(new True());
}
} else if (firstPredicate instanceof SimplePredicate && secondPredicate instanceof SimpleSetPredicate) {
SimplePredicate simplePredicate = (SimplePredicate) firstPredicate;
SimpleSetPredicate simpleSetPredicate = (SimpleSetPredicate) secondPredicate;
if (!(simplePredicate.getField()).equals(simpleSetPredicate.getField()) || !(SimplePredicate.Operator.EQUAL).equals(simplePredicate.getOperator()) || !(SimpleSetPredicate.BooleanOperator.IS_IN).equals(simpleSetPredicate.getBooleanOperator())) {
throw new IllegalArgumentException();
}
secondChild.setPredicate(addCategoricalField(simpleSetPredicate));
} else if (firstPredicate instanceof SimpleSetPredicate && secondPredicate instanceof SimplePredicate) {
SimpleSetPredicate simpleSetPredicate = (SimpleSetPredicate) firstPredicate;
SimplePredicate simplePredicate = (SimplePredicate) secondPredicate;
if (!(simpleSetPredicate.getField()).equals(simplePredicate.getField()) || !(SimpleSetPredicate.BooleanOperator.IS_IN).equals(simpleSetPredicate.getBooleanOperator()) || !(SimplePredicate.Operator.EQUAL).equals(simplePredicate.getOperator())) {
throw new IllegalArgumentException();
}
swapChildren(children);
firstChild = children.get(0);
secondChild = children.get(1);
secondChild.setPredicate(addCategoricalField(simpleSetPredicate));
} else if (firstPredicate instanceof SimpleSetPredicate && secondPredicate instanceof SimpleSetPredicate) {
SimpleSetPredicate firstSimpleSetPredicate = (SimpleSetPredicate) firstPredicate;
SimpleSetPredicate secondSimpleSetPredicate = (SimpleSetPredicate) secondPredicate;
if (!(firstSimpleSetPredicate.getField()).equals(secondSimpleSetPredicate.getField()) || !(SimpleSetPredicate.BooleanOperator.IS_IN).equals(firstSimpleSetPredicate.getBooleanOperator()) || !(SimpleSetPredicate.BooleanOperator.IS_IN).equals(secondSimpleSetPredicate.getBooleanOperator())) {
throw new IllegalArgumentException();
}
secondChild.setPredicate(addCategoricalField(secondSimpleSetPredicate));
} else {
throw new IllegalArgumentException();
}
} else {
if (score == null) {
throw new IllegalArgumentException();
}
}
}
use of org.dmg.pmml.SimpleSetPredicate in project jpmml-sparkml by jpmml.
the class TreeModelCompactor method isCategoricalField.
private boolean isCategoricalField(FieldName name) {
Deque<PMMLObject> parents = getParents();
for (PMMLObject parent : parents) {
if (parent instanceof Node) {
Node node = (Node) parent;
Predicate predicate = node.getPredicate();
if (predicate instanceof SimpleSetPredicate) {
SimpleSetPredicate simpleSetPredicate = (SimpleSetPredicate) predicate;
FieldName categoricalField = simpleSetPredicate.getField();
if ((name).equals(categoricalField)) {
return true;
}
} else if (predicate instanceof True) {
True truePredicate = (True) predicate;
FieldName categoricalField = this.categoricalFields.get(truePredicate);
if ((name).equals(categoricalField)) {
return true;
}
}
} else {
return false;
}
}
return false;
}
Aggregations