Search in sources :

Example 31 with KiePMMLDroolsRule

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

the class KiePMMLDescrRhsFactoryTest method declareIfThenWithResult.

@Test
public void declareIfThenWithResult() {
    String name = "NAME";
    String statusToSet = "STATUS_TO_SET";
    String result = "RESULT";
    ResultCode resultCode = ResultCode.OK;
    KiePMMLDroolsRule rule = KiePMMLDroolsRule.builder(name, statusToSet, Collections.emptyList()).withResultCode(resultCode).withResult(result).build();
    KiePMMLDescrRhsFactory.factory(ruleBuilder).declareIfThen(rule);
    assertNotNull(ruleBuilder.getDescr().getConsequence());
    String expectedConsequence = String.format(UPDATE_STATUS_HOLDER_STATUS, statusToSet);
    assertEquals(expectedConsequence, ruleBuilder.getDescr().getConsequence());
    assertNotNull(ruleBuilder.getDescr().getNamedConsequences());
    assertEquals(1, ruleBuilder.getDescr().getNamedConsequences().size());
    assertNotNull(ruleBuilder.getDescr().getNamedConsequences().get(BREAK_LABEL));
    String retrievedConsequence = ruleBuilder.getDescr().getNamedConsequences().get(BREAK_LABEL).toString();
    assertTrue(retrievedConsequence.contains(String.format(UPDATE_STATUS_HOLDER_STATUS, DONE)));
    assertTrue(retrievedConsequence.contains(String.format(SET_PMML4_RESULT_CODE, resultCode)));
    assertTrue(retrievedConsequence.contains(String.format(ADD_PMML4_RESULT_VARIABLE, result)));
}
Also used : KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) ResultCode(org.kie.pmml.api.enums.ResultCode) Test(org.junit.Test)

Example 32 with KiePMMLDroolsRule

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

the class KiePMMLDescrRhsFactoryTest method declareDefaultThenWithResult.

@Test
public void declareDefaultThenWithResult() {
    String name = "NAME";
    String statusToSet = "STATUS_TO_SET";
    String result = "RESULT";
    ResultCode resultCode = ResultCode.OK;
    KiePMMLDroolsRule rule = KiePMMLDroolsRule.builder(name, statusToSet, Collections.emptyList()).withResultCode(resultCode).withResult(result).build();
    KiePMMLDescrRhsFactory.factory(ruleBuilder).declareDefaultThen(rule);
    assertNotNull(ruleBuilder.getDescr().getConsequence());
    String retrievedConsequence = ruleBuilder.getDescr().getConsequence().toString();
    assertTrue(retrievedConsequence.contains(String.format(UPDATE_STATUS_HOLDER_STATUS, statusToSet)));
    assertTrue(retrievedConsequence.contains(String.format(SET_PMML4_RESULT_CODE, resultCode)));
    assertTrue(retrievedConsequence.contains(String.format(ADD_PMML4_RESULT_VARIABLE, result)));
    assertNotNull(ruleBuilder.getDescr().getNamedConsequences());
    assertTrue(ruleBuilder.getDescr().getNamedConsequences().isEmpty());
}
Also used : KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) ResultCode(org.kie.pmml.api.enums.ResultCode) Test(org.junit.Test)

Example 33 with KiePMMLDroolsRule

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

Example 34 with KiePMMLDroolsRule

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

the class KiePMMLScorecardModelCharacteristicASTFactoryTest method declareRulesFromCharacteristics.

@Test
public void declareRulesFromCharacteristics() {
    Characteristics characteristics = scorecardModel.getCharacteristics();
    String parentPath = "_will";
    List<KiePMMLDroolsRule> retrieved = getKiePMMLScorecardModelCharacteristicASTFactory().declareRulesFromCharacteristics(characteristics, parentPath, null);
    final List<Characteristic> characteristicList = characteristics.getCharacteristics();
    List<Attribute> attributes = new ArrayList<>();
    AtomicInteger counter = new AtomicInteger(0);
    for (int i = 0; i < characteristicList.size(); i++) {
        Characteristic characteristic = characteristicList.get(i);
        attributes.addAll(characteristic.getAttributes());
        for (int j = 0; j < characteristic.getAttributes().size(); j++) {
            Attribute attribute = characteristic.getAttributes().get(j);
            KiePMMLDroolsRule rule = retrieved.get(counter.incrementAndGet());
            int expectedOperatorValuesSize = 1;
            Integer expectedAndConstraints = null;
            Integer expectedInConstraints = null;
            BOOLEAN_OPERATOR expectedOperator = BOOLEAN_OPERATOR.AND;
            if (attribute.getPredicate() instanceof SimplePredicate) {
                expectedAndConstraints = 1;
            }
            if (attribute.getPredicate() instanceof CompoundPredicate) {
                expectedOperatorValuesSize = ((CompoundPredicate) attribute.getPredicate()).getPredicates().size();
                expectedAndConstraints = 1;
            }
            if (attribute.getPredicate() instanceof SimpleSetPredicate) {
                expectedInConstraints = 1;
            }
            boolean isLastCharacteristic = (i == characteristicList.size() - 1);
            String statusToSet = isLastCharacteristic ? DONE : String.format(PATH_PATTERN, parentPath, characteristicList.get(i + 1).getName());
            commonValidateRule(rule, attribute, statusToSet, parentPath + "_" + characteristic.getName(), j, isLastCharacteristic, expectedAndConstraints, expectedInConstraints, expectedOperator, null, expectedOperatorValuesSize);
        }
    }
    assertEquals(attributes.size() + 1, retrieved.size());
}
Also used : Attribute(org.dmg.pmml.scorecard.Attribute) Characteristic(org.dmg.pmml.scorecard.Characteristic) ArrayList(java.util.ArrayList) BOOLEAN_OPERATOR(org.kie.pmml.api.enums.BOOLEAN_OPERATOR) SimplePredicate(org.dmg.pmml.SimplePredicate) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Characteristics(org.dmg.pmml.scorecard.Characteristics) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) CompoundPredicate(org.dmg.pmml.CompoundPredicate) Test(org.junit.Test)

Example 35 with KiePMMLDroolsRule

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

the class KiePMMLScorecardModelCharacteristicASTFactoryTest method declareRuleFromAttributeWithSimplePredicateLastCharacteristic.

@Test
public void declareRuleFromAttributeWithSimplePredicateLastCharacteristic() {
    Attribute attribute = getSimplePredicateAttribute();
    final String parentPath = "parent_path";
    final int attributeIndex = 2;
    final List<KiePMMLDroolsRule> rules = new ArrayList<>();
    final String statusToSet = "status_to_set";
    final String characteristicReasonCode = "REASON_CODE";
    final double characteristicBaselineScore = 12;
    final boolean isLastCharacteristic = true;
    getKiePMMLScorecardModelCharacteristicASTFactory().declareRuleFromAttribute(attribute, parentPath, attributeIndex, rules, statusToSet, characteristicReasonCode, characteristicBaselineScore, isLastCharacteristic);
    assertEquals(1, rules.size());
    commonValidateRule(rules.get(0), attribute, statusToSet, parentPath, attributeIndex, isLastCharacteristic, 1, null, BOOLEAN_OPERATOR.AND, "value <= 5.0", 1);
}
Also used : Attribute(org.dmg.pmml.scorecard.Attribute) ArrayList(java.util.ArrayList) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) 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