use of org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue in project drools by kiegroup.
the class KiePMMLASTFactoryUtilsTest method populateKiePMMLFieldOperatorValueListWithCompoundPredicates.
@Test
public void populateKiePMMLFieldOperatorValueListWithCompoundPredicates() {
final List<KiePMMLFieldOperatorValue> toPopulate = new ArrayList<>();
KiePMMLASTFactoryUtils.populateKiePMMLFieldOperatorValueListWithCompoundPredicates(toPopulate, compoundPredicates, fieldTypeMap);
assertFalse(toPopulate.isEmpty());
// one entry is for "AND" compounds and the other is for "OR" ones
assertEquals(2, toPopulate.size());
final Map<CompoundPredicate.BooleanOperator, List<CompoundPredicate>> partitionedCompoundPredicates = compoundPredicates.stream().collect(Collectors.groupingBy(CompoundPredicate::getBooleanOperator));
partitionedCompoundPredicates.forEach((booleanOperator, compoundPredicates) -> {
final KiePMMLFieldOperatorValue operatorValue = toPopulate.stream().filter(kiePMMLFieldOperatorValue -> kiePMMLFieldOperatorValue.getOperator().equals(BOOLEAN_OPERATOR.byName(booleanOperator.value()))).findFirst().orElseThrow(() -> new RuntimeException("Failed toRetrieve KiePMMLFieldOperatorValue for " + "BooleanOperator " + booleanOperator));
final List<KiePMMLFieldOperatorValue> nestedKiePMMLFieldOperatorValues = operatorValue.getNestedKiePMMLFieldOperatorValues();
final List<Predicate> nestedPredicates = compoundPredicates.stream().flatMap(compoundPredicate -> compoundPredicate.getPredicates().stream()).collect(Collectors.toList());
assertEquals(nestedPredicates.size(), nestedKiePMMLFieldOperatorValues.size());
nestedKiePMMLFieldOperatorValues.forEach(new Consumer<KiePMMLFieldOperatorValue>() {
@Override
public void accept(KiePMMLFieldOperatorValue kiePMMLFieldOperatorValue) {
assertEquals(1, kiePMMLFieldOperatorValue.getKiePMMLOperatorValues().size());
final KiePMMLOperatorValue kiePMMLOperatorValue = kiePMMLFieldOperatorValue.getKiePMMLOperatorValues().get(0);
SimplePredicate simplePredicate = nestedPredicates.stream().map(predicate -> (SimplePredicate) predicate).filter(predicate -> predicate.getField().getValue().equals(getOriginalPredicateName(kiePMMLFieldOperatorValue.getName()))).findFirst().orElseThrow(() -> new RuntimeException("Failed to find SimplePredicate for " + kiePMMLFieldOperatorValue.getName()));
commonVerifyKiePMMLOperatorValue(kiePMMLOperatorValue, simplePredicate);
nestedPredicates.remove(simplePredicate);
}
});
assertTrue(nestedPredicates.isEmpty());
});
}
use of org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue in project drools by kiegroup.
the class KiePMMLDescrLhsFactoryTest method declareConstraintsXorWrongInput.
@Test(expected = KiePMMLException.class)
public void declareConstraintsXorWrongInput() {
String temperatureField = "TEMPERATURE";
String humidityField = "HUMIDITY";
final List<KiePMMLFieldOperatorValue> xorConstraints = Arrays.asList(new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.LESS_THAN, 35)), null), new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.GREATER_THAN, 85)), null), new KiePMMLFieldOperatorValue(humidityField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.LESS_THAN, 56)), null), new KiePMMLFieldOperatorValue(humidityField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.GREATER_THAN, 91)), null));
KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintsXor(xorConstraints);
}
use of org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue in project drools by kiegroup.
the class KiePMMLDescrLhsFactoryTest method declareConstraintsXor.
@Test
public void declareConstraintsXor() {
String temperatureField = "TEMPERATURE";
final List<KiePMMLFieldOperatorValue> xorConstraints = Arrays.asList(new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.LESS_THAN, 35)), null), new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.GREATER_THAN, 85)), null));
KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintsXor(xorConstraints);
assertNotNull(lhsBuilder.getDescr());
assertNotNull(lhsBuilder.getDescr().getDescrs());
assertEquals(1, lhsBuilder.getDescr().getDescrs().size());
assertTrue(lhsBuilder.getDescr().getDescrs().get(0) instanceof AndDescr);
AndDescr rootAndDescr = (AndDescr) lhsBuilder.getDescr().getDescrs().get(0);
assertEquals(2, rootAndDescr.getDescrs().size());
assertTrue(rootAndDescr.getDescrs().get(0) instanceof NotDescr);
assertTrue(rootAndDescr.getDescrs().get(1) instanceof ExistsDescr);
// "Not" construct
NotDescr notDescr = (NotDescr) rootAndDescr.getDescrs().get(0);
assertEquals(1, notDescr.getDescrs().size());
assertTrue(notDescr.getDescrs().get(0) instanceof AndDescr);
AndDescr notAndDescr = (AndDescr) notDescr.getDescrs().get(0);
assertTrue(notAndDescr.getDescrs().get(0) instanceof PatternDescr);
assertTrue(notAndDescr.getDescrs().get(1) instanceof PatternDescr);
PatternDescr patternDescr = (PatternDescr) notAndDescr.getDescrs().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());
patternDescr = (PatternDescr) notAndDescr.getDescrs().get(1);
assertEquals(temperatureField, 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());
// "Exists" construct
ExistsDescr existsDescr = (ExistsDescr) rootAndDescr.getDescrs().get(1);
assertEquals(1, existsDescr.getDescrs().size());
assertTrue(existsDescr.getDescrs().get(0) instanceof OrDescr);
OrDescr existsOrDescr = (OrDescr) existsDescr.getDescrs().get(0);
assertEquals(2, existsOrDescr.getDescrs().size());
assertTrue(existsOrDescr.getDescrs().get(0) instanceof PatternDescr);
assertTrue(existsOrDescr.getDescrs().get(1) instanceof OrDescr);
patternDescr = (PatternDescr) existsOrDescr.getDescrs().get(0);
assertEquals(temperatureField, 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 < 35";
assertEquals(expected, exprConstraintDescr.getExpression());
OrDescr nestedOrDescr = (OrDescr) existsOrDescr.getDescrs().get(1);
assertEquals(1, nestedOrDescr.getDescrs().size());
assertTrue(nestedOrDescr.getDescrs().get(0) instanceof PatternDescr);
patternDescr = (PatternDescr) nestedOrDescr.getDescrs().get(0);
assertEquals(temperatureField, 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());
}
use of org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue in project drools by kiegroup.
the class KiePMMLDescrLhsFactoryTest method declareConstraintsOr.
@Test
public void declareConstraintsOr() {
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.or());
assertNotNull(lhsBuilder.getDescr());
assertEquals(1, lhsBuilder.getDescr().getDescrs().size());
assertTrue(lhsBuilder.getDescr().getDescrs().get(0) instanceof OrDescr);
OrDescr baseOrDescr = (OrDescr) lhsBuilder.getDescr().getDescrs().get(0);
final List<BaseDescr> descrs = baseOrDescr.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());
}
use of org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue in project drools by kiegroup.
the class KiePMMLDescrFactoryTest method getBaseDescr.
@Test
public void getBaseDescr() {
List<KiePMMLDroolsType> types = new ArrayList<>();
types.add(KiePMMLDescrTestUtils.getDroolsType());
types.add(KiePMMLDescrTestUtils.getDottedDroolsType());
List<KiePMMLFieldOperatorValue> orConstraints = Arrays.asList(new KiePMMLFieldOperatorValue(PATTERN_TYPE, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.LESS_THAN, 35)), null), new KiePMMLFieldOperatorValue(PATTERN_TYPE, BOOLEAN_OPERATOR.AND, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.GREATER_THAN, 85)), null));
KiePMMLDroolsRule rule = KiePMMLDroolsRule.builder(RULE_NAME, STATUS_TO_SET, Collections.emptyList()).withOrConstraints(orConstraints).build();
List<KiePMMLDroolsRule> rules = new ArrayList<>();
rules.add(rule);
KiePMMLDroolsAST drooledAST = new KiePMMLDroolsAST(types, rules);
PackageDescr packageDescr = KiePMMLDescrFactory.getBaseDescr(drooledAST, PACKAGE_NAME);
assertEquals(PACKAGE_NAME, packageDescr.getName());
checkImports(packageDescr.getImports());
checkGlobals(packageDescr.getGlobals());
checkRules(packageDescr.getRules());
}
Aggregations