Search in sources :

Example 21 with CompoundPredicate

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

the class KiePMMLCompoundPredicateASTFactory method declareRuleFromCompoundPredicate.

/**
 * @param toAccumulate
 * @param statusToSet
 * @param reasonCodeAndValue
 * @param isLastCharacteristic
 */
public void declareRuleFromCompoundPredicate(final Number toAccumulate, final String statusToSet, final KiePMMLReasonCodeAndValue reasonCodeAndValue, boolean isLastCharacteristic) {
    logger.trace("declareRuleFromCompoundPredicate {} {} {}", toAccumulate, statusToSet, isLastCharacteristic);
    CompoundPredicate compoundPredicate = (CompoundPredicate) predicateASTFactoryData.getPredicate();
    switch(compoundPredicate.getBooleanOperator()) {
        case SURROGATE:
            final String agendaActivationGroup = String.format(KiePMMLAbstractModelASTFactory.SURROGATE_GROUP_PATTERN, predicateASTFactoryData.getCurrentRule());
            declareRuleFromCompoundPredicateSurrogate(agendaActivationGroup, statusToSet);
            KiePMMLCompoundPredicateWithAccumulationASTFactory.declareRuleFromCompoundPredicateSurrogate(predicateASTFactoryData, agendaActivationGroup, toAccumulate, statusToSet, reasonCodeAndValue, isLastCharacteristic);
            break;
        case AND:
            declareRuleFromCompoundPredicateAndOrXor(toAccumulate, statusToSet, reasonCodeAndValue, isLastCharacteristic);
            break;
        case OR:
            declareRuleFromCompoundPredicateAndOrXor(toAccumulate, statusToSet, reasonCodeAndValue, isLastCharacteristic);
            break;
        case XOR:
            declareRuleFromCompoundPredicateAndOrXor(toAccumulate, statusToSet, reasonCodeAndValue, isLastCharacteristic);
            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 22 with CompoundPredicate

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

the class KiePMMLCompoundPredicateWithAccumulationASTFactory method declareRuleFromCompoundPredicateSurrogate.

/**
 * Method to be invoked when <b>compoundPredicate.getBooleanOperator()</b> is <code>SURROGATE</code>.
 * Throws exception otherwise
 * @param predicateASTFactoryData
 * @param agendaActivationGroup
 * @param toAccumulate
 * @param statusToSet
 * @param reasonCodeAndValue
 * @param isLastCharacteristic
 */
public static void declareRuleFromCompoundPredicateSurrogate(final PredicateASTFactoryData predicateASTFactoryData, final String agendaActivationGroup, final Number toAccumulate, final String statusToSet, final KiePMMLReasonCodeAndValue reasonCodeAndValue, final boolean isLastCharacteristic) {
    logger.trace("declareRuleFromCompoundPredicateSurrogate {} {} {} {} {}", predicateASTFactoryData, agendaActivationGroup, toAccumulate, statusToSet, isLastCharacteristic);
    // Managing only SimplePredicates for the moment being
    CompoundPredicate compoundPredicate = (CompoundPredicate) predicateASTFactoryData.getPredicate();
    final List<Predicate> simplePredicates = compoundPredicate.getPredicates().stream().filter(predicate -> predicate instanceof SimplePredicate).collect(Collectors.toList());
    simplePredicates.forEach(predicate -> {
        SimplePredicate simplePredicate = (SimplePredicate) predicate;
        PredicateASTFactoryData newPredicateASTFactoryData = predicateASTFactoryData.cloneWithPredicate(simplePredicate);
        KiePMMLSimplePredicateASTFactory.factory(newPredicateASTFactoryData).declareRuleFromSimplePredicateSurrogate(agendaActivationGroup, toAccumulate, statusToSet, reasonCodeAndValue, isLastCharacteristic);
    });
}
Also used : CompoundPredicate(org.dmg.pmml.CompoundPredicate) List(java.util.List) Predicate(org.dmg.pmml.Predicate) SimplePredicate(org.dmg.pmml.SimplePredicate) Logger(org.slf4j.Logger) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) ResultCode(org.kie.pmml.api.enums.ResultCode) KiePMMLReasonCodeAndValue(org.kie.pmml.models.drools.tuples.KiePMMLReasonCodeAndValue) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) CompoundPredicate(org.dmg.pmml.CompoundPredicate) SimplePredicate(org.dmg.pmml.SimplePredicate) CompoundPredicate(org.dmg.pmml.CompoundPredicate) Predicate(org.dmg.pmml.Predicate) SimplePredicate(org.dmg.pmml.SimplePredicate)

Example 23 with CompoundPredicate

use of org.dmg.pmml.CompoundPredicate 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);
}
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) CompoundPredicate(org.dmg.pmml.CompoundPredicate) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) Predicate(org.dmg.pmml.Predicate) CompoundPredicate(org.dmg.pmml.CompoundPredicate) SimplePredicate(org.dmg.pmml.SimplePredicate) Test(org.junit.Test)

Example 24 with CompoundPredicate

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

the class KiePMMLScorecardModelCharacteristicASTFactoryTest method getCompoundPredicate.

private CompoundPredicate getCompoundPredicate() {
    CompoundPredicate toReturn = new CompoundPredicate();
    toReturn.setBooleanOperator(CompoundPredicate.BooleanOperator.AND);
    final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
    toReturn.addPredicates(getSimplePredicate(fieldName, DataType.DOUBLE, 5.0, SimplePredicate.Operator.GREATER_OR_EQUAL, fieldTypeMap), getSimplePredicate(fieldName, DataType.DOUBLE, 12.0, SimplePredicate.Operator.LESS_THAN, fieldTypeMap));
    return toReturn;
}
Also used : HashMap(java.util.HashMap) CompoundPredicate(org.dmg.pmml.CompoundPredicate) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType)

Example 25 with CompoundPredicate

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

the class KiePMMLCompoundPredicateASTFactoryTest method declareRuleFromCompoundPredicateSurrogateFinalLeaf.

@Test
public void declareRuleFromCompoundPredicateSurrogateFinalLeaf() {
    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, true);
    // 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 in a final leaf node
                if (isTrueRule) {
                    assertEquals(DONE, retrieved.getStatusToSet());
                    assertEquals(result, retrieved.getResult());
                    assertEquals(ResultCode.OK, 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