Search in sources :

Example 71 with DataField

use of org.dmg.pmml.DataField 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 72 with DataField

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

the class KiePMMLUtilTest method populateMissingMiningTargetField.

@Test
public void populateMissingMiningTargetField() throws Exception {
    final InputStream inputStream = getFileInputStream(NO_TARGET_FIELD_SAMPLE);
    final PMML pmml = org.jpmml.model.PMMLUtil.unmarshal(inputStream);
    final Model toPopulate = pmml.getModels().get(0);
    List<MiningField> miningTargetFields = getMiningTargetFields(toPopulate.getMiningSchema().getMiningFields());
    assertTrue(miningTargetFields.isEmpty());
    assertNull(toPopulate.getTargets().getTargets().get(0).getField());
    KiePMMLUtil.populateMissingMiningTargetField(toPopulate, pmml.getDataDictionary().getDataFields());
    miningTargetFields = getMiningTargetFields(toPopulate.getMiningSchema().getMiningFields());
    assertEquals(1, miningTargetFields.size());
    final MiningField targetField = miningTargetFields.get(0);
    assertTrue(pmml.getDataDictionary().getDataFields().stream().anyMatch(dataField -> dataField.getName().equals(targetField.getName())));
    assertEquals(targetField.getName(), toPopulate.getTargets().getTargets().get(0).getField());
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Model(org.dmg.pmml.Model) OutputField(org.dmg.pmml.OutputField) Random(java.util.Random) ResultFeature(org.dmg.pmml.ResultFeature) FieldName(org.dmg.pmml.FieldName) MODELNAME_TEMPLATE(org.kie.pmml.compiler.commons.utils.KiePMMLUtil.MODELNAME_TEMPLATE) OpType(org.dmg.pmml.OpType) Charset(java.nio.charset.Charset) MiningFunction(org.dmg.pmml.MiningFunction) MiningField(org.dmg.pmml.MiningField) MiningModel(org.dmg.pmml.mining.MiningModel) PMML(org.dmg.pmml.PMML) Targets(org.dmg.pmml.Targets) Assert.assertNotNull(org.junit.Assert.assertNotNull) DataType(org.dmg.pmml.DataType) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) JAXBException(javax.xml.bind.JAXBException) Target(org.dmg.pmml.Target) StandardCharsets(java.nio.charset.StandardCharsets) SEGMENTID_TEMPLATE(org.kie.pmml.compiler.commons.utils.KiePMMLUtil.SEGMENTID_TEMPLATE) DataField(org.dmg.pmml.DataField) SEGMENTMODELNAME_TEMPLATE(org.kie.pmml.compiler.commons.utils.KiePMMLUtil.SEGMENTMODELNAME_TEMPLATE) List(java.util.List) Segment(org.dmg.pmml.mining.Segment) TARGETFIELD_TEMPLATE(org.kie.pmml.compiler.commons.utils.KiePMMLUtil.TARGETFIELD_TEMPLATE) Assert.assertNull(org.junit.Assert.assertNull) FileUtils.getFileInputStream(org.kie.test.util.filesystem.FileUtils.getFileInputStream) Assert.assertFalse(org.junit.Assert.assertFalse) SAXException(org.xml.sax.SAXException) KiePMMLUtil.getMiningTargetFields(org.kie.pmml.compiler.commons.utils.KiePMMLUtil.getMiningTargetFields) Optional(java.util.Optional) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) BufferedReader(java.io.BufferedReader) MathContext(org.dmg.pmml.MathContext) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) MiningField(org.dmg.pmml.MiningField) FileUtils.getFileInputStream(org.kie.test.util.filesystem.FileUtils.getFileInputStream) InputStream(java.io.InputStream) Model(org.dmg.pmml.Model) MiningModel(org.dmg.pmml.mining.MiningModel) PMML(org.dmg.pmml.PMML) Test(org.junit.Test)

Example 73 with DataField

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

the class KiePMMLUtilTest method getTargetDataField.

@Test
public void getTargetDataField() throws Exception {
    final InputStream inputStream = getFileInputStream(NO_TARGET_FIELD_SAMPLE);
    final PMML pmml = org.jpmml.model.PMMLUtil.unmarshal(inputStream);
    final Model model = pmml.getModels().get(0);
    Optional<DataField> optionalDataField = KiePMMLUtil.getTargetDataField(model);
    assertTrue(optionalDataField.isPresent());
    DataField retrieved = optionalDataField.get();
    String expected = String.format(TARGETFIELD_TEMPLATE, "golfing");
    assertEquals(expected, retrieved.getName().getValue());
}
Also used : DataField(org.dmg.pmml.DataField) FileUtils.getFileInputStream(org.kie.test.util.filesystem.FileUtils.getFileInputStream) InputStream(java.io.InputStream) Model(org.dmg.pmml.Model) MiningModel(org.dmg.pmml.mining.MiningModel) PMML(org.dmg.pmml.PMML) Test(org.junit.Test)

Example 74 with DataField

use of org.dmg.pmml.DataField 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 75 with DataField

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

the class KiePMMLSimplePredicateFactoryTest method getSimplePredicateVariableDeclaration.

@Test
public void getSimplePredicateVariableDeclaration() throws IOException {
    String variableName = "variableName";
    final SimplePredicate simplePredicate = new SimplePredicate();
    simplePredicate.setField(FieldName.create("CUSTOM_FIELD"));
    simplePredicate.setValue("235.435");
    simplePredicate.setOperator(SimplePredicate.Operator.EQUAL);
    String operatorString = OPERATOR.class.getName() + "." + OPERATOR.byName(simplePredicate.getOperator().value());
    DataField dataField = new DataField();
    dataField.setName(simplePredicate.getField());
    dataField.setDataType(DataType.DOUBLE);
    DataDictionary dataDictionary = new DataDictionary();
    dataDictionary.addDataFields(dataField);
    BlockStmt retrieved = KiePMMLSimplePredicateFactory.getSimplePredicateVariableDeclaration(variableName, simplePredicate, getFieldsFromDataDictionary(dataDictionary));
    String text = getFileContent(TEST_01_SOURCE);
    Statement expected = JavaParserUtils.parseBlock(String.format(text, variableName, simplePredicate.getField().getValue(), operatorString, simplePredicate.getValue()));
    assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
    List<Class<?>> imports = Arrays.asList(KiePMMLSimplePredicate.class, Collections.class);
    commonValidateCompilationWithImports(retrieved, imports);
}
Also used : DataField(org.dmg.pmml.DataField) 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) KiePMMLSimplePredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate) SimplePredicate(org.dmg.pmml.SimplePredicate) Test(org.junit.Test)

Aggregations

DataField (org.dmg.pmml.DataField)101 Test (org.junit.Test)51 DataDictionary (org.dmg.pmml.DataDictionary)42 MiningField (org.dmg.pmml.MiningField)42 MiningSchema (org.dmg.pmml.MiningSchema)30 PMMLModelTestUtils.getRandomDataField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField)28 RegressionModel (org.dmg.pmml.regression.RegressionModel)27 CommonTestingUtils.getFieldsFromDataDictionary (org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary)27 FieldName (org.dmg.pmml.FieldName)24 Model (org.dmg.pmml.Model)24 PMMLModelTestUtils.getDataField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataField)22 DataType (org.dmg.pmml.DataType)19 OutputField (org.dmg.pmml.OutputField)19 PMMLModelTestUtils.getRandomMiningField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField)19 PMMLModelTestUtils.getMiningField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getMiningField)18 ArrayList (java.util.ArrayList)17 List (java.util.List)17 PMML (org.dmg.pmml.PMML)17 Collectors (java.util.stream.Collectors)16 OpType (org.dmg.pmml.OpType)15