Search in sources :

Example 1 with KiePMMLFieldOperatorValue

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());
    });
}
Also used : IntStream(java.util.stream.IntStream) Predicate(org.dmg.pmml.Predicate) BeforeClass(org.junit.BeforeClass) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PMMLModelTestUtils(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils) KiePMMLOperatorValue(org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue) Map(java.util.Map) CompoundPredicate(org.dmg.pmml.CompoundPredicate) Assert.assertNotNull(org.junit.Assert.assertNotNull) DataType(org.dmg.pmml.DataType) BOOLEAN_OPERATOR(org.kie.pmml.api.enums.BOOLEAN_OPERATOR) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) OPERATOR(org.kie.pmml.api.enums.OPERATOR) Consumer(java.util.function.Consumer) List(java.util.List) SimplePredicate(org.dmg.pmml.SimplePredicate) Assert.assertFalse(org.junit.Assert.assertFalse) PMMLModelTestUtils.getRandomSimplePredicateOperator(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomSimplePredicateOperator) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) Assert.assertEquals(org.junit.Assert.assertEquals) KiePMMLModelUtils.getSanitizedClassName(org.kie.pmml.commons.utils.KiePMMLModelUtils.getSanitizedClassName) PMMLModelTestUtils.getRandomObject(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomObject) ArrayList(java.util.ArrayList) SimplePredicate(org.dmg.pmml.SimplePredicate) Predicate(org.dmg.pmml.Predicate) CompoundPredicate(org.dmg.pmml.CompoundPredicate) SimplePredicate(org.dmg.pmml.SimplePredicate) KiePMMLOperatorValue(org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 2 with KiePMMLFieldOperatorValue

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());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) SimplePredicate(org.dmg.pmml.SimplePredicate) KiePMMLASTTestUtils.getPredicateASTFactoryData(org.kie.pmml.models.drools.utils.KiePMMLASTTestUtils.getPredicateASTFactoryData) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Test(org.junit.Test)

Example 3 with KiePMMLFieldOperatorValue

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());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) SimplePredicate(org.dmg.pmml.SimplePredicate) KiePMMLASTTestUtils.getPredicateASTFactoryData(org.kie.pmml.models.drools.utils.KiePMMLASTTestUtils.getPredicateASTFactoryData) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Test(org.junit.Test)

Example 4 with KiePMMLFieldOperatorValue

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());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) SimplePredicate(org.dmg.pmml.SimplePredicate) KiePMMLASTTestUtils.getPredicateASTFactoryData(org.kie.pmml.models.drools.utils.KiePMMLASTTestUtils.getPredicateASTFactoryData) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Test(org.junit.Test)

Example 5 with KiePMMLFieldOperatorValue

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);
}
Also used : KiePMMLOperatorValue(org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) Test(org.junit.Test)

Aggregations

KiePMMLFieldOperatorValue (org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue)17 Test (org.junit.Test)14 KiePMMLOperatorValue (org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue)11 ArrayList (java.util.ArrayList)8 SimplePredicate (org.dmg.pmml.SimplePredicate)8 HashMap (java.util.HashMap)7 KiePMMLDroolsRule (org.kie.pmml.models.drools.ast.KiePMMLDroolsRule)7 KiePMMLOriginalTypeGeneratedType (org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType)7 CompoundPredicate (org.dmg.pmml.CompoundPredicate)4 AndDescr (org.drools.drl.ast.descr.AndDescr)4 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)4 PatternDescr (org.drools.drl.ast.descr.PatternDescr)4 BOOLEAN_OPERATOR (org.kie.pmml.api.enums.BOOLEAN_OPERATOR)4 OPERATOR (org.kie.pmml.api.enums.OPERATOR)4 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)4 KiePMMLASTTestUtils.getPredicateASTFactoryData (org.kie.pmml.models.drools.utils.KiePMMLASTTestUtils.getPredicateASTFactoryData)4 List (java.util.List)3 Map (java.util.Map)3 Consumer (java.util.function.Consumer)3 Collectors (java.util.stream.Collectors)3