Search in sources :

Example 1 with CompoundPredicate

use of org.dmg.pmml.CompoundPredicate 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 2 with CompoundPredicate

use of org.dmg.pmml.CompoundPredicate 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 3 with CompoundPredicate

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

the class PMMLModelTestUtils method getRandomCompoundPredicate.

public static CompoundPredicate getRandomCompoundPredicate(List<Field<?>> fields) {
    CompoundPredicate toReturn = new CompoundPredicate();
    toReturn.setBooleanOperator(getRandomCompoundPredicateAndOrOperator(new Random().nextInt(10)));
    IntStream.range(0, fields.size() - 1).forEach(i -> {
        toReturn.addPredicates(getRandomSimplePredicate((DataField) fields.get(i)));
    });
    toReturn.addPredicates(getRandomSimpleSetPredicate((DataField) fields.get(fields.size() - 1)));
    return toReturn;
}
Also used : Random(java.util.Random) DataField(org.dmg.pmml.DataField) CompoundPredicate(org.dmg.pmml.CompoundPredicate)

Example 4 with CompoundPredicate

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

the class PMMLModelTestUtils method getRandomCompoundPredicate.

public static CompoundPredicate getRandomCompoundPredicate() {
    CompoundPredicate toReturn = new CompoundPredicate();
    toReturn.setBooleanOperator(getRandomCompoundPredicateAndOrOperator(new Random().nextInt(10)));
    IntStream.range(0, 3).forEach(i -> {
        toReturn.addPredicates(getRandomSimplePredicate());
    });
    toReturn.addPredicates(getRandomSimpleSetPredicate());
    return toReturn;
}
Also used : Random(java.util.Random) CompoundPredicate(org.dmg.pmml.CompoundPredicate)

Example 5 with CompoundPredicate

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

the class KiePMMLCompoundPredicateWithResultASTFactory method declareRuleFromCompoundPredicateSurrogate.

/**
 * Method to be invoked when <b>compoundPredicate.getBooleanOperator()</b> is <code>SURROGATE</code>.
 * Throws exception otherwise
 * @param predicateASTFactoryData
 * @param agendaActivationGroup
 * @param result
 * @param isFinalLeaf
 */
public static void declareRuleFromCompoundPredicateSurrogate(final PredicateASTFactoryData predicateASTFactoryData, final String agendaActivationGroup, final Object result, boolean isFinalLeaf) {
    logger.trace("declareRuleFromCompoundPredicateSurrogate {} {} {} {}", predicateASTFactoryData, agendaActivationGroup, result, isFinalLeaf);
    // Managing only SimplePredicates for the moment being
    CompoundPredicate compoundPredicate = (CompoundPredicate) predicateASTFactoryData.getPredicate();
    final List<Predicate> simplePredicates = compoundPredicate.getPredicates().stream().filter(predicate -> predicate instanceof SimplePredicate).collect(Collectors.toList());
    simplePredicates.forEach(predicate -> {
        SimplePredicate simplePredicate = (SimplePredicate) predicate;
        PredicateASTFactoryData newPredicateASTFactoryData = predicateASTFactoryData.cloneWithPredicate(simplePredicate);
        KiePMMLSimplePredicateASTFactory.factory(newPredicateASTFactoryData).declareRuleFromSimplePredicateSurrogate(agendaActivationGroup, result, isFinalLeaf);
    });
}
Also used : CompoundPredicate(org.dmg.pmml.CompoundPredicate) List(java.util.List) Predicate(org.dmg.pmml.Predicate) SimplePredicate(org.dmg.pmml.SimplePredicate) Logger(org.slf4j.Logger) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) ResultCode(org.kie.pmml.api.enums.ResultCode) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) CompoundPredicate(org.dmg.pmml.CompoundPredicate) SimplePredicate(org.dmg.pmml.SimplePredicate) CompoundPredicate(org.dmg.pmml.CompoundPredicate) Predicate(org.dmg.pmml.Predicate) SimplePredicate(org.dmg.pmml.SimplePredicate)

Aggregations

CompoundPredicate (org.dmg.pmml.CompoundPredicate)29 SimplePredicate (org.dmg.pmml.SimplePredicate)17 Test (org.junit.Test)14 List (java.util.List)9 Collectors (java.util.stream.Collectors)9 Predicate (org.dmg.pmml.Predicate)9 KiePMMLDroolsRule (org.kie.pmml.models.drools.ast.KiePMMLDroolsRule)9 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 SimpleSetPredicate (org.dmg.pmml.SimpleSetPredicate)7 KiePMMLOriginalTypeGeneratedType (org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType)7 KiePMMLCompoundPredicate (org.kie.pmml.commons.model.predicates.KiePMMLCompoundPredicate)6 DataField (org.dmg.pmml.DataField)5 DataType (org.dmg.pmml.DataType)5 Assert.assertTrue (org.junit.Assert.assertTrue)5 KiePMMLSimplePredicate (org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate)5 KiePMMLSimpleSetPredicate (org.kie.pmml.commons.model.predicates.KiePMMLSimpleSetPredicate)5 PMMLModelTestUtils.getSimplePredicate (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getSimplePredicate)5 KiePMMLSimpleSetPredicateFactoryTest.getSimpleSetPredicate (org.kie.pmml.compiler.commons.codegenfactories.KiePMMLSimpleSetPredicateFactoryTest.getSimpleSetPredicate)5 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)4