Search in sources :

Example 6 with SimpleSetPredicate

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

the class KiePMMLSimpleSetPredicateASTFactoryTest method declareRuleFromSimpleSetPredicateIsNotInFinalLeaf.

@Test
public void declareRuleFromSimpleSetPredicateIsNotInFinalLeaf() {
    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<>();
    String statusToSet = DONE;
    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(statusToSet, retrieved.getStatusToSet());
    assertEquals(String.format(STATUS_PATTERN, parentPath), retrieved.getStatusConstraint());
    assertEquals(ResultCode.OK, retrieved.getResultCode());
    assertEquals(result, 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 7 with SimpleSetPredicate

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

the class KiePMMLCharacteristicFactoryTest method getAttributeVariableDeclarationWithComplexPartialScore.

@Test
public void getAttributeVariableDeclarationWithComplexPartialScore() throws IOException {
    final String variableName = "variableName";
    Array.Type arrayType = Array.Type.STRING;
    List<String> values1 = getStringObjects(arrayType, 4);
    Attribute attribute1 = getAttribute(values1, 1);
    List<String> values2 = getStringObjects(arrayType, 4);
    Attribute attribute2 = getAttribute(values2, 2);
    CompoundPredicate compoundPredicate1 = (CompoundPredicate) attribute1.getPredicate();
    CompoundPredicate compoundPredicate2 = (CompoundPredicate) attribute2.getPredicate();
    DataDictionary dataDictionary = new DataDictionary();
    for (Predicate predicate : compoundPredicate1.getPredicates()) {
        DataField toAdd = null;
        if (predicate instanceof SimplePredicate) {
            toAdd = new DataField();
            toAdd.setName(((SimplePredicate) predicate).getField());
            toAdd.setDataType(DataType.DOUBLE);
        } else if (predicate instanceof SimpleSetPredicate) {
            toAdd = new DataField();
            toAdd.setName(((SimpleSetPredicate) predicate).getField());
            toAdd.setDataType(DataType.DOUBLE);
        }
        if (toAdd != null) {
            dataDictionary.addDataFields(toAdd);
        }
    }
    for (Predicate predicate : compoundPredicate2.getPredicates()) {
        DataField toAdd = null;
        if (predicate instanceof SimplePredicate) {
            toAdd = new DataField();
            toAdd.setName(((SimplePredicate) predicate).getField());
            toAdd.setDataType(DataType.DOUBLE);
        } else if (predicate instanceof SimpleSetPredicate) {
            toAdd = new DataField();
            toAdd.setName(((SimpleSetPredicate) predicate).getField());
            toAdd.setDataType(DataType.DOUBLE);
        }
        if (toAdd != null) {
            dataDictionary.addDataFields(toAdd);
        }
    }
    String valuesString1 = values1.stream().map(valueString -> "\"" + valueString + "\"").collect(Collectors.joining(","));
    String valuesString2 = values2.stream().map(valueString -> "\"" + valueString + "\"").collect(Collectors.joining(","));
    Characteristic characteristic = new Characteristic();
    characteristic.addAttributes(attribute1, attribute2);
    characteristic.setBaselineScore(22);
    characteristic.setReasonCode(REASON_CODE);
    BlockStmt retrieved = KiePMMLCharacteristicFactory.getCharacteristicVariableDeclaration(variableName, characteristic, getFieldsFromDataDictionary(dataDictionary));
    String text = getFileContent(TEST_01_SOURCE);
    Statement expected = JavaParserUtils.parseBlock(String.format(text, variableName, valuesString1, valuesString2, characteristic.getBaselineScore(), characteristic.getReasonCode()));
    assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
    List<Class<?>> imports = Arrays.asList(KiePMMLAttribute.class, KiePMMLCharacteristic.class, KiePMMLComplexPartialScore.class, KiePMMLCompoundPredicate.class, KiePMMLConstant.class, KiePMMLSimplePredicate.class, KiePMMLSimpleSetPredicate.class, Arrays.class, Collections.class);
    commonValidateCompilationWithImports(retrieved, imports);
}
Also used : KiePMMLConstant(org.kie.pmml.commons.model.expressions.KiePMMLConstant) Arrays(java.util.Arrays) Predicate(org.dmg.pmml.Predicate) PMMLModelTestUtils.getSimplePredicate(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getSimplePredicate) KiePMMLAttribute(org.kie.pmml.models.scorecard.model.KiePMMLAttribute) KiePMMLComplexPartialScore(org.kie.pmml.models.scorecard.model.KiePMMLComplexPartialScore) Characteristic(org.dmg.pmml.scorecard.Characteristic) ComplexPartialScore(org.dmg.pmml.scorecard.ComplexPartialScore) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate) CompoundPredicate(org.dmg.pmml.CompoundPredicate) JavaParserUtils(org.kie.pmml.compiler.commons.utils.JavaParserUtils) KiePMMLSimplePredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate) DataType(org.dmg.pmml.DataType) KiePMMLSimpleSetPredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimpleSetPredicate) KiePMMLCompoundPredicate(org.kie.pmml.commons.model.predicates.KiePMMLCompoundPredicate) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) DataDictionary(org.dmg.pmml.DataDictionary) Test(org.junit.Test) Statement(com.github.javaparser.ast.stmt.Statement) Attribute(org.dmg.pmml.scorecard.Attribute) CommonTestingUtils.getFieldsFromDataDictionary(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary) Collectors(java.util.stream.Collectors) Array(org.dmg.pmml.Array) FileUtils.getFileContent(org.kie.test.util.filesystem.FileUtils.getFileContent) DataField(org.dmg.pmml.DataField) List(java.util.List) SimplePredicate(org.dmg.pmml.SimplePredicate) PMMLModelTestUtils.getStringObjects(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getStringObjects) CodegenTestUtils.commonValidateCompilationWithImports(org.kie.pmml.compiler.commons.testutils.CodegenTestUtils.commonValidateCompilationWithImports) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLSimpleSetPredicateFactoryTest.getSimpleSetPredicate(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLSimpleSetPredicateFactoryTest.getSimpleSetPredicate) Constant(org.dmg.pmml.Constant) Collections(java.util.Collections) KiePMMLCharacteristic(org.kie.pmml.models.scorecard.model.KiePMMLCharacteristic) KiePMMLAttribute(org.kie.pmml.models.scorecard.model.KiePMMLAttribute) Attribute(org.dmg.pmml.scorecard.Attribute) Statement(com.github.javaparser.ast.stmt.Statement) Characteristic(org.dmg.pmml.scorecard.Characteristic) KiePMMLCharacteristic(org.kie.pmml.models.scorecard.model.KiePMMLCharacteristic) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) DataDictionary(org.dmg.pmml.DataDictionary) CommonTestingUtils.getFieldsFromDataDictionary(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary) PMMLModelTestUtils.getSimplePredicate(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getSimplePredicate) KiePMMLSimplePredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate) SimplePredicate(org.dmg.pmml.SimplePredicate) Predicate(org.dmg.pmml.Predicate) PMMLModelTestUtils.getSimplePredicate(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getSimplePredicate) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate) CompoundPredicate(org.dmg.pmml.CompoundPredicate) KiePMMLSimplePredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate) KiePMMLSimpleSetPredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimpleSetPredicate) KiePMMLCompoundPredicate(org.kie.pmml.commons.model.predicates.KiePMMLCompoundPredicate) SimplePredicate(org.dmg.pmml.SimplePredicate) KiePMMLSimpleSetPredicateFactoryTest.getSimpleSetPredicate(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLSimpleSetPredicateFactoryTest.getSimpleSetPredicate) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate) KiePMMLSimpleSetPredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimpleSetPredicate) KiePMMLSimpleSetPredicateFactoryTest.getSimpleSetPredicate(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLSimpleSetPredicateFactoryTest.getSimpleSetPredicate) Array(org.dmg.pmml.Array) DataField(org.dmg.pmml.DataField) CompoundPredicate(org.dmg.pmml.CompoundPredicate) KiePMMLCompoundPredicate(org.kie.pmml.commons.model.predicates.KiePMMLCompoundPredicate) Test(org.junit.Test)

Example 8 with SimpleSetPredicate

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

the class KiePMMLAttributeFactoryTest 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)

Example 9 with SimpleSetPredicate

use of org.dmg.pmml.SimpleSetPredicate in project jpmml-sparkml by jpmml.

the class TreeModelCompactor method handleNodePush.

private void handleNodePush(Node node) {
    String id = node.getId();
    String score = node.getScore();
    if (id != null) {
        throw new IllegalArgumentException();
    }
    if (node.hasNodes()) {
        List<Node> children = node.getNodes();
        if (children.size() != 2 || score != null) {
            throw new IllegalArgumentException();
        }
        Node firstChild = children.get(0);
        Node secondChild = children.get(1);
        Predicate firstPredicate = firstChild.getPredicate();
        Predicate secondPredicate = secondChild.getPredicate();
        predicate: if (firstPredicate instanceof SimplePredicate && secondPredicate instanceof SimplePredicate) {
            SimplePredicate firstSimplePredicate = (SimplePredicate) firstPredicate;
            SimplePredicate secondSimplePredicate = (SimplePredicate) secondPredicate;
            SimplePredicate.Operator firstOperator = firstSimplePredicate.getOperator();
            SimplePredicate.Operator secondOperator = secondSimplePredicate.getOperator();
            if (!(firstSimplePredicate.getField()).equals(secondSimplePredicate.getField())) {
                throw new IllegalArgumentException();
            }
            if ((SimplePredicate.Operator.EQUAL).equals(firstOperator) && (SimplePredicate.Operator.EQUAL).equals(secondOperator)) {
                if (!isCategoricalField(firstSimplePredicate.getField())) {
                    break predicate;
                }
                secondChild.setPredicate(new True());
            } else {
                if (!(firstSimplePredicate.getValue()).equals(secondSimplePredicate.getValue())) {
                    throw new IllegalArgumentException();
                }
                if ((SimplePredicate.Operator.NOT_EQUAL).equals(firstOperator) && (SimplePredicate.Operator.EQUAL).equals(secondOperator)) {
                    swapChildren(children);
                    firstChild = children.get(0);
                    secondChild = children.get(1);
                } else if ((SimplePredicate.Operator.EQUAL).equals(firstOperator) && (SimplePredicate.Operator.NOT_EQUAL).equals(secondOperator)) {
                // Ignored
                } else if ((SimplePredicate.Operator.LESS_OR_EQUAL).equals(firstOperator) && (SimplePredicate.Operator.GREATER_THAN).equals(secondOperator)) {
                // Ignored
                } else {
                    throw new IllegalArgumentException();
                }
                secondChild.setPredicate(new True());
            }
        } else if (firstPredicate instanceof SimplePredicate && secondPredicate instanceof SimpleSetPredicate) {
            SimplePredicate simplePredicate = (SimplePredicate) firstPredicate;
            SimpleSetPredicate simpleSetPredicate = (SimpleSetPredicate) secondPredicate;
            if (!(simplePredicate.getField()).equals(simpleSetPredicate.getField()) || !(SimplePredicate.Operator.EQUAL).equals(simplePredicate.getOperator()) || !(SimpleSetPredicate.BooleanOperator.IS_IN).equals(simpleSetPredicate.getBooleanOperator())) {
                throw new IllegalArgumentException();
            }
            secondChild.setPredicate(addCategoricalField(simpleSetPredicate));
        } else if (firstPredicate instanceof SimpleSetPredicate && secondPredicate instanceof SimplePredicate) {
            SimpleSetPredicate simpleSetPredicate = (SimpleSetPredicate) firstPredicate;
            SimplePredicate simplePredicate = (SimplePredicate) secondPredicate;
            if (!(simpleSetPredicate.getField()).equals(simplePredicate.getField()) || !(SimpleSetPredicate.BooleanOperator.IS_IN).equals(simpleSetPredicate.getBooleanOperator()) || !(SimplePredicate.Operator.EQUAL).equals(simplePredicate.getOperator())) {
                throw new IllegalArgumentException();
            }
            swapChildren(children);
            firstChild = children.get(0);
            secondChild = children.get(1);
            secondChild.setPredicate(addCategoricalField(simpleSetPredicate));
        } else if (firstPredicate instanceof SimpleSetPredicate && secondPredicate instanceof SimpleSetPredicate) {
            SimpleSetPredicate firstSimpleSetPredicate = (SimpleSetPredicate) firstPredicate;
            SimpleSetPredicate secondSimpleSetPredicate = (SimpleSetPredicate) secondPredicate;
            if (!(firstSimpleSetPredicate.getField()).equals(secondSimpleSetPredicate.getField()) || !(SimpleSetPredicate.BooleanOperator.IS_IN).equals(firstSimpleSetPredicate.getBooleanOperator()) || !(SimpleSetPredicate.BooleanOperator.IS_IN).equals(secondSimpleSetPredicate.getBooleanOperator())) {
                throw new IllegalArgumentException();
            }
            secondChild.setPredicate(addCategoricalField(secondSimpleSetPredicate));
        } else {
            throw new IllegalArgumentException();
        }
    } else {
        if (score == null) {
            throw new IllegalArgumentException();
        }
    }
}
Also used : Node(org.dmg.pmml.tree.Node) True(org.dmg.pmml.True) SimplePredicate(org.dmg.pmml.SimplePredicate) Predicate(org.dmg.pmml.Predicate) SimplePredicate(org.dmg.pmml.SimplePredicate) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate)

Example 10 with SimpleSetPredicate

use of org.dmg.pmml.SimpleSetPredicate in project jpmml-sparkml by jpmml.

the class TreeModelCompactor method isCategoricalField.

private boolean isCategoricalField(FieldName name) {
    Deque<PMMLObject> parents = getParents();
    for (PMMLObject parent : parents) {
        if (parent instanceof Node) {
            Node node = (Node) parent;
            Predicate predicate = node.getPredicate();
            if (predicate instanceof SimpleSetPredicate) {
                SimpleSetPredicate simpleSetPredicate = (SimpleSetPredicate) predicate;
                FieldName categoricalField = simpleSetPredicate.getField();
                if ((name).equals(categoricalField)) {
                    return true;
                }
            } else if (predicate instanceof True) {
                True truePredicate = (True) predicate;
                FieldName categoricalField = this.categoricalFields.get(truePredicate);
                if ((name).equals(categoricalField)) {
                    return true;
                }
            }
        } else {
            return false;
        }
    }
    return false;
}
Also used : Node(org.dmg.pmml.tree.Node) PMMLObject(org.dmg.pmml.PMMLObject) True(org.dmg.pmml.True) FieldName(org.dmg.pmml.FieldName) Predicate(org.dmg.pmml.Predicate) SimplePredicate(org.dmg.pmml.SimplePredicate) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate)

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