Search in sources :

Example 6 with SimplePredicate

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

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

use of org.dmg.pmml.SimplePredicate 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 9 with SimplePredicate

use of org.dmg.pmml.SimplePredicate 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)

Example 10 with SimplePredicate

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

the class KiePMMLCompoundPredicateASTFactoryTest method declareRuleFromCompoundPredicateAndOrXorNotFinalLeaf.

@Test
public void declareRuleFromCompoundPredicateAndOrXorNotFinalLeaf() {
    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";
    for (CompoundPredicate.BooleanOperator operator : CompoundPredicate.BooleanOperator.values()) {
        if (operator.equals(CompoundPredicate.BooleanOperator.SURROGATE)) {
            continue;
        }
        CompoundPredicate compoundPredicate = new CompoundPredicate();
        compoundPredicate.setBooleanOperator(operator);
        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);
        assertEquals(1, rules.size());
        final KiePMMLDroolsRule retrieved = rules.get(0);
        assertNotNull(retrieved);
        assertEquals(currentRule, retrieved.getName());
        assertEquals(currentRule, retrieved.getStatusToSet());
        assertEquals(String.format(STATUS_PATTERN, parentPath), retrieved.getStatusConstraint());
        switch(compoundPredicate.getBooleanOperator()) {
            case AND:
                assertNotNull(retrieved.getAndConstraints());
                break;
            case OR:
                assertNotNull(retrieved.getOrConstraints());
                break;
            case XOR:
                assertNotNull(retrieved.getXorConstraints());
                break;
            default:
        }
    }
}
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) Test(org.junit.Test)

Aggregations

SimplePredicate (org.dmg.pmml.SimplePredicate)30 Test (org.junit.Test)17 CompoundPredicate (org.dmg.pmml.CompoundPredicate)15 ArrayList (java.util.ArrayList)11 KiePMMLDroolsRule (org.kie.pmml.models.drools.ast.KiePMMLDroolsRule)11 HashMap (java.util.HashMap)10 List (java.util.List)10 KiePMMLOriginalTypeGeneratedType (org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType)10 Collectors (java.util.stream.Collectors)9 SimpleSetPredicate (org.dmg.pmml.SimpleSetPredicate)9 Predicate (org.dmg.pmml.Predicate)8 KiePMMLSimplePredicate (org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate)7 KiePMMLFieldOperatorValue (org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue)7 DataField (org.dmg.pmml.DataField)6 DataType (org.dmg.pmml.DataType)6 KiePMMLASTTestUtils.getPredicateASTFactoryData (org.kie.pmml.models.drools.utils.KiePMMLASTTestUtils.getPredicateASTFactoryData)6 Map (java.util.Map)5 Assert.assertTrue (org.junit.Assert.assertTrue)5 KiePMMLCompoundPredicate (org.kie.pmml.commons.model.predicates.KiePMMLCompoundPredicate)5 KiePMMLSimpleSetPredicate (org.kie.pmml.commons.model.predicates.KiePMMLSimpleSetPredicate)5