use of org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue 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.ast.KiePMMLFieldOperatorValue in project drools by kiegroup.
the class KiePMMLSimplePredicateASTFactoryTest method declareRuleFromSimplePredicateSurrogateFinalLeaf.
@Test
public void declareRuleFromSimplePredicateSurrogateFinalLeaf() {
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, true);
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(DONE, retrieved.getStatusToSet());
assertNull(retrieved.getStatusConstraint());
assertEquals(agendaActivationGroup, retrieved.getAgendaGroup());
assertEquals(agendaActivationGroup, retrieved.getActivationGroup());
assertNull(retrieved.getIfBreakField());
assertNull(retrieved.getIfBreakOperator());
assertNull(retrieved.getIfBreakValue());
assertNull(retrieved.getNotConstraints());
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());
assertEquals(result, retrieved.getResult());
assertEquals(ResultCode.OK, 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 KiePMMLSimplePredicateASTFactoryTest method declareIntermediateRuleFromSimplePredicateNotFinalLeaf.
@Test
public void declareIntermediateRuleFromSimplePredicateNotFinalLeaf() {
final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
SimplePredicate simplePredicate = getSimplePredicate("outlook", "VALUE", SimplePredicate.Operator.LESS_THAN, fieldTypeMap);
String parentPath = "_will play";
String currentRule = "_will play_will play";
String declaredType = fieldTypeMap.get("outlook").getGeneratedType();
String result = "RESULT";
final List<KiePMMLDroolsRule> rules = new ArrayList<>();
PredicateASTFactoryData predicateASTFactoryData = getPredicateASTFactoryData(simplePredicate, Collections.emptyList(), rules, parentPath, currentRule, fieldTypeMap);
KiePMMLSimplePredicateASTFactory.factory(predicateASTFactoryData).declareRuleFromSimplePredicate(result, false);
assertEquals(1, rules.size());
final KiePMMLDroolsRule retrieved = rules.get(0);
assertNotNull(retrieved);
assertEquals(currentRule, retrieved.getName());
assertEquals(currentRule, retrieved.getStatusToSet());
assertEquals(String.format(KiePMMLAbstractModelASTFactory.STATUS_PATTERN, parentPath), retrieved.getStatusConstraint());
assertEquals(currentRule, retrieved.getStatusToSet());
final List<KiePMMLFieldOperatorValue> andConstraints = retrieved.getAndConstraints();
assertNotNull(andConstraints);
assertEquals(1, andConstraints.size());
KiePMMLFieldOperatorValue kiePMMLFieldOperatorValue = retrieved.getAndConstraints().get(0);
assertEquals(declaredType, kiePMMLFieldOperatorValue.getName());
assertEquals(BOOLEAN_OPERATOR.AND, kiePMMLFieldOperatorValue.getOperator());
String expectedValue = "value < \"" + simplePredicate.getValue() + "\"";
assertEquals(expectedValue, kiePMMLFieldOperatorValue.getConstraintsAsString());
}
use of org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue in project drools by kiegroup.
the class KiePMMLSimplePredicateASTFactoryTest method declareRuleFromSimplePredicateFinalLeaf.
@Test
public void declareRuleFromSimplePredicateFinalLeaf() {
final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
SimplePredicate simplePredicate = getSimplePredicate("outlook", "VALUE", SimplePredicate.Operator.LESS_THAN, fieldTypeMap);
String parentPath = "_will play";
String currentRule = "_will play_will play";
String declaredType = fieldTypeMap.get("outlook").getGeneratedType();
String result = "RESULT";
final List<KiePMMLDroolsRule> rules = new ArrayList<>();
PredicateASTFactoryData predicateASTFactoryData = getPredicateASTFactoryData(simplePredicate, Collections.emptyList(), rules, parentPath, currentRule, fieldTypeMap);
KiePMMLSimplePredicateASTFactory.factory(predicateASTFactoryData).declareRuleFromSimplePredicate(result, true);
assertEquals(1, rules.size());
final KiePMMLDroolsRule retrieved = rules.get(0);
assertNotNull(retrieved);
assertEquals(currentRule, retrieved.getName());
assertEquals(DONE, retrieved.getStatusToSet());
assertEquals(String.format(KiePMMLAbstractModelASTFactory.STATUS_PATTERN, parentPath), retrieved.getStatusConstraint());
assertEquals(ResultCode.OK, retrieved.getResultCode());
assertEquals(result, retrieved.getResult());
final List<KiePMMLFieldOperatorValue> andConstraints = retrieved.getAndConstraints();
assertNotNull(andConstraints);
assertEquals(1, andConstraints.size());
KiePMMLFieldOperatorValue kiePMMLFieldOperatorValue = retrieved.getAndConstraints().get(0);
assertEquals(declaredType, kiePMMLFieldOperatorValue.getName());
assertEquals(BOOLEAN_OPERATOR.AND, kiePMMLFieldOperatorValue.getOperator());
String expectedValue = "value < \"" + simplePredicate.getValue() + "\"";
assertEquals(expectedValue, kiePMMLFieldOperatorValue.getConstraintsAsString());
}
use of org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue 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);
}
Aggregations