Search in sources :

Example 16 with SimpleSetPredicate

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

the class KiePMMLAttributeFactoryTest method getAttributeVariableDeclarationWithComplexPartialScore.

@Test
public void getAttributeVariableDeclarationWithComplexPartialScore() throws IOException {
    final String variableName = "variableName";
    Attribute attribute = new Attribute();
    attribute.setReasonCode(REASON_CODE);
    Array.Type arrayType = Array.Type.STRING;
    List<String> values = getStringObjects(arrayType, 4);
    CompoundPredicate compoundPredicate = getCompoundPredicate(values, arrayType);
    attribute.setPredicate(compoundPredicate);
    attribute.setComplexPartialScore(getComplexPartialScore());
    String valuesString = values.stream().map(valueString -> "\"" + valueString + "\"").collect(Collectors.joining(","));
    DataDictionary dataDictionary = new DataDictionary();
    for (Predicate predicate : compoundPredicate.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);
        }
    }
    BlockStmt retrieved = KiePMMLAttributeFactory.getAttributeVariableDeclaration(variableName, attribute, getFieldsFromDataDictionary(dataDictionary));
    String text = getFileContent(TEST_01_SOURCE);
    Statement expected = JavaParserUtils.parseBlock(String.format(text, variableName, valuesString));
    assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
    List<Class<?>> imports = Arrays.asList(KiePMMLAttribute.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) 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) KiePMMLAttribute(org.kie.pmml.models.scorecard.model.KiePMMLAttribute) Attribute(org.dmg.pmml.scorecard.Attribute) Statement(com.github.javaparser.ast.stmt.Statement) 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 17 with SimpleSetPredicate

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

the class KiePMMLSimpleSetPredicateASTFactory method getBuilderForSimpleSetPredicate.

private KiePMMLDroolsRule.Builder getBuilderForSimpleSetPredicate(final String statusToSet) {
    logger.trace("declareRuleFromSimpleSetPredicate {}", statusToSet);
    String statusConstraint = StringUtils.isEmpty(predicateASTFactoryData.getParentPath()) ? STATUS_NULL : String.format(STATUS_PATTERN, predicateASTFactoryData.getParentPath());
    SimpleSetPredicate simpleSetPredicate = (SimpleSetPredicate) predicateASTFactoryData.getPredicate();
    String key = predicateASTFactoryData.getFieldTypeMap().get(simpleSetPredicate.getField().getValue()).getGeneratedType();
    String stringValue = (String) simpleSetPredicate.getArray().getValue();
    String[] valuesArray = stringValue.split(" ");
    List<Object> value = Arrays.stream(valuesArray).map(rawValue -> {
        String originalType = predicateASTFactoryData.getFieldTypeMap().get(simpleSetPredicate.getField().getValue()).getOriginalType();
        switch(originalType) {
            case "string":
                return "\"" + rawValue + "\"";
            case "double":
                return Double.valueOf(rawValue).toString();
            default:
                return rawValue;
        }
    }).collect(Collectors.toList());
    Map<String, List<Object>> constraints = Collections.singletonMap(key, value);
    KiePMMLDroolsRule.Builder toReturn = KiePMMLDroolsRule.builder(predicateASTFactoryData.getCurrentRule(), statusToSet, predicateASTFactoryData.getOutputFields()).withStatusConstraint(statusConstraint);
    if (SimpleSetPredicate.BooleanOperator.IS_IN.equals(simpleSetPredicate.getBooleanOperator())) {
        toReturn = toReturn.withInConstraints(constraints);
    } else {
        toReturn = toReturn.withNotInConstraints(constraints);
    }
    return toReturn;
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) DONE(org.kie.pmml.commons.Constants.DONE) List(java.util.List) KiePMMLReasonCodeAndValue(org.kie.pmml.models.drools.tuples.KiePMMLReasonCodeAndValue) Map(java.util.Map) STATUS_PATTERN(org.kie.pmml.models.drools.ast.factories.KiePMMLAbstractModelASTFactory.STATUS_PATTERN) Collections(java.util.Collections) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate) STATUS_NULL(org.kie.pmml.models.drools.ast.factories.KiePMMLAbstractModelASTFactory.STATUS_NULL) StringUtils(org.drools.core.util.StringUtils) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) List(java.util.List) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate)

Example 18 with SimpleSetPredicate

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

the class KiePMMLPredicateInstanceFactoryTest method getKiePMMLPredicate.

@Test
public void getKiePMMLPredicate() {
    List<Field<?>> fields = IntStream.range(0, 3).mapToObj(i -> getRandomDataField()).collect(Collectors.toList());
    SimplePredicate simplePredicate1 = getRandomSimplePredicate((DataField) fields.get(0));
    KiePMMLPredicate retrieved = KiePMMLPredicateInstanceFactory.getKiePMMLPredicate(simplePredicate1, fields);
    commonVerifyKiePMMLPredicate(retrieved, simplePredicate1);
    SimpleSetPredicate simpleSetPredicate = getRandomSimpleSetPredicate((DataField) fields.get(2));
    retrieved = KiePMMLPredicateInstanceFactory.getKiePMMLPredicate(simpleSetPredicate, fields);
    commonVerifyKiePMMLPredicate(retrieved, simpleSetPredicate);
    final CompoundPredicate compoundPredicate = getRandomCompoundPredicate(fields);
    retrieved = KiePMMLPredicateInstanceFactory.getKiePMMLPredicate(compoundPredicate, fields);
    commonVerifyKiePMMLPredicate(retrieved, compoundPredicate);
    False falsePredicate = new False();
    retrieved = KiePMMLPredicateInstanceFactory.getKiePMMLPredicate(falsePredicate, fields);
    commonVerifyKiePMMLPredicate(retrieved, falsePredicate);
    True truePredicate = new True();
    retrieved = KiePMMLPredicateInstanceFactory.getKiePMMLPredicate(truePredicate, fields);
    commonVerifyKiePMMLPredicate(retrieved, truePredicate);
}
Also used : IntStream(java.util.stream.IntStream) PMMLModelTestUtils.getRandomSimplePredicate(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomSimplePredicate) CompoundPredicate(org.dmg.pmml.CompoundPredicate) PMMLModelTestUtils.getRandomCompoundPredicate(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomCompoundPredicate) False(org.dmg.pmml.False) Test(org.junit.Test) PMMLModelTestUtils.getRandomDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField) Collectors(java.util.stream.Collectors) KiePMMLPredicate(org.kie.pmml.commons.model.predicates.KiePMMLPredicate) DataField(org.dmg.pmml.DataField) List(java.util.List) SimplePredicate(org.dmg.pmml.SimplePredicate) Field(org.dmg.pmml.Field) InstanceFactoriesTestCommon.commonVerifyKiePMMLPredicate(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLPredicate) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate) PMMLModelTestUtils.getRandomSimpleSetPredicate(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomSimpleSetPredicate) True(org.dmg.pmml.True) PMMLModelTestUtils.getRandomDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField) DataField(org.dmg.pmml.DataField) Field(org.dmg.pmml.Field) KiePMMLPredicate(org.kie.pmml.commons.model.predicates.KiePMMLPredicate) InstanceFactoriesTestCommon.commonVerifyKiePMMLPredicate(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLPredicate) True(org.dmg.pmml.True) CompoundPredicate(org.dmg.pmml.CompoundPredicate) PMMLModelTestUtils.getRandomCompoundPredicate(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomCompoundPredicate) False(org.dmg.pmml.False) PMMLModelTestUtils.getRandomSimplePredicate(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomSimplePredicate) SimplePredicate(org.dmg.pmml.SimplePredicate) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate) PMMLModelTestUtils.getRandomSimpleSetPredicate(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomSimpleSetPredicate) Test(org.junit.Test)

Example 19 with SimpleSetPredicate

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

the class KiePMMLCompoundPredicateFactoryTest method getCompoundPredicateVariableDeclaration.

@Test
public void getCompoundPredicateVariableDeclaration() throws IOException {
    String variableName = "variableName";
    SimplePredicate simplePredicate1 = getSimplePredicate(PARAM_1, value1, operator1);
    SimplePredicate simplePredicate2 = getSimplePredicate(PARAM_2, value2, operator2);
    Array.Type arrayType = Array.Type.STRING;
    List<String> values = getStringObjects(arrayType, 4);
    SimpleSetPredicate simpleSetPredicate = getSimpleSetPredicate(values, arrayType, SimpleSetPredicate.BooleanOperator.IS_IN);
    CompoundPredicate compoundPredicate = new CompoundPredicate();
    compoundPredicate.setBooleanOperator(CompoundPredicate.BooleanOperator.AND);
    compoundPredicate.getPredicates().add(0, simplePredicate1);
    compoundPredicate.getPredicates().add(1, simplePredicate2);
    compoundPredicate.getPredicates().add(2, simpleSetPredicate);
    DataField dataField1 = new DataField();
    dataField1.setName(simplePredicate1.getField());
    dataField1.setDataType(DataType.DOUBLE);
    DataField dataField2 = new DataField();
    dataField2.setName(simplePredicate2.getField());
    dataField2.setDataType(DataType.DOUBLE);
    DataField dataField3 = new DataField();
    dataField3.setName(simpleSetPredicate.getField());
    dataField3.setDataType(DataType.DOUBLE);
    DataDictionary dataDictionary = new DataDictionary();
    dataDictionary.addDataFields(dataField1, dataField2, dataField3);
    String booleanOperatorString = BOOLEAN_OPERATOR.class.getName() + "." + BOOLEAN_OPERATOR.byName(compoundPredicate.getBooleanOperator().value()).name();
    String valuesString = values.stream().map(valueString -> "\"" + valueString + "\"").collect(Collectors.joining(","));
    final List<Field<?>> fields = getFieldsFromDataDictionary(dataDictionary);
    BlockStmt retrieved = KiePMMLCompoundPredicateFactory.getCompoundPredicateVariableDeclaration(variableName, compoundPredicate, fields);
    String text = getFileContent(TEST_01_SOURCE);
    Statement expected = JavaParserUtils.parseBlock(String.format(text, variableName, valuesString, booleanOperatorString));
    assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
    List<Class<?>> imports = Arrays.asList(KiePMMLCompoundPredicate.class, KiePMMLSimplePredicate.class, KiePMMLSimpleSetPredicate.class, Arrays.class, Collections.class);
    commonValidateCompilationWithImports(retrieved, imports);
}
Also used : Arrays(java.util.Arrays) PMMLModelTestUtils.getSimplePredicate(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getSimplePredicate) Field(org.dmg.pmml.Field) 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) BOOLEAN_OPERATOR(org.kie.pmml.api.enums.BOOLEAN_OPERATOR) 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) 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) Collections(java.util.Collections) Statement(com.github.javaparser.ast.stmt.Statement) 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) 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) Field(org.dmg.pmml.Field) DataField(org.dmg.pmml.DataField) DataField(org.dmg.pmml.DataField) CompoundPredicate(org.dmg.pmml.CompoundPredicate) KiePMMLCompoundPredicate(org.kie.pmml.commons.model.predicates.KiePMMLCompoundPredicate) Test(org.junit.Test)

Example 20 with SimpleSetPredicate

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

the class PMMLModelTestUtils method getRandomSimpleSetPredicate.

public static SimpleSetPredicate getRandomSimpleSetPredicate() {
    FieldName fieldName = FieldName.create(RandomStringUtils.random(6, true, false));
    SimpleSetPredicate toReturn = new SimpleSetPredicate();
    toReturn.setField(fieldName);
    toReturn.setBooleanOperator(getRandomSimpleSetPredicateOperator());
    Array.Type arrayType = getRandomArrayType();
    List<String> values = getStringObjects(arrayType, 3);
    Array array = getArray(arrayType, values);
    toReturn.setArray(array);
    return toReturn;
}
Also used : Array(org.dmg.pmml.Array) FieldName(org.dmg.pmml.FieldName) 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