Search in sources :

Example 6 with CompoundPredicate

use of org.dmg.pmml.CompoundPredicate in project drools by kiegroup.

the class KiePMMLCompoundPredicateASTFactory method declareRuleFromCompoundPredicate.

/**
 * @param result
 * @param isFinalLeaf
 */
public void declareRuleFromCompoundPredicate(final Object result, final boolean isFinalLeaf) {
    logger.trace("declareRuleFromCompoundPredicate {} {}", result, isFinalLeaf);
    CompoundPredicate compoundPredicate = (CompoundPredicate) predicateASTFactoryData.getPredicate();
    switch(compoundPredicate.getBooleanOperator()) {
        case SURROGATE:
            final String agendaActivationGroup = String.format(KiePMMLAbstractModelASTFactory.SURROGATE_GROUP_PATTERN, predicateASTFactoryData.getCurrentRule());
            declareRuleFromCompoundPredicateSurrogate(agendaActivationGroup, null);
            KiePMMLCompoundPredicateWithResultASTFactory.declareRuleFromCompoundPredicateSurrogate(predicateASTFactoryData, agendaActivationGroup, result, isFinalLeaf);
            break;
        case AND:
            declareRuleFromCompoundPredicateAndOrXor(result, isFinalLeaf);
            break;
        case OR:
            declareRuleFromCompoundPredicateAndOrXor(result, isFinalLeaf);
            break;
        case XOR:
            declareRuleFromCompoundPredicateAndOrXor(result, isFinalLeaf);
            break;
        default:
            throw new IllegalStateException(String.format("Unknown CompoundPredicate.booleanOperator %st", compoundPredicate.getBooleanOperator()));
    }
}
Also used : CompoundPredicate(org.dmg.pmml.CompoundPredicate) KiePMMLASTFactoryUtils.getConstraintEntriesFromAndOrCompoundPredicate(org.kie.pmml.models.drools.utils.KiePMMLASTFactoryUtils.getConstraintEntriesFromAndOrCompoundPredicate) KiePMMLASTFactoryUtils.getConstraintEntriesFromXOrCompoundPredicate(org.kie.pmml.models.drools.utils.KiePMMLASTFactoryUtils.getConstraintEntriesFromXOrCompoundPredicate)

Example 7 with CompoundPredicate

use of org.dmg.pmml.CompoundPredicate 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 8 with CompoundPredicate

use of org.dmg.pmml.CompoundPredicate in project drools by kiegroup.

the class KiePMMLASTFactoryUtilsTest method getConstraintEntriesFromXOrCompoundPredicateWrongSize.

@Test(expected = KiePMMLException.class)
public void getConstraintEntriesFromXOrCompoundPredicateWrongSize() {
    CompoundPredicate compoundPredicate = new CompoundPredicate();
    compoundPredicate.setBooleanOperator(CompoundPredicate.BooleanOperator.XOR);
    compoundPredicate.getPredicates().addAll(simplePredicates);
    KiePMMLASTFactoryUtils.getConstraintEntriesFromXOrCompoundPredicate(compoundPredicate, fieldTypeMap);
}
Also used : CompoundPredicate(org.dmg.pmml.CompoundPredicate) Test(org.junit.Test)

Example 9 with CompoundPredicate

use of org.dmg.pmml.CompoundPredicate in project drools by kiegroup.

the class KiePMMLASTFactoryUtilsTest method getConstraintEntriesFromXOrCompoundPredicateWrongOperator.

@Test(expected = KiePMMLException.class)
public void getConstraintEntriesFromXOrCompoundPredicateWrongOperator() {
    CompoundPredicate compoundPredicate = new CompoundPredicate();
    compoundPredicate.setBooleanOperator(CompoundPredicate.BooleanOperator.AND);
    KiePMMLASTFactoryUtils.getConstraintEntriesFromXOrCompoundPredicate(compoundPredicate, fieldTypeMap);
}
Also used : CompoundPredicate(org.dmg.pmml.CompoundPredicate) Test(org.junit.Test)

Example 10 with CompoundPredicate

use of org.dmg.pmml.CompoundPredicate in project drools by kiegroup.

the class KiePMMLCompoundPredicateASTFactoryTest method declareRuleFromCompoundPredicateSurrogateNotFinalLeaf.

@Test
public void declareRuleFromCompoundPredicateSurrogateNotFinalLeaf() {
    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, false);
    // 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 not in a final leaf node
                if (isTrueRule) {
                    assertEquals(currentRule, retrieved.getStatusToSet());
                    assertNull(retrieved.getResult());
                    assertNull(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());
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SimplePredicate(org.dmg.pmml.SimplePredicate) KiePMMLASTTestUtils.getPredicateASTFactoryData(org.kie.pmml.models.drools.utils.KiePMMLASTTestUtils.getPredicateASTFactoryData) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) CompoundPredicate(org.dmg.pmml.CompoundPredicate) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

CompoundPredicate (org.dmg.pmml.CompoundPredicate)29 SimplePredicate (org.dmg.pmml.SimplePredicate)17 Test (org.junit.Test)14 List (java.util.List)9 Collectors (java.util.stream.Collectors)9 Predicate (org.dmg.pmml.Predicate)9 KiePMMLDroolsRule (org.kie.pmml.models.drools.ast.KiePMMLDroolsRule)9 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 SimpleSetPredicate (org.dmg.pmml.SimpleSetPredicate)7 KiePMMLOriginalTypeGeneratedType (org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType)7 KiePMMLCompoundPredicate (org.kie.pmml.commons.model.predicates.KiePMMLCompoundPredicate)6 DataField (org.dmg.pmml.DataField)5 DataType (org.dmg.pmml.DataType)5 Assert.assertTrue (org.junit.Assert.assertTrue)5 KiePMMLSimplePredicate (org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate)5 KiePMMLSimpleSetPredicate (org.kie.pmml.commons.model.predicates.KiePMMLSimpleSetPredicate)5 PMMLModelTestUtils.getSimplePredicate (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getSimplePredicate)5 KiePMMLSimpleSetPredicateFactoryTest.getSimpleSetPredicate (org.kie.pmml.compiler.commons.codegenfactories.KiePMMLSimpleSetPredicateFactoryTest.getSimpleSetPredicate)5 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)4