use of org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue in project drools by kiegroup.
the class KiePMMLASTFactoryUtilsTest method getConstraintEntriesFromXOrCompoundPredicate.
@Test
public void getConstraintEntriesFromXOrCompoundPredicate() {
CompoundPredicate compoundPredicate = new CompoundPredicate();
compoundPredicate.setBooleanOperator(CompoundPredicate.BooleanOperator.XOR);
List<Predicate> predicates = IntStream.range(0, 2).mapToObj(index -> simplePredicates.get(index)).collect(Collectors.toList());
compoundPredicate.getPredicates().addAll(predicates);
List<KiePMMLFieldOperatorValue> retrieved = KiePMMLASTFactoryUtils.getConstraintEntriesFromXOrCompoundPredicate(compoundPredicate, fieldTypeMap);
assertNotNull(retrieved);
assertEquals(predicates.size(), retrieved.size());
commonVerifyKiePMMLFieldOperatorValueList(retrieved, null);
}
use of org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue in project drools by kiegroup.
the class KiePMMLASTFactoryUtilsTest method getConstraintEntryFromSimplePredicates.
@Test
public void getConstraintEntryFromSimplePredicates() {
final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
String fieldName = "FIELD_NAME";
List<SimplePredicate> simplePredicates = IntStream.range(0, 2).mapToObj(index -> {
fieldTypeMap.put(fieldName, new KiePMMLOriginalTypeGeneratedType(DataType.STRING.value(), getSanitizedClassName(fieldName.toUpperCase())));
return PMMLModelTestUtils.getSimplePredicate(fieldName, "VALUE-" + index, SimplePredicate.Operator.LESS_THAN);
}).collect(Collectors.toList());
final KiePMMLFieldOperatorValue retrieved = KiePMMLASTFactoryUtils.getConstraintEntryFromSimplePredicates(fieldName, BOOLEAN_OPERATOR.OR, simplePredicates, fieldTypeMap);
assertEquals(fieldName, retrieved.getName());
assertNotNull(retrieved.getConstraintsAsString());
String expected = "value < \"VALUE-0\" || value < \"VALUE-1\"";
assertEquals(expected, retrieved.getConstraintsAsString());
}
use of org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue in project drools by kiegroup.
the class KiePMMLASTFactoryUtils method populateKiePMMLFieldOperatorValueListWithCompoundPredicates.
/**
* Populate the given <code>List<KiePMMLFieldOperatorValue></code> with <code>KiePMMLFieldOperatorValue</code>s generated from the given <b>compoundPredicates</b>
* @param toPopulate
* @param compoundPredicates
* @param fieldTypeMap
*/
static void populateKiePMMLFieldOperatorValueListWithCompoundPredicates(final List<KiePMMLFieldOperatorValue> toPopulate, final List<CompoundPredicate> compoundPredicates, final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap) {
final List<KiePMMLFieldOperatorValue> nestedAndPredicates = new LinkedList<>();
final List<KiePMMLFieldOperatorValue> nestedOrPredicates = new LinkedList<>();
compoundPredicates.forEach(nestedCompoundPredicate -> {
switch((nestedCompoundPredicate).getBooleanOperator()) {
case OR:
nestedOrPredicates.addAll(getConstraintEntriesFromAndOrCompoundPredicate(nestedCompoundPredicate, fieldTypeMap));
break;
case AND:
nestedAndPredicates.addAll(getConstraintEntriesFromAndOrCompoundPredicate(nestedCompoundPredicate, fieldTypeMap));
break;
default:
throw new IllegalStateException(String.format("Unmanaged CompoundPredicate.booleanOperator %s at populateKiePMMLFieldOperatorValueListWithCompoundPredicates", nestedCompoundPredicate.getBooleanOperator()));
}
});
if (!nestedAndPredicates.isEmpty()) {
toPopulate.add(new KiePMMLFieldOperatorValue(null, BOOLEAN_OPERATOR.AND, Collections.emptyList(), nestedAndPredicates));
}
if (!nestedOrPredicates.isEmpty()) {
toPopulate.add(new KiePMMLFieldOperatorValue(null, BOOLEAN_OPERATOR.OR, Collections.emptyList(), nestedOrPredicates));
}
}
use of org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue 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.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue in project drools by kiegroup.
the class KiePMMLDescrLhsFactoryTest method declareConstraintAnd.
@Test
public void declareConstraintAnd() {
String temperatureField = "TEMPERATURE";
String humidityField = "HUMIDITY";
List<KiePMMLFieldOperatorValue> kiePMMLOperatorValues = Arrays.asList(new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.AND, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.LESS_THAN, 35)), null), new KiePMMLFieldOperatorValue(humidityField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.GREATER_THAN, 85)), null));
KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintsAndOr(kiePMMLOperatorValues, lhsBuilder.and());
assertNotNull(lhsBuilder.getDescr());
assertEquals(1, lhsBuilder.getDescr().getDescrs().size());
assertTrue(lhsBuilder.getDescr().getDescrs().get(0) instanceof AndDescr);
AndDescr baseAndDescr = (AndDescr) lhsBuilder.getDescr().getDescrs().get(0);
final List<BaseDescr> descrs = baseAndDescr.getDescrs();
assertNotNull(descrs);
assertEquals(2, descrs.size());
// First KiePMMLFieldOperatorValue
assertTrue(descrs.get(0) instanceof PatternDescr);
PatternDescr patternDescr = (PatternDescr) descrs.get(0);
assertEquals(temperatureField, patternDescr.getObjectType());
assertNull(patternDescr.getIdentifier());
assertTrue(patternDescr.getConstraint() instanceof AndDescr);
AndDescr andDescr = (AndDescr) patternDescr.getConstraint();
assertEquals(1, andDescr.getDescrs().size());
assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
ExprConstraintDescr exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
assertFalse(exprConstraintDescr.isNegated());
assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
String expected = "value < 35";
assertEquals(expected, exprConstraintDescr.getExpression());
// Second KiePMMLFieldOperatorValue
assertTrue(descrs.get(1) instanceof PatternDescr);
patternDescr = (PatternDescr) descrs.get(1);
assertEquals(humidityField, patternDescr.getObjectType());
assertNull(patternDescr.getIdentifier());
assertTrue(patternDescr.getConstraint() instanceof AndDescr);
andDescr = (AndDescr) patternDescr.getConstraint();
assertEquals(1, andDescr.getDescrs().size());
assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
assertFalse(exprConstraintDescr.isNegated());
assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
expected = "value > 85";
assertEquals(expected, exprConstraintDescr.getExpression());
}
Aggregations