Search in sources :

Example 41 with KiePMMLOriginalTypeGeneratedType

use of org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType 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 42 with KiePMMLOriginalTypeGeneratedType

use of org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType in project drools by kiegroup.

the class KiePMMLDerivedFieldASTFactoryTest method commonValidateKiePMMLDroolsType.

private void commonValidateKiePMMLDroolsType(KiePMMLDroolsType toValidate, DerivedField derivedField) {
    String derivedFieldName = derivedField.getName().getValue();
    String expectedName = getSanitizedClassName(derivedFieldName.toUpperCase());
    assertEquals(expectedName, toValidate.getName());
    String expectedType = DATA_TYPE.byName(derivedField.getDataType().value()).getMappedClass().getSimpleName();
    assertEquals(expectedType, toValidate.getType());
    assertTrue(fieldTypeMap.containsKey(derivedFieldName));
    KiePMMLOriginalTypeGeneratedType retrieved = fieldTypeMap.get(derivedFieldName);
    assertEquals(derivedField.getDataType().value(), retrieved.getOriginalType());
    assertEquals(expectedName, retrieved.getGeneratedType());
}
Also used : KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType)

Example 43 with KiePMMLOriginalTypeGeneratedType

use of org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType in project drools by kiegroup.

the class KiePMMLTreeModelASTFactoryTest method getKiePMMLDroolsGolfingAST.

@Test
public void getKiePMMLDroolsGolfingAST() {
    final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = getFieldTypeMap(golfingPmml.getDataDictionary(), golfingPmml.getTransformationDictionary(), golfingModel.getLocalTransformations());
    List<KiePMMLDroolsType> types = Collections.emptyList();
    KiePMMLDroolsAST retrieved = KiePMMLTreeModelASTFactory.getKiePMMLDroolsAST(getFieldsFromDataDictionary(golfingPmml.getDataDictionary()), golfingModel, fieldTypeMap, types);
    assertNotNull(retrieved);
    assertEquals(types, retrieved.getTypes());
    assertFalse(retrieved.getRules().isEmpty());
}
Also used : KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) KiePMMLDroolsType(org.kie.pmml.models.drools.ast.KiePMMLDroolsType) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Test(org.junit.Test)

Example 44 with KiePMMLOriginalTypeGeneratedType

use of org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType in project drools by kiegroup.

the class KiePMMLTreeModelASTFactoryTest method getKiePMMLDroolsIrisAST.

@Test
public void getKiePMMLDroolsIrisAST() {
    final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = getFieldTypeMap(irisPmml.getDataDictionary(), irisPmml.getTransformationDictionary(), irisModel.getLocalTransformations());
    List<KiePMMLDroolsType> types = Collections.emptyList();
    KiePMMLDroolsAST retrieved = KiePMMLTreeModelASTFactory.getKiePMMLDroolsAST(getFieldsFromDataDictionary(irisPmml.getDataDictionary()), irisModel, fieldTypeMap, types);
    assertNotNull(retrieved);
    assertEquals(types, retrieved.getTypes());
    assertFalse(retrieved.getRules().isEmpty());
}
Also used : KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) KiePMMLDroolsType(org.kie.pmml.models.drools.ast.KiePMMLDroolsType) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Test(org.junit.Test)

Example 45 with KiePMMLOriginalTypeGeneratedType

use of org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType in project drools by kiegroup.

the class KiePMMLTreeModelNodeASTFactoryTest method declareRulesFromRootIrisNode.

@Test
public void declareRulesFromRootIrisNode() {
    Node rootNode = irisModel.getNode();
    assertEquals("setosa", rootNode.getScore());
    final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
    final List<Field<?>> fields = getFieldsFromDataDictionary(irisPmml.getDataDictionary());
    DATA_TYPE targetType = getTargetFieldType(fields, irisModel);
    KiePMMLDataDictionaryASTFactory.factory(fieldTypeMap).declareTypes(fields);
    KiePMMLTreeModelNodeASTFactory.factory(fieldTypeMap, Collections.emptyList(), TreeModel.NoTrueChildStrategy.RETURN_NULL_PREDICTION, targetType).declareRulesFromRootNode(rootNode, "_setosa");
    assertFalse(fieldTypeMap.isEmpty());
}
Also used : Field(org.dmg.pmml.Field) HashMap(java.util.HashMap) LeafNode(org.dmg.pmml.tree.LeafNode) Node(org.dmg.pmml.tree.Node) ClassifierNode(org.dmg.pmml.tree.ClassifierNode) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Test(org.junit.Test)

Aggregations

KiePMMLOriginalTypeGeneratedType (org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType)45 Test (org.junit.Test)32 HashMap (java.util.HashMap)29 ArrayList (java.util.ArrayList)16 KiePMMLDroolsRule (org.kie.pmml.models.drools.ast.KiePMMLDroolsRule)14 KiePMMLASTTestUtils.getPredicateASTFactoryData (org.kie.pmml.models.drools.utils.KiePMMLASTTestUtils.getPredicateASTFactoryData)12 KiePMMLDroolsAST (org.kie.pmml.models.drools.ast.KiePMMLDroolsAST)11 SimplePredicate (org.dmg.pmml.SimplePredicate)9 KiePMMLDroolsType (org.kie.pmml.models.drools.ast.KiePMMLDroolsType)9 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)8 Field (org.dmg.pmml.Field)7 DATA_TYPE (org.kie.pmml.api.enums.DATA_TYPE)7 List (java.util.List)6 CompoundPredicate (org.dmg.pmml.CompoundPredicate)6 DataDictionary (org.dmg.pmml.DataDictionary)6 Map (java.util.Map)5 DataField (org.dmg.pmml.DataField)5 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)5 CommonTestingUtils.getFieldsFromDataDictionary (org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary)5 KiePMMLFieldOperatorValue (org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue)5