Search in sources :

Example 16 with KiePMMLDroolsRule

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

the class KiePMMLDescrRhsFactoryTest method declareDefaultThenWithoutResult.

@Test
public void declareDefaultThenWithoutResult() {
    String name = "NAME";
    String statusToSet = "STATUS_TO_SET";
    KiePMMLDroolsRule rule = KiePMMLDroolsRule.builder(name, statusToSet, Collections.emptyList()).build();
    KiePMMLDescrRhsFactory.factory(ruleBuilder).declareDefaultThen(rule);
    assertNotNull(ruleBuilder.getDescr().getConsequence());
    String expectedConsequence = String.format(UPDATE_STATUS_HOLDER_STATUS, statusToSet);
    assertTrue(ruleBuilder.getDescr().getConsequence().toString().contains(expectedConsequence));
    assertNotNull(ruleBuilder.getDescr().getNamedConsequences());
    assertTrue(ruleBuilder.getDescr().getNamedConsequences().isEmpty());
}
Also used : KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) Test(org.junit.Test)

Example 17 with KiePMMLDroolsRule

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

the class KiePMMLDescrRhsFactoryTest method commonDeclareThen.

@Test
public void commonDeclareThen() {
    String ruleName = "RULENAME";
    String statusToSet = "STATUSTOSET";
    String outputFieldName = "OUTPUTFIELDNAME";
    Object result = "RESULT";
    OutputField outputField = new OutputField();
    outputField.setName(FieldName.create(outputFieldName));
    outputField.setResultFeature(ResultFeature.PREDICTED_VALUE);
    List<OutputField> outputFields = Collections.singletonList(outputField);
    KiePMMLDroolsRule.Builder builder = KiePMMLDroolsRule.builder(ruleName, statusToSet, outputFields);
    KiePMMLDroolsRule rule = builder.build();
    StringJoiner joiner = new StringJoiner("");
    KiePMMLDescrRhsFactory.factory(ruleBuilder).commonDeclareThen(rule, joiner);
    String retrieved = joiner.toString();
    assertTrue(retrieved.isEmpty());
    // 
    ResultCode resultCode = ResultCode.OK;
    builder = builder.withResultCode(resultCode);
    rule = builder.build();
    joiner = new StringJoiner("");
    KiePMMLDescrRhsFactory.factory(ruleBuilder).commonDeclareThen(rule, joiner);
    retrieved = joiner.toString();
    String expected = String.format(SET_PMML4_RESULT_CODE, resultCode);
    assertTrue(retrieved.contains(expected));
    // 
    builder = builder.withResult(result);
    rule = builder.build();
    joiner = new StringJoiner("");
    KiePMMLDescrRhsFactory.factory(ruleBuilder).commonDeclareThen(rule, joiner);
    retrieved = joiner.toString();
    expected = String.format(ADD_PMML4_RESULT_VARIABLE, result);
    assertTrue(retrieved.contains(expected));
    expected = String.format(ADD_PMML4_OUTPUT_FIELD, outputFieldName, result);
    assertTrue(retrieved.contains(expected));
    // 
    String focusedAgendaGroup = "FOCUSEDAGENDAGROUP";
    builder = builder.withFocusedAgendaGroup(focusedAgendaGroup);
    rule = builder.build();
    joiner = new StringJoiner("");
    KiePMMLDescrRhsFactory.factory(ruleBuilder).commonDeclareThen(rule, joiner);
    retrieved = joiner.toString();
    expected = String.format(FOCUS_AGENDA_GROUP, focusedAgendaGroup);
    assertTrue(retrieved.contains(expected));
// 
}
Also used : OutputField(org.dmg.pmml.OutputField) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) StringJoiner(java.util.StringJoiner) ResultCode(org.kie.pmml.api.enums.ResultCode) Test(org.junit.Test)

Example 18 with KiePMMLDroolsRule

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

the class KiePMMLDescrRhsFactoryTest method declareRhsWithIfBreak.

@Test
public void declareRhsWithIfBreak() {
    String name = "NAME";
    String statusToSet = "STATUS_TO_SET";
    String ifBreakField = "ifBreakField";
    String ifBreakOperator = "ifBreakOperator";
    Object ifBreakValue = "ifBreakValue";
    KiePMMLDroolsRule rule = KiePMMLDroolsRule.builder(name, statusToSet, Collections.emptyList()).withIfBreak(ifBreakField, ifBreakOperator, ifBreakValue).build();
    KiePMMLDescrRhsFactory.factory(ruleBuilder).declareRhs(rule);
    assertNotNull(ruleBuilder.getDescr().getConsequence());
    String expectedConsequence = String.format(UPDATE_STATUS_HOLDER_STATUS, statusToSet);
    assertTrue(ruleBuilder.getDescr().getConsequence().toString().contains(expectedConsequence));
    assertNotNull(ruleBuilder.getDescr().getNamedConsequences());
    assertEquals(1, ruleBuilder.getDescr().getNamedConsequences().size());
    assertNotNull(ruleBuilder.getDescr().getNamedConsequences().get(BREAK_LABEL));
}
Also used : KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) Test(org.junit.Test)

Example 19 with KiePMMLDroolsRule

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

the class KiePMMLDescrRhsFactoryTest method declareIfThenWithoutResult.

@Test
public void declareIfThenWithoutResult() {
    String name = "NAME";
    String statusToSet = "STATUS_TO_SET";
    KiePMMLDroolsRule rule = KiePMMLDroolsRule.builder(name, statusToSet, Collections.emptyList()).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));
    expectedConsequence = String.format(UPDATE_STATUS_HOLDER_STATUS, DONE);
    assertTrue(expectedConsequence, ruleBuilder.getDescr().getNamedConsequences().get(BREAK_LABEL).toString().contains(expectedConsequence));
}
Also used : KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) Test(org.junit.Test)

Example 20 with KiePMMLDroolsRule

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

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