Search in sources :

Example 21 with KiePMMLDroolsRule

use of org.kie.pmml.models.drools.ast.KiePMMLDroolsRule 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)

Example 22 with KiePMMLDroolsRule

use of org.kie.pmml.models.drools.ast.KiePMMLDroolsRule in project drools by kiegroup.

the class KiePMMLCompoundPredicateASTFactoryTest method declareRuleFromCompoundPredicateAndOrXorFinalLeaf.

@Test
public void declareRuleFromCompoundPredicateAndOrXorFinalLeaf() {
    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, true);
        assertEquals(1, rules.size());
        final KiePMMLDroolsRule retrieved = rules.get(0);
        assertNotNull(retrieved);
        assertEquals(currentRule, retrieved.getName());
        assertEquals(DONE, retrieved.getStatusToSet());
        assertEquals(String.format(STATUS_PATTERN, parentPath), retrieved.getStatusConstraint());
        assertEquals(result, retrieved.getResult());
        assertEquals(ResultCode.OK, retrieved.getResultCode());
        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)

Example 23 with KiePMMLDroolsRule

use of org.kie.pmml.models.drools.ast.KiePMMLDroolsRule in project drools by kiegroup.

the class KiePMMLScorecardModelASTFactory method getKiePMMLDroolsAST.

/**
 * Returns the <code>KiePMMLDroolsAST</code> built out of the given parameters.
 * It also <b>populate</b> the <b>fieldNameTypeNameMap</b> with mapping between
 * original field' name and <b>original type/generated type</b> tupla
 *
 * @param fields
 * @param model
 * @param fieldTypeMap
 * @param types
 * @return
 */
public static KiePMMLDroolsAST getKiePMMLDroolsAST(final List<Field<?>> fields, final Scorecard model, final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap, final List<KiePMMLDroolsType> types) {
    logger.trace("getKiePMMLDroolsAST {} {} {}", fields, model, fieldTypeMap);
    DATA_TYPE targetType = getTargetFieldType(fields, model);
    List<OutputField> outputFields = model.getOutput() != null ? model.getOutput().getOutputFields() : Collections.emptyList();
    KiePMMLScorecardModelCharacteristicASTFactory factory = KiePMMLScorecardModelCharacteristicASTFactory.factory(fieldTypeMap, outputFields, targetType);
    if (model.isUseReasonCodes()) {
        factory = factory.withReasonCodes(model.getBaselineScore(), REASONCODE_ALGORITHM.byName(model.getReasonCodeAlgorithm().value()));
    }
    final List<KiePMMLDroolsRule> rules = factory.declareRulesFromCharacteristics(model.getCharacteristics(), "", model.getInitialScore());
    return new KiePMMLDroolsAST(types, rules);
}
Also used : KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) OutputField(org.dmg.pmml.OutputField) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE)

Example 24 with KiePMMLDroolsRule

use of org.kie.pmml.models.drools.ast.KiePMMLDroolsRule in project drools by kiegroup.

the class KiePMMLSimplePredicateASTFactoryTest method declareRuleFromSimplePredicateSurrogateNotFinalLeaf.

@Test
public void declareRuleFromSimplePredicateSurrogateNotFinalLeaf() {
    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, false);
    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(currentRule, retrieved.getStatusToSet());
    assertNull(retrieved.getStatusConstraint());
    assertEquals(agendaActivationGroup, retrieved.getAgendaGroup());
    assertEquals(agendaActivationGroup, retrieved.getActivationGroup());
    assertNull(retrieved.getIfBreakField());
    assertNull(retrieved.getIfBreakOperator());
    assertNull(retrieved.getIfBreakValue());
    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());
    assertNull(retrieved.getResult());
    assertNull(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 25 with KiePMMLDroolsRule

use of org.kie.pmml.models.drools.ast.KiePMMLDroolsRule in project drools by kiegroup.

the class KiePMMLTruePredicateASTFactoryTest method declareRuleFromTruePredicateNotFinalLeaf.

@Test
public void declareRuleFromTruePredicateNotFinalLeaf() {
    String parentPath = "_will play";
    String currentRule = "_will play_will play";
    final List<KiePMMLDroolsRule> rules = new ArrayList<>();
    True truePredicate = new True();
    PredicateASTFactoryData predicateASTFactoryData = getPredicateASTFactoryData(truePredicate, Collections.emptyList(), rules, parentPath, currentRule, Collections.emptyMap());
    KiePMMLTruePredicateASTFactory.factory(predicateASTFactoryData).declareRuleFromTruePredicateWithResult(DONE, 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());
    assertNull(retrieved.getAndConstraints());
    assertNull(retrieved.getResultCode());
}
Also used : ArrayList(java.util.ArrayList) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) True(org.dmg.pmml.True) KiePMMLASTTestUtils.getPredicateASTFactoryData(org.kie.pmml.models.drools.utils.KiePMMLASTTestUtils.getPredicateASTFactoryData) Test(org.junit.Test)

Aggregations

KiePMMLDroolsRule (org.kie.pmml.models.drools.ast.KiePMMLDroolsRule)36 Test (org.junit.Test)33 ArrayList (java.util.ArrayList)25 HashMap (java.util.HashMap)14 KiePMMLOriginalTypeGeneratedType (org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType)14 KiePMMLASTTestUtils.getPredicateASTFactoryData (org.kie.pmml.models.drools.utils.KiePMMLASTTestUtils.getPredicateASTFactoryData)14 SimplePredicate (org.dmg.pmml.SimplePredicate)9 Attribute (org.dmg.pmml.scorecard.Attribute)6 KiePMMLFieldOperatorValue (org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue)6 CompoundPredicate (org.dmg.pmml.CompoundPredicate)5 SimpleSetPredicate (org.dmg.pmml.SimpleSetPredicate)5 List (java.util.List)4 DATA_TYPE (org.kie.pmml.api.enums.DATA_TYPE)4 OutputField (org.dmg.pmml.OutputField)3 Characteristic (org.dmg.pmml.scorecard.Characteristic)3 ResultCode (org.kie.pmml.api.enums.ResultCode)3 KiePMMLDroolsAST (org.kie.pmml.models.drools.ast.KiePMMLDroolsAST)3 Map (java.util.Map)2 Field (org.dmg.pmml.Field)2 True (org.dmg.pmml.True)2