Search in sources :

Example 11 with SimpleSetPredicate

use of org.dmg.pmml.SimpleSetPredicate in project drools by kiegroup.

the class KiePMMLSimpleSetPredicateASTFactoryTest method declareRuleFromSimpleSetPredicateIsInFinalLeaf.

@Test
public void declareRuleFromSimpleSetPredicateIsInFinalLeaf() {
    final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
    List<String> values = Arrays.asList("-5", "0.5", "1", "10");
    final SimpleSetPredicate simpleSetPredicate = getSimpleSetPredicate("input1", values, SimpleSetPredicate.BooleanOperator.IS_IN, fieldTypeMap);
    String parentPath = "_classA";
    String currentRule = "_classA_classB";
    String result = "classB";
    String declaredType = fieldTypeMap.get("input1").getGeneratedType();
    final List<KiePMMLDroolsRule> rules = new ArrayList<>();
    PredicateASTFactoryData predicateASTFactoryData = getPredicateASTFactoryData(simpleSetPredicate, Collections.emptyList(), rules, parentPath, currentRule, fieldTypeMap);
    KiePMMLSimpleSetPredicateASTFactory.factory(predicateASTFactoryData).declareRuleFromSimpleSetPredicate(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(ResultCode.OK, retrieved.getResultCode());
    assertEquals(result, retrieved.getResult());
    assertNotNull(retrieved.getInConstraints());
    final Map<String, List<Object>> inConstraints = retrieved.getInConstraints();
    assertEquals(1, inConstraints.size());
    assertTrue(inConstraints.containsKey(declaredType));
    final List<Object> retrievedValues = inConstraints.get(declaredType);
    List<String> originalPredicateValues = Arrays.asList(((String) simpleSetPredicate.getArray().getValue()).split(" "));
    assertEquals(originalPredicateValues.size(), retrievedValues.size());
    retrievedValues.forEach(retrievedValue -> {
        assertTrue(originalPredicateValues.contains(retrievedValue));
    });
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KiePMMLASTTestUtils.getPredicateASTFactoryData(org.kie.pmml.models.drools.utils.KiePMMLASTTestUtils.getPredicateASTFactoryData) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 12 with SimpleSetPredicate

use of org.dmg.pmml.SimpleSetPredicate in project drools by kiegroup.

the class KiePMMLSimpleSetPredicateASTFactoryTest method declareRuleFromSimpleSetPredicateIsNotInNotFinalLeaf.

@Test
public void declareRuleFromSimpleSetPredicateIsNotInNotFinalLeaf() {
    final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
    List<String> values = Arrays.asList("3", "8.5");
    final SimpleSetPredicate simpleSetPredicate = getSimpleSetPredicate("input2", values, SimpleSetPredicate.BooleanOperator.IS_NOT_IN, fieldTypeMap);
    String parentPath = "_classA";
    String currentRule = "_classA_classC";
    String result = "classC";
    String declaredType = fieldTypeMap.get("input2").getGeneratedType();
    final List<KiePMMLDroolsRule> rules = new ArrayList<>();
    PredicateASTFactoryData predicateASTFactoryData = getPredicateASTFactoryData(simpleSetPredicate, Collections.emptyList(), rules, parentPath, currentRule, fieldTypeMap);
    KiePMMLSimpleSetPredicateASTFactory.factory(predicateASTFactoryData).declareRuleFromSimpleSetPredicate(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());
    assertNull(retrieved.getResultCode());
    assertNull(retrieved.getResult());
    assertNotNull(retrieved.getNotInConstraints());
    final Map<String, List<Object>> notInConstraints = retrieved.getNotInConstraints();
    assertEquals(1, notInConstraints.size());
    assertTrue(notInConstraints.containsKey(declaredType));
    final List<Object> retrievedValues = notInConstraints.get(declaredType);
    List<String> originalPredicateValues = Arrays.asList(((String) simpleSetPredicate.getArray().getValue()).split(" "));
    assertEquals(originalPredicateValues.size(), retrievedValues.size());
    retrievedValues.forEach(retrievedValue -> {
        assertTrue(originalPredicateValues.contains(retrievedValue));
    });
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KiePMMLASTTestUtils.getPredicateASTFactoryData(org.kie.pmml.models.drools.utils.KiePMMLASTTestUtils.getPredicateASTFactoryData) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 13 with SimpleSetPredicate

use of org.dmg.pmml.SimpleSetPredicate in project drools by kiegroup.

the class KiePMMLSimpleSetPredicateASTFactoryTest method declareRuleFromSimpleSetPredicateIsInNotFinalLeaf.

@Test
public void declareRuleFromSimpleSetPredicateIsInNotFinalLeaf() {
    final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
    List<String> values = Arrays.asList("-5", "0.5", "1", "10");
    final SimpleSetPredicate simpleSetPredicate = getSimpleSetPredicate("input1", values, SimpleSetPredicate.BooleanOperator.IS_IN, fieldTypeMap);
    String parentPath = "_classA";
    String currentRule = "_classA_classB";
    String result = "classB";
    String declaredType = fieldTypeMap.get("input1").getGeneratedType();
    final List<KiePMMLDroolsRule> rules = new ArrayList<>();
    PredicateASTFactoryData predicateASTFactoryData = getPredicateASTFactoryData(simpleSetPredicate, Collections.emptyList(), rules, parentPath, currentRule, fieldTypeMap);
    KiePMMLSimpleSetPredicateASTFactory.factory(predicateASTFactoryData).declareRuleFromSimpleSetPredicate(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());
    assertNull(retrieved.getResultCode());
    assertNull(retrieved.getResult());
    assertNotNull(retrieved.getInConstraints());
    final Map<String, List<Object>> inConstraints = retrieved.getInConstraints();
    assertEquals(1, inConstraints.size());
    assertTrue(inConstraints.containsKey(declaredType));
    final List<Object> retrievedValues = inConstraints.get(declaredType);
    List<String> originalPredicateValues = Arrays.asList(((String) simpleSetPredicate.getArray().getValue()).split(" "));
    assertEquals(originalPredicateValues.size(), retrievedValues.size());
    retrievedValues.forEach(retrievedValue -> {
        assertTrue(originalPredicateValues.contains(retrievedValue));
    });
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KiePMMLASTTestUtils.getPredicateASTFactoryData(org.kie.pmml.models.drools.utils.KiePMMLASTTestUtils.getPredicateASTFactoryData) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 14 with SimpleSetPredicate

use of org.dmg.pmml.SimpleSetPredicate 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 15 with SimpleSetPredicate

use of org.dmg.pmml.SimpleSetPredicate in project drools by kiegroup.

the class KiePMMLCharacteristicFactoryTest method getCompoundPredicate.

private CompoundPredicate getCompoundPredicate(List<String> values, Array.Type arrayType) {
    SimplePredicate simplePredicate1 = getSimplePredicate(PARAM_1, value1, operator1);
    SimplePredicate simplePredicate2 = getSimplePredicate(PARAM_2, value2, operator2);
    SimpleSetPredicate simpleSetPredicate = getSimpleSetPredicate(values, arrayType, SimpleSetPredicate.BooleanOperator.IS_IN);
    CompoundPredicate toReturn = new CompoundPredicate();
    toReturn.setBooleanOperator(CompoundPredicate.BooleanOperator.AND);
    toReturn.getPredicates().add(0, simplePredicate1);
    toReturn.getPredicates().add(1, simplePredicate2);
    toReturn.getPredicates().add(2, simpleSetPredicate);
    return toReturn;
}
Also used : CompoundPredicate(org.dmg.pmml.CompoundPredicate) KiePMMLCompoundPredicate(org.kie.pmml.commons.model.predicates.KiePMMLCompoundPredicate) PMMLModelTestUtils.getSimplePredicate(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getSimplePredicate) KiePMMLSimplePredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate) SimplePredicate(org.dmg.pmml.SimplePredicate) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate) KiePMMLSimpleSetPredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimpleSetPredicate) KiePMMLSimpleSetPredicateFactoryTest.getSimpleSetPredicate(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLSimpleSetPredicateFactoryTest.getSimpleSetPredicate)

Aggregations

SimpleSetPredicate (org.dmg.pmml.SimpleSetPredicate)21 List (java.util.List)11 Test (org.junit.Test)11 SimplePredicate (org.dmg.pmml.SimplePredicate)10 Array (org.dmg.pmml.Array)9 KiePMMLSimpleSetPredicate (org.kie.pmml.commons.model.predicates.KiePMMLSimpleSetPredicate)8 CompoundPredicate (org.dmg.pmml.CompoundPredicate)7 Collectors (java.util.stream.Collectors)6 ArrayList (java.util.ArrayList)5 Arrays (java.util.Arrays)5 Collections (java.util.Collections)5 DataField (org.dmg.pmml.DataField)5 FieldName (org.dmg.pmml.FieldName)5 Predicate (org.dmg.pmml.Predicate)5 KiePMMLCompoundPredicate (org.kie.pmml.commons.model.predicates.KiePMMLCompoundPredicate)5 KiePMMLSimplePredicate (org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate)5 PMMLModelTestUtils.getSimplePredicate (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getSimplePredicate)5 KiePMMLSimpleSetPredicateFactoryTest.getSimpleSetPredicate (org.kie.pmml.compiler.commons.codegenfactories.KiePMMLSimpleSetPredicateFactoryTest.getSimpleSetPredicate)5 KiePMMLDroolsRule (org.kie.pmml.models.drools.ast.KiePMMLDroolsRule)5 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)4