Search in sources :

Example 81 with DataField

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

the class KiePMMLUtil method populateMissingOutputFieldDataType.

/**
 * Method to populate the <b>dataType</b> property of <code>OutputField</code>s.
 * Such property was optional until 4.4.1 spec
 * @param toPopulate
 * @param miningFields
 * @param dataFields
 */
static void populateMissingOutputFieldDataType(List<OutputField> toPopulate, List<MiningField> miningFields, List<DataField> dataFields) {
    // partial implementation to fix missing "dataType" inside OutputField; "dataType" became mandatory only in 4.4.1 version
    List<MiningField> targetFields = getMiningTargetFields(miningFields);
    toPopulate.stream().filter(outputField -> outputField.getDataType() == null).forEach(outputField -> {
        MiningField referencedField = null;
        if (outputField.getTargetField() != null) {
            referencedField = targetFields.stream().filter(targetField -> outputField.getTargetField().equals(targetField.getName())).findFirst().orElseThrow(() -> new KiePMMLException("Failed to find a target field for OutputField " + outputField.getName().getValue()));
        }
        if (referencedField == null && (outputField.getResultFeature() == null || outputField.getResultFeature().equals(ResultFeature.PREDICTED_VALUE))) {
            // default predictedValue
            referencedField = targetFields.stream().findFirst().orElse(// It is allowed to not have any "target" field inside MiningSchema
            null);
        }
        if (referencedField == null && ResultFeature.PROBABILITY.equals(outputField.getResultFeature())) {
            // we set the "dataType" to "double" because outputField is a "probability", we may return
            outputField.setDataType(DataType.DOUBLE);
            return;
        }
        if (referencedField != null) {
            FieldName targetFieldName = referencedField.getName();
            DataField dataField = dataFields.stream().filter(df -> df.getName().equals(targetFieldName)).findFirst().orElseThrow(() -> new KiePMMLException("Failed to find a DataField field for " + "MiningField " + targetFieldName.toString()));
            outputField.setDataType(dataField.getDataType());
        }
    });
}
Also used : PMML(org.dmg.pmml.PMML) Model(org.dmg.pmml.Model) OutputField(org.dmg.pmml.OutputField) Targets(org.dmg.pmml.Targets) DataType(org.dmg.pmml.DataType) ResultFeature(org.dmg.pmml.ResultFeature) MiningSchema(org.dmg.pmml.MiningSchema) Collectors(java.util.stream.Collectors) JAXBException(javax.xml.bind.JAXBException) Target(org.dmg.pmml.Target) DataField(org.dmg.pmml.DataField) FieldName(org.dmg.pmml.FieldName) OpType(org.dmg.pmml.OpType) List(java.util.List) Segment(org.dmg.pmml.mining.Segment) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXException(org.xml.sax.SAXException) Optional(java.util.Optional) MiningFunction(org.dmg.pmml.MiningFunction) MiningField(org.dmg.pmml.MiningField) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) MathContext(org.dmg.pmml.MathContext) InputStream(java.io.InputStream) MiningModel(org.dmg.pmml.mining.MiningModel) MiningField(org.dmg.pmml.MiningField) DataField(org.dmg.pmml.DataField) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) FieldName(org.dmg.pmml.FieldName)

Example 82 with DataField

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

use of org.dmg.pmml.DataField in project openscoring by openscoring.

the class ModelUtil method encodeInputFields.

private static List<Field> encodeInputFields(List<InputField> inputFields) {
    Function<InputField, Field> function = new Function<InputField, Field>() {

        @Override
        public Field apply(InputField inputField) {
            FieldName name = inputField.getName();
            DataField dataField = (DataField) inputField.getField();
            Field field = new Field(name.getValue());
            field.setName(dataField.getDisplayName());
            field.setDataType(inputField.getDataType());
            field.setOpType(inputField.getOpType());
            field.setValues(encodeValues(dataField));
            return field;
        }
    };
    List<Field> fields = new ArrayList<>(Lists.transform(inputFields, function));
    return fields;
}
Also used : InputField(org.jpmml.evaluator.InputField) OutputField(org.jpmml.evaluator.OutputField) DataField(org.dmg.pmml.DataField) TargetField(org.jpmml.evaluator.TargetField) Field(org.openscoring.common.Field) Function(com.google.common.base.Function) InputField(org.jpmml.evaluator.InputField) DataField(org.dmg.pmml.DataField) ArrayList(java.util.ArrayList) FieldName(org.dmg.pmml.FieldName)

Example 84 with DataField

use of org.dmg.pmml.DataField in project shifu by ShifuML.

the class PMMLAdapterCommonUtil method getDataDicHeaders.

/**
 * get the header names from the PMML data dictionary
 *
 * @param pmml
 *            the pmml model
 * @return headers
 */
public static String[] getDataDicHeaders(final PMML pmml) {
    DataDictionary dictionary = pmml.getDataDictionary();
    List<DataField> fields = dictionary.getDataFields();
    int len = fields.size();
    String[] headers = new String[len];
    for (int i = 0; i < len; i++) {
        headers[i] = fields.get(i).getName().getValue();
    }
    return headers;
}
Also used : DataField(org.dmg.pmml.DataField) DataDictionary(org.dmg.pmml.DataDictionary)

Example 85 with DataField

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

the class DMNImportPMMLInfo method from.

public static Either<Exception, DMNImportPMMLInfo> from(InputStream is, DMNCompilerConfigurationImpl cc, DMNModelImpl model, Import i) {
    try {
        final PMML pmml = org.jpmml.model.PMMLUtil.unmarshal(is);
        PMMLHeaderInfo h = PMMLInfo.pmmlToHeaderInfo(pmml, pmml.getHeader());
        for (DataField df : pmml.getDataDictionary().getDataFields()) {
            String dfName = df.getName().getValue();
            BuiltInType ft = getBuiltInTypeByDataType(df.getDataType());
            List<FEELProfile> helperFEELProfiles = cc.getFeelProfiles();
            DMNFEELHelper feel = new DMNFEELHelper(cc.getRootClassLoader(), helperFEELProfiles);
            List<UnaryTest> av = new ArrayList<>();
            if (df.getValues() != null && !df.getValues().isEmpty() && ft != BuiltInType.UNKNOWN) {
                final BuiltInType feelType = ft;
                String lov = df.getValues().stream().map(Value::getValue).map(o -> feelType == BuiltInType.STRING ? "\"" + o.toString() + "\"" : o.toString()).collect(Collectors.joining(","));
                av = feel.evaluateUnaryTests(lov, Collections.emptyMap());
            } else if (df.getIntervals() != null && !df.getIntervals().isEmpty() && ft != BuiltInType.UNKNOWN) {
                for (Interval interval : df.getIntervals()) {
                    String utString = null;
                    switch(interval.getClosure()) {
                        case CLOSED_CLOSED:
                            utString = new StringBuilder("[").append(interval.getLeftMargin()).append("..").append(interval.getRightMargin()).append("]").toString();
                            break;
                        case CLOSED_OPEN:
                            utString = new StringBuilder("[").append(interval.getLeftMargin()).append("..").append(interval.getRightMargin()).append(")").toString();
                            break;
                        case OPEN_CLOSED:
                            utString = new StringBuilder("(").append(interval.getLeftMargin()).append("..").append(interval.getRightMargin()).append("]").toString();
                            break;
                        case OPEN_OPEN:
                            utString = new StringBuilder("(").append(interval.getLeftMargin()).append("..").append(interval.getRightMargin()).append(")").toString();
                            break;
                    }
                    List<UnaryTest> ut = feel.evaluateUnaryTests(utString, Collections.emptyMap());
                    av.addAll(ut);
                }
            }
            DMNType type = new SimpleTypeImpl(i.getNamespace(), dfName, null, false, av, model.getTypeRegistry().resolveType(model.getDefinitions().getURIFEEL(), ft.getName()), ft);
            model.getTypeRegistry().registerType(type);
        }
        pmml.getModels().stream().forEach(m -> registerOutputFieldType(m, model, i));
        List<DMNPMMLModelInfo> models = pmml.getModels().stream().map(m -> PMMLInfo.pmmlToModelInfo(m)).map(proto -> DMNPMMLModelInfo.from(proto, model, i)).collect(Collectors.toList());
        DMNImportPMMLInfo info = new DMNImportPMMLInfo(i, models, h);
        return Either.ofRight(info);
    } catch (Throwable e) {
        return Either.ofLeft(new Exception("Unable to process DMNImportPMMLInfo", e));
    }
}
Also used : DMNType(org.kie.dmn.api.core.DMNType) Interval(org.dmg.pmml.Interval) Model(org.dmg.pmml.Model) OutputField(org.dmg.pmml.OutputField) FEELProfile(org.kie.dmn.feel.lang.FEELProfile) LoggerFactory(org.slf4j.LoggerFactory) Either(org.kie.dmn.feel.util.Either) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SimpleTypeImpl(org.kie.dmn.core.impl.SimpleTypeImpl) Value(org.dmg.pmml.Value) Map(java.util.Map) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) PMML(org.dmg.pmml.PMML) DMNCompilerConfigurationImpl(org.kie.dmn.core.compiler.DMNCompilerConfigurationImpl) Logger(org.slf4j.Logger) Collection(java.util.Collection) DataType(org.dmg.pmml.DataType) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) DMNModelImpl(org.kie.dmn.core.impl.DMNModelImpl) Import(org.kie.dmn.model.api.Import) Collectors(java.util.stream.Collectors) DataField(org.dmg.pmml.DataField) List(java.util.List) DMNFEELHelper(org.kie.dmn.core.compiler.DMNFEELHelper) CompositeTypeImpl(org.kie.dmn.core.impl.CompositeTypeImpl) Collections(java.util.Collections) InputStream(java.io.InputStream) DMNFEELHelper(org.kie.dmn.core.compiler.DMNFEELHelper) FEELProfile(org.kie.dmn.feel.lang.FEELProfile) ArrayList(java.util.ArrayList) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) DataField(org.dmg.pmml.DataField) SimpleTypeImpl(org.kie.dmn.core.impl.SimpleTypeImpl) Value(org.dmg.pmml.Value) PMML(org.dmg.pmml.PMML) ArrayList(java.util.ArrayList) List(java.util.List) Interval(org.dmg.pmml.Interval) DMNType(org.kie.dmn.api.core.DMNType)

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