Search in sources :

Example 11 with KiePMMLFieldOperatorValue

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

the class KiePMMLASTFactoryUtilsTest method getConstraintEntriesFromXOrCompoundPredicate.

@Test
public void getConstraintEntriesFromXOrCompoundPredicate() {
    CompoundPredicate compoundPredicate = new CompoundPredicate();
    compoundPredicate.setBooleanOperator(CompoundPredicate.BooleanOperator.XOR);
    List<Predicate> predicates = IntStream.range(0, 2).mapToObj(index -> simplePredicates.get(index)).collect(Collectors.toList());
    compoundPredicate.getPredicates().addAll(predicates);
    List<KiePMMLFieldOperatorValue> retrieved = KiePMMLASTFactoryUtils.getConstraintEntriesFromXOrCompoundPredicate(compoundPredicate, fieldTypeMap);
    assertNotNull(retrieved);
    assertEquals(predicates.size(), retrieved.size());
    commonVerifyKiePMMLFieldOperatorValueList(retrieved, null);
}
Also used : IntStream(java.util.stream.IntStream) Predicate(org.dmg.pmml.Predicate) BeforeClass(org.junit.BeforeClass) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PMMLModelTestUtils(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils) KiePMMLOperatorValue(org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue) Map(java.util.Map) CompoundPredicate(org.dmg.pmml.CompoundPredicate) Assert.assertNotNull(org.junit.Assert.assertNotNull) DataType(org.dmg.pmml.DataType) BOOLEAN_OPERATOR(org.kie.pmml.api.enums.BOOLEAN_OPERATOR) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) OPERATOR(org.kie.pmml.api.enums.OPERATOR) Consumer(java.util.function.Consumer) List(java.util.List) SimplePredicate(org.dmg.pmml.SimplePredicate) Assert.assertFalse(org.junit.Assert.assertFalse) PMMLModelTestUtils.getRandomSimplePredicateOperator(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomSimplePredicateOperator) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) Assert.assertEquals(org.junit.Assert.assertEquals) KiePMMLModelUtils.getSanitizedClassName(org.kie.pmml.commons.utils.KiePMMLModelUtils.getSanitizedClassName) PMMLModelTestUtils.getRandomObject(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomObject) CompoundPredicate(org.dmg.pmml.CompoundPredicate) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) Predicate(org.dmg.pmml.Predicate) CompoundPredicate(org.dmg.pmml.CompoundPredicate) SimplePredicate(org.dmg.pmml.SimplePredicate) Test(org.junit.Test)

Example 12 with KiePMMLFieldOperatorValue

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

the class KiePMMLASTFactoryUtilsTest method getConstraintEntryFromSimplePredicates.

@Test
public void getConstraintEntryFromSimplePredicates() {
    final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
    String fieldName = "FIELD_NAME";
    List<SimplePredicate> simplePredicates = IntStream.range(0, 2).mapToObj(index -> {
        fieldTypeMap.put(fieldName, new KiePMMLOriginalTypeGeneratedType(DataType.STRING.value(), getSanitizedClassName(fieldName.toUpperCase())));
        return PMMLModelTestUtils.getSimplePredicate(fieldName, "VALUE-" + index, SimplePredicate.Operator.LESS_THAN);
    }).collect(Collectors.toList());
    final KiePMMLFieldOperatorValue retrieved = KiePMMLASTFactoryUtils.getConstraintEntryFromSimplePredicates(fieldName, BOOLEAN_OPERATOR.OR, simplePredicates, fieldTypeMap);
    assertEquals(fieldName, retrieved.getName());
    assertNotNull(retrieved.getConstraintsAsString());
    String expected = "value < \"VALUE-0\" || value < \"VALUE-1\"";
    assertEquals(expected, retrieved.getConstraintsAsString());
}
Also used : IntStream(java.util.stream.IntStream) Predicate(org.dmg.pmml.Predicate) BeforeClass(org.junit.BeforeClass) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PMMLModelTestUtils(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils) KiePMMLOperatorValue(org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue) Map(java.util.Map) CompoundPredicate(org.dmg.pmml.CompoundPredicate) Assert.assertNotNull(org.junit.Assert.assertNotNull) DataType(org.dmg.pmml.DataType) BOOLEAN_OPERATOR(org.kie.pmml.api.enums.BOOLEAN_OPERATOR) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) OPERATOR(org.kie.pmml.api.enums.OPERATOR) Consumer(java.util.function.Consumer) List(java.util.List) SimplePredicate(org.dmg.pmml.SimplePredicate) Assert.assertFalse(org.junit.Assert.assertFalse) PMMLModelTestUtils.getRandomSimplePredicateOperator(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomSimplePredicateOperator) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) Assert.assertEquals(org.junit.Assert.assertEquals) KiePMMLModelUtils.getSanitizedClassName(org.kie.pmml.commons.utils.KiePMMLModelUtils.getSanitizedClassName) PMMLModelTestUtils.getRandomObject(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomObject) HashMap(java.util.HashMap) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) SimplePredicate(org.dmg.pmml.SimplePredicate) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Test(org.junit.Test)

Example 13 with KiePMMLFieldOperatorValue

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

the class KiePMMLASTFactoryUtils method populateKiePMMLFieldOperatorValueListWithCompoundPredicates.

/**
 * Populate the given <code>List&lt;KiePMMLFieldOperatorValue&gt;</code> with <code>KiePMMLFieldOperatorValue</code>s generated from the given <b>compoundPredicates</b>
 * @param toPopulate
 * @param compoundPredicates
 * @param fieldTypeMap
 */
static void populateKiePMMLFieldOperatorValueListWithCompoundPredicates(final List<KiePMMLFieldOperatorValue> toPopulate, final List<CompoundPredicate> compoundPredicates, final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap) {
    final List<KiePMMLFieldOperatorValue> nestedAndPredicates = new LinkedList<>();
    final List<KiePMMLFieldOperatorValue> nestedOrPredicates = new LinkedList<>();
    compoundPredicates.forEach(nestedCompoundPredicate -> {
        switch((nestedCompoundPredicate).getBooleanOperator()) {
            case OR:
                nestedOrPredicates.addAll(getConstraintEntriesFromAndOrCompoundPredicate(nestedCompoundPredicate, fieldTypeMap));
                break;
            case AND:
                nestedAndPredicates.addAll(getConstraintEntriesFromAndOrCompoundPredicate(nestedCompoundPredicate, fieldTypeMap));
                break;
            default:
                throw new IllegalStateException(String.format("Unmanaged CompoundPredicate.booleanOperator %s at populateKiePMMLFieldOperatorValueListWithCompoundPredicates", nestedCompoundPredicate.getBooleanOperator()));
        }
    });
    if (!nestedAndPredicates.isEmpty()) {
        toPopulate.add(new KiePMMLFieldOperatorValue(null, BOOLEAN_OPERATOR.AND, Collections.emptyList(), nestedAndPredicates));
    }
    if (!nestedOrPredicates.isEmpty()) {
        toPopulate.add(new KiePMMLFieldOperatorValue(null, BOOLEAN_OPERATOR.OR, Collections.emptyList(), nestedOrPredicates));
    }
}
Also used : KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) LinkedList(java.util.LinkedList)

Example 14 with KiePMMLFieldOperatorValue

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

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

the class KiePMMLDescrLhsFactoryTest method declareConstraintAnd.

@Test
public void declareConstraintAnd() {
    String temperatureField = "TEMPERATURE";
    String humidityField = "HUMIDITY";
    List<KiePMMLFieldOperatorValue> kiePMMLOperatorValues = Arrays.asList(new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.AND, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.LESS_THAN, 35)), null), new KiePMMLFieldOperatorValue(humidityField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.GREATER_THAN, 85)), null));
    KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintsAndOr(kiePMMLOperatorValues, lhsBuilder.and());
    assertNotNull(lhsBuilder.getDescr());
    assertEquals(1, lhsBuilder.getDescr().getDescrs().size());
    assertTrue(lhsBuilder.getDescr().getDescrs().get(0) instanceof AndDescr);
    AndDescr baseAndDescr = (AndDescr) lhsBuilder.getDescr().getDescrs().get(0);
    final List<BaseDescr> descrs = baseAndDescr.getDescrs();
    assertNotNull(descrs);
    assertEquals(2, descrs.size());
    // First KiePMMLFieldOperatorValue
    assertTrue(descrs.get(0) instanceof PatternDescr);
    PatternDescr patternDescr = (PatternDescr) descrs.get(0);
    assertEquals(temperatureField, patternDescr.getObjectType());
    assertNull(patternDescr.getIdentifier());
    assertTrue(patternDescr.getConstraint() instanceof AndDescr);
    AndDescr andDescr = (AndDescr) patternDescr.getConstraint();
    assertEquals(1, andDescr.getDescrs().size());
    assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
    ExprConstraintDescr exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
    assertFalse(exprConstraintDescr.isNegated());
    assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
    String expected = "value < 35";
    assertEquals(expected, exprConstraintDescr.getExpression());
    // Second KiePMMLFieldOperatorValue
    assertTrue(descrs.get(1) instanceof PatternDescr);
    patternDescr = (PatternDescr) descrs.get(1);
    assertEquals(humidityField, patternDescr.getObjectType());
    assertNull(patternDescr.getIdentifier());
    assertTrue(patternDescr.getConstraint() instanceof AndDescr);
    andDescr = (AndDescr) patternDescr.getConstraint();
    assertEquals(1, andDescr.getDescrs().size());
    assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
    exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
    assertFalse(exprConstraintDescr.isNegated());
    assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
    expected = "value > 85";
    assertEquals(expected, exprConstraintDescr.getExpression());
}
Also used : KiePMMLOperatorValue(org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) BaseDescr(org.drools.drl.ast.descr.BaseDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Aggregations

KiePMMLFieldOperatorValue (org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue)17 Test (org.junit.Test)14 KiePMMLOperatorValue (org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue)11 ArrayList (java.util.ArrayList)8 SimplePredicate (org.dmg.pmml.SimplePredicate)8 HashMap (java.util.HashMap)7 KiePMMLDroolsRule (org.kie.pmml.models.drools.ast.KiePMMLDroolsRule)7 KiePMMLOriginalTypeGeneratedType (org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType)7 CompoundPredicate (org.dmg.pmml.CompoundPredicate)4 AndDescr (org.drools.drl.ast.descr.AndDescr)4 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)4 PatternDescr (org.drools.drl.ast.descr.PatternDescr)4 BOOLEAN_OPERATOR (org.kie.pmml.api.enums.BOOLEAN_OPERATOR)4 OPERATOR (org.kie.pmml.api.enums.OPERATOR)4 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)4 KiePMMLASTTestUtils.getPredicateASTFactoryData (org.kie.pmml.models.drools.utils.KiePMMLASTTestUtils.getPredicateASTFactoryData)4 List (java.util.List)3 Map (java.util.Map)3 Consumer (java.util.function.Consumer)3 Collectors (java.util.stream.Collectors)3