use of org.dmg.pmml.SimplePredicate 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.SimplePredicate 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.SimplePredicate in project drools by kiegroup.
the class KiePMMLSimplePredicateASTFactoryTest method declareRuleFromSimplePredicateSurrogateNotFinalLeaf.
@Test
public void declareRuleFromSimplePredicateSurrogateNotFinalLeaf() {
final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
SimplePredicate simplePredicate = getSimplePredicate("outlook", "VALUE", SimplePredicate.Operator.LESS_THAN, fieldTypeMap);
String currentRule = "_will play_will play";
String agendaActivationGroup = "_will play_will play Group";
String result = "RESULT";
String parentPath = "parentPath";
final List<KiePMMLDroolsRule> rules = new ArrayList<>();
PredicateASTFactoryData predicateASTFactoryData = getPredicateASTFactoryData(simplePredicate, Collections.emptyList(), rules, parentPath, currentRule, fieldTypeMap);
KiePMMLSimplePredicateASTFactory.factory(predicateASTFactoryData).declareRuleFromSimplePredicateSurrogate(agendaActivationGroup, result, false);
assertEquals(2, rules.size());
// This is the "TRUE" matching rule
KiePMMLDroolsRule retrieved = rules.get(0);
assertNotNull(retrieved);
String baseExpectedRule = String.format(KiePMMLAbstractModelASTFactory.SURROGATE_RULENAME_PATTERN, currentRule, fieldTypeMap.get(simplePredicate.getField().getValue()).getGeneratedType());
String expectedRule = baseExpectedRule + "_TRUE";
assertEquals(expectedRule, retrieved.getName());
assertEquals(currentRule, retrieved.getStatusToSet());
assertNull(retrieved.getStatusConstraint());
assertEquals(agendaActivationGroup, retrieved.getAgendaGroup());
assertEquals(agendaActivationGroup, retrieved.getActivationGroup());
assertNull(retrieved.getIfBreakField());
assertNull(retrieved.getIfBreakOperator());
assertNull(retrieved.getIfBreakValue());
assertNotNull(retrieved.getAndConstraints());
assertEquals(1, retrieved.getAndConstraints().size());
KiePMMLFieldOperatorValue kiePMMLFieldOperatorValue = retrieved.getAndConstraints().get(0);
assertEquals("OUTLOOK", kiePMMLFieldOperatorValue.getName());
assertEquals(BOOLEAN_OPERATOR.SURROGATE, kiePMMLFieldOperatorValue.getOperator());
assertEquals("value < \"VALUE\"", kiePMMLFieldOperatorValue.getConstraintsAsString());
assertNull(retrieved.getResult());
assertNull(retrieved.getResultCode());
// This is the "FALSE" matching rule
retrieved = rules.get(1);
assertNotNull(retrieved);
expectedRule = baseExpectedRule + "_FALSE";
assertEquals(expectedRule, retrieved.getName());
assertEquals(parentPath, retrieved.getStatusToSet());
assertNull(retrieved.getStatusConstraint());
assertEquals(agendaActivationGroup, retrieved.getAgendaGroup());
assertEquals(agendaActivationGroup, retrieved.getActivationGroup());
assertNull(retrieved.getIfBreakField());
assertNull(retrieved.getIfBreakOperator());
assertNull(retrieved.getIfBreakValue());
assertNull(retrieved.getAndConstraints());
assertNotNull(retrieved.getNotConstraints());
assertEquals(1, retrieved.getNotConstraints().size());
kiePMMLFieldOperatorValue = retrieved.getNotConstraints().get(0);
assertEquals("OUTLOOK", kiePMMLFieldOperatorValue.getName());
assertEquals(BOOLEAN_OPERATOR.SURROGATE, kiePMMLFieldOperatorValue.getOperator());
assertEquals("value < \"VALUE\"", kiePMMLFieldOperatorValue.getConstraintsAsString());
assertNull(retrieved.getResult());
assertNull(retrieved.getResultCode());
}
use of org.dmg.pmml.SimplePredicate in project drools by kiegroup.
the class KiePMMLCompoundPredicateASTFactoryTest method declareRuleFromCompoundPredicateSurrogateFinalLeaf.
@Test
public void declareRuleFromCompoundPredicateSurrogateFinalLeaf() {
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";
CompoundPredicate compoundPredicate = new CompoundPredicate();
compoundPredicate.setBooleanOperator(CompoundPredicate.BooleanOperator.SURROGATE);
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);
// For each "surrogate" predicate two rules -"TRUE" and "FALSE" - are generated; one more rule is generated for the Compound predicate itself
int expectedRules = (predicates.size() * 2) + 1;
assertEquals(expectedRules, rules.size());
String agendaActivationGroup = String.format(SURROGATE_GROUP_PATTERN, currentRule);
for (KiePMMLDroolsRule retrieved : rules) {
String ruleName = retrieved.getName();
if (ruleName.contains("_surrogate_")) {
String[] ruleNameParts = ruleName.split("_surrogate_");
String generatedTypePart = ruleNameParts[1];
boolean isTrueRule = generatedTypePart.endsWith("_TRUE");
final String generatedType = generatedTypePart.replace("_TRUE", "").replace("_FALSE", "");
final Optional<String> fieldName = fieldTypeMap.entrySet().stream().filter(entry -> generatedType.equals(entry.getValue().getGeneratedType())).map(Map.Entry::getKey).findFirst();
if (fieldName.isPresent()) {
SimplePredicate mappedPredicate = predicates.stream().filter(pred -> fieldName.get().equals(pred.getField().getValue())).findFirst().orElse(null);
assertNotNull(mappedPredicate);
assertNull(retrieved.getStatusConstraint());
assertEquals(agendaActivationGroup, retrieved.getActivationGroup());
assertEquals(agendaActivationGroup, retrieved.getAgendaGroup());
// Those are in a final leaf node
if (isTrueRule) {
assertEquals(DONE, retrieved.getStatusToSet());
assertEquals(result, retrieved.getResult());
assertEquals(ResultCode.OK, retrieved.getResultCode());
} else {
assertEquals(parentPath, retrieved.getStatusToSet());
assertNull(retrieved.getResult());
assertNull(retrieved.getResultCode());
}
}
} else {
assertNotNull(retrieved.getStatusConstraint());
assertEquals(String.format(STATUS_PATTERN, parentPath), retrieved.getStatusConstraint());
assertEquals(agendaActivationGroup, retrieved.getFocusedAgendaGroup());
assertNull(retrieved.getStatusToSet());
assertNull(retrieved.getResult());
assertNull(retrieved.getResultCode());
}
}
}
use of org.dmg.pmml.SimplePredicate in project drools by kiegroup.
the class KiePMMLScorecardModelCharacteristicASTFactoryTest method declareRulesFromCharacteristics.
@Test
public void declareRulesFromCharacteristics() {
Characteristics characteristics = scorecardModel.getCharacteristics();
String parentPath = "_will";
List<KiePMMLDroolsRule> retrieved = getKiePMMLScorecardModelCharacteristicASTFactory().declareRulesFromCharacteristics(characteristics, parentPath, null);
final List<Characteristic> characteristicList = characteristics.getCharacteristics();
List<Attribute> attributes = new ArrayList<>();
AtomicInteger counter = new AtomicInteger(0);
for (int i = 0; i < characteristicList.size(); i++) {
Characteristic characteristic = characteristicList.get(i);
attributes.addAll(characteristic.getAttributes());
for (int j = 0; j < characteristic.getAttributes().size(); j++) {
Attribute attribute = characteristic.getAttributes().get(j);
KiePMMLDroolsRule rule = retrieved.get(counter.incrementAndGet());
int expectedOperatorValuesSize = 1;
Integer expectedAndConstraints = null;
Integer expectedInConstraints = null;
BOOLEAN_OPERATOR expectedOperator = BOOLEAN_OPERATOR.AND;
if (attribute.getPredicate() instanceof SimplePredicate) {
expectedAndConstraints = 1;
}
if (attribute.getPredicate() instanceof CompoundPredicate) {
expectedOperatorValuesSize = ((CompoundPredicate) attribute.getPredicate()).getPredicates().size();
expectedAndConstraints = 1;
}
if (attribute.getPredicate() instanceof SimpleSetPredicate) {
expectedInConstraints = 1;
}
boolean isLastCharacteristic = (i == characteristicList.size() - 1);
String statusToSet = isLastCharacteristic ? DONE : String.format(PATH_PATTERN, parentPath, characteristicList.get(i + 1).getName());
commonValidateRule(rule, attribute, statusToSet, parentPath + "_" + characteristic.getName(), j, isLastCharacteristic, expectedAndConstraints, expectedInConstraints, expectedOperator, null, expectedOperatorValuesSize);
}
}
assertEquals(attributes.size() + 1, retrieved.size());
}
Aggregations