Search in sources :

Example 11 with MiningField

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

the class ModelUtilsTest method getTargetFieldName.

@Test
public void getTargetFieldName() {
    final String fieldName = "fieldName";
    MiningField.UsageType usageType = MiningField.UsageType.ACTIVE;
    MiningField miningField = getMiningField(fieldName, usageType);
    final DataField dataField = getDataField(fieldName, OpType.CATEGORICAL, DataType.STRING);
    final DataDictionary dataDictionary = new DataDictionary();
    dataDictionary.addDataFields(dataField);
    MiningSchema miningSchema = new MiningSchema();
    miningSchema.addMiningFields(miningField);
    final Model model = new RegressionModel();
    model.setMiningSchema(miningSchema);
    final List<Field<?>> fields = getFieldsFromDataDictionary(dataDictionary);
    Optional<String> retrieved = ModelUtils.getTargetFieldName(fields, model);
    assertFalse(retrieved.isPresent());
    usageType = MiningField.UsageType.PREDICTED;
    miningField = getMiningField(fieldName, usageType);
    miningSchema = new MiningSchema();
    miningSchema.addMiningFields(miningField);
    model.setMiningSchema(miningSchema);
    retrieved = ModelUtils.getTargetFieldName(fields, model);
    assertTrue(retrieved.isPresent());
    assertEquals(fieldName, retrieved.get());
}
Also used : PMMLModelTestUtils.getMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getMiningField) DataField(org.dmg.pmml.DataField) ParameterField(org.dmg.pmml.ParameterField) OutputField(org.dmg.pmml.OutputField) PMMLModelTestUtils.getDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataField) Field(org.dmg.pmml.Field) DerivedField(org.dmg.pmml.DerivedField) MiningField(org.dmg.pmml.MiningField) PMMLModelTestUtils.getRandomDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField) PMMLModelTestUtils.getRandomOutputField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomOutputField) PMMLModelTestUtils.getRandomMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField) PMMLModelTestUtils.getMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getMiningField) MiningField(org.dmg.pmml.MiningField) PMMLModelTestUtils.getRandomMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField) DataField(org.dmg.pmml.DataField) PMMLModelTestUtils.getDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataField) PMMLModelTestUtils.getRandomDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField) MiningSchema(org.dmg.pmml.MiningSchema) Model(org.dmg.pmml.Model) RegressionModel(org.dmg.pmml.regression.RegressionModel) DataDictionary(org.dmg.pmml.DataDictionary) CommonTestingUtils.getFieldsFromDataDictionary(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary) RegressionModel(org.dmg.pmml.regression.RegressionModel) Test(org.junit.Test)

Example 12 with MiningField

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

the class KiePMMLMiningFieldFactory method getMiningFieldVariableDeclaration.

static BlockStmt getMiningFieldVariableDeclaration(final String variableName, final MiningField miningField, final List<Field<?>> fields) {
    final MethodDeclaration methodDeclaration = MININGFIELD_TEMPLATE.getMethodsByName(GETKIEPMMLMININGFIELD).get(0).clone();
    final BlockStmt miningFieldBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(miningFieldBody, MININGFIELD).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, MININGFIELD, miningFieldBody)));
    variableDeclarator.setName(variableName);
    final BlockStmt toReturn = new BlockStmt();
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, MININGFIELD, toReturn))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    final StringLiteralExpr nameExpr = new StringLiteralExpr(miningField.getName().getValue());
    Expression fieldUsageTypeExpr;
    if (miningField.getUsageType() != null) {
        final FIELD_USAGE_TYPE fieldUsageType = FIELD_USAGE_TYPE.byName(miningField.getUsageType().value());
        fieldUsageTypeExpr = new NameExpr(FIELD_USAGE_TYPE.class.getName() + "." + fieldUsageType.name());
    } else {
        fieldUsageTypeExpr = new NullLiteralExpr();
    }
    Expression opTypeExpr;
    if (miningField.getOpType() != null) {
        final OP_TYPE opType = OP_TYPE.byName(miningField.getOpType().value());
        opTypeExpr = new NameExpr(OP_TYPE.class.getName() + "." + opType.name());
    } else {
        opTypeExpr = new NullLiteralExpr();
    }
    final List<Field<?>> mappedFields = getMappedFields(fields, miningField.getName().getValue());
    final DataType dataType = getDataType(mappedFields, miningField.getName().getValue());
    final DATA_TYPE data_TYPE = DATA_TYPE.byName(dataType.value());
    Expression dataTypeExpr = new NameExpr(DATA_TYPE.class.getName() + "." + data_TYPE.name());
    Expression missingValueTreatmentMethodExpr;
    if (miningField.getMissingValueTreatment() != null) {
        final MISSING_VALUE_TREATMENT_METHOD missingValueTreatmentMethod = MISSING_VALUE_TREATMENT_METHOD.byName(miningField.getMissingValueTreatment().value());
        missingValueTreatmentMethodExpr = new NameExpr(MISSING_VALUE_TREATMENT_METHOD.class.getName() + "." + missingValueTreatmentMethod.name());
    } else {
        missingValueTreatmentMethodExpr = new NullLiteralExpr();
    }
    Expression invalidValueTreatmentMethodExpr;
    if (miningField.getInvalidValueTreatment() != null) {
        final INVALID_VALUE_TREATMENT_METHOD invalidValueTreatmentMethod = INVALID_VALUE_TREATMENT_METHOD.byName(miningField.getInvalidValueTreatment().value());
        invalidValueTreatmentMethodExpr = new NameExpr(INVALID_VALUE_TREATMENT_METHOD.class.getName() + "." + invalidValueTreatmentMethod.name());
    } else {
        invalidValueTreatmentMethodExpr = new NullLiteralExpr();
    }
    Expression missingValueReplacementExpr;
    if (miningField.getMissingValueReplacement() != null) {
        final String missingValueReplacement = miningField.getMissingValueReplacement().toString();
        missingValueReplacementExpr = new StringLiteralExpr(missingValueReplacement);
    } else {
        missingValueReplacementExpr = new NullLiteralExpr();
    }
    Expression invalidValueReplacementExpr;
    if (miningField.getInvalidValueReplacement() != null) {
        final String invalidValueReplacement = miningField.getInvalidValueReplacement().toString();
        invalidValueReplacementExpr = new StringLiteralExpr(invalidValueReplacement);
    } else {
        invalidValueReplacementExpr = new NullLiteralExpr();
    }
    DataField dataField = getMappedDataField(mappedFields);
    NodeList<Expression> allowedValuesExpressions = dataField != null ? getAllowedValuesExpressions(dataField) : new NodeList<>();
    NodeList<Expression> intervalsExpressions = dataField != null ? getIntervalsExpressions(dataField) : new NodeList<>();
    builder.setArgument(0, nameExpr);
    getChainedMethodCallExprFrom("withFieldUsageType", initializer).setArgument(0, fieldUsageTypeExpr);
    getChainedMethodCallExprFrom("withOpType", initializer).setArgument(0, opTypeExpr);
    getChainedMethodCallExprFrom("withDataType", initializer).setArgument(0, dataTypeExpr);
    getChainedMethodCallExprFrom("withMissingValueTreatmentMethod", initializer).setArgument(0, missingValueTreatmentMethodExpr);
    getChainedMethodCallExprFrom("withInvalidValueTreatmentMethod", initializer).setArgument(0, invalidValueTreatmentMethodExpr);
    getChainedMethodCallExprFrom("withMissingValueReplacement", initializer).setArgument(0, missingValueReplacementExpr);
    getChainedMethodCallExprFrom("withInvalidValueReplacement", initializer).setArgument(0, invalidValueReplacementExpr);
    getChainedMethodCallExprFrom("withAllowedValues", initializer).getArgument(0).asMethodCallExpr().setArguments(allowedValuesExpressions);
    getChainedMethodCallExprFrom("withIntervals", initializer).getArgument(0).asMethodCallExpr().setArguments(intervalsExpressions);
    miningFieldBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) INVALID_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Field(org.dmg.pmml.Field) MiningField(org.dmg.pmml.MiningField) DataField(org.dmg.pmml.DataField) MISSING_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.MISSING_VALUE_TREATMENT_METHOD) FIELD_USAGE_TYPE(org.kie.pmml.api.enums.FIELD_USAGE_TYPE) DataField(org.dmg.pmml.DataField) Expression(com.github.javaparser.ast.expr.Expression) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) DataType(org.dmg.pmml.DataType) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 13 with MiningField

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

the class PMMLModelTestUtils method getMiningField.

public static MiningField getMiningField(String fieldName, MiningField.UsageType usageType) {
    MiningField toReturn = getRandomMiningField();
    toReturn.setName(FieldName.create(fieldName));
    toReturn.setUsageType(usageType);
    return toReturn;
}
Also used : MiningField(org.dmg.pmml.MiningField)

Example 14 with MiningField

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

the class ModelUtilsTest method getOpTypeByMiningFieldsNotFound.

@Test(expected = KiePMMLInternalException.class)
public void getOpTypeByMiningFieldsNotFound() {
    final Model model = new RegressionModel();
    final DataDictionary dataDictionary = new DataDictionary();
    final MiningSchema miningSchema = new MiningSchema();
    IntStream.range(0, 3).forEach(i -> {
        String fieldName = "field" + i;
        final DataField dataField = getRandomDataField();
        dataField.setName(FieldName.create(fieldName));
        dataDictionary.addDataFields(dataField);
        final MiningField miningField = getRandomMiningField();
        miningField.setName(dataField.getName());
        miningSchema.addMiningFields(miningField);
    });
    model.setMiningSchema(miningSchema);
    ModelUtils.getOpType(getFieldsFromDataDictionary(dataDictionary), model, "NOT_EXISTING");
}
Also used : PMMLModelTestUtils.getMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getMiningField) MiningField(org.dmg.pmml.MiningField) PMMLModelTestUtils.getRandomMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField) MiningSchema(org.dmg.pmml.MiningSchema) DataField(org.dmg.pmml.DataField) PMMLModelTestUtils.getDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataField) PMMLModelTestUtils.getRandomDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField) Model(org.dmg.pmml.Model) RegressionModel(org.dmg.pmml.regression.RegressionModel) DataDictionary(org.dmg.pmml.DataDictionary) CommonTestingUtils.getFieldsFromDataDictionary(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary) RegressionModel(org.dmg.pmml.regression.RegressionModel) Test(org.junit.Test)

Example 15 with MiningField

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

the class KiePMMLUtilTest method populateMissingOutputFieldDataType.

@Test
public void populateMissingOutputFieldDataType() {
    Random random = new Random();
    List<String> fieldNames = IntStream.range(0, 6).mapToObj(i -> RandomStringUtils.random(6, true, false)).collect(Collectors.toList());
    List<DataField> dataFields = fieldNames.stream().map(fieldName -> {
        DataField toReturn = new DataField();
        toReturn.setName(FieldName.create(fieldName));
        DataType dataType = DataType.values()[random.nextInt(DataType.values().length)];
        toReturn.setDataType(dataType);
        return toReturn;
    }).collect(Collectors.toList());
    List<MiningField> miningFields = IntStream.range(0, dataFields.size() - 1).mapToObj(dataFields::get).map(dataField -> {
        MiningField toReturn = new MiningField();
        toReturn.setName(FieldName.create(dataField.getName().getValue()));
        toReturn.setUsageType(MiningField.UsageType.ACTIVE);
        return toReturn;
    }).collect(Collectors.toList());
    DataField lastDataField = dataFields.get(dataFields.size() - 1);
    MiningField targetMiningField = new MiningField();
    targetMiningField.setName(FieldName.create(lastDataField.getName().getValue()));
    targetMiningField.setUsageType(MiningField.UsageType.TARGET);
    miningFields.add(targetMiningField);
    // Following OutputFields should be populated based on "ResultFeature.PROBABILITY"
    List<OutputField> outputFields = IntStream.range(0, 3).mapToObj(i -> {
        OutputField toReturn = new OutputField();
        toReturn.setName(FieldName.create(RandomStringUtils.random(6, true, false)));
        toReturn.setResultFeature(ResultFeature.PROBABILITY);
        return toReturn;
    }).collect(Collectors.toList());
    // Following OutputField should be populated based on "ResultFeature.PREDICTED_VALUE"
    OutputField targetOutputField = new OutputField();
    targetOutputField.setName(FieldName.create(RandomStringUtils.random(6, true, false)));
    targetOutputField.setResultFeature(ResultFeature.PREDICTED_VALUE);
    outputFields.add(targetOutputField);
    // Following OutputField should be populated based on "TargetField" property
    OutputField targetingOutputField = new OutputField();
    targetingOutputField.setName(FieldName.create(RandomStringUtils.random(6, true, false)));
    targetingOutputField.setTargetField(FieldName.create(targetMiningField.getName().getValue()));
    outputFields.add(targetingOutputField);
    outputFields.forEach(outputField -> assertNull(outputField.getDataType()));
    IntStream.range(0, 2).forEach(i -> {
        OutputField toAdd = new OutputField();
        toAdd.setName(FieldName.create(RandomStringUtils.random(6, true, false)));
        DataType dataType = DataType.values()[random.nextInt(DataType.values().length)];
        toAdd.setDataType(dataType);
        outputFields.add(toAdd);
    });
    KiePMMLUtil.populateMissingOutputFieldDataType(outputFields, miningFields, dataFields);
    outputFields.forEach(outputField -> assertNotNull(outputField.getDataType()));
}
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) Random(java.util.Random) DataField(org.dmg.pmml.DataField) DataType(org.dmg.pmml.DataType) OutputField(org.dmg.pmml.OutputField) Test(org.junit.Test)

Aggregations

MiningField (org.dmg.pmml.MiningField)59 DataField (org.dmg.pmml.DataField)40 Test (org.junit.Test)39 MiningSchema (org.dmg.pmml.MiningSchema)33 DataDictionary (org.dmg.pmml.DataDictionary)25 RegressionModel (org.dmg.pmml.regression.RegressionModel)24 Model (org.dmg.pmml.Model)22 PMMLModelTestUtils.getRandomDataField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField)20 PMML (org.dmg.pmml.PMML)18 PMMLModelTestUtils.getRandomMiningField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField)18 PMMLModelTestUtils.getMiningField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getMiningField)17 PMMLModelTestUtils.getDataField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataField)16 OutputField (org.dmg.pmml.OutputField)15 CommonTestingUtils.getFieldsFromDataDictionary (org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary)15 FieldName (org.dmg.pmml.FieldName)12 Target (org.dmg.pmml.Target)11 Targets (org.dmg.pmml.Targets)11 OP_TYPE (org.kie.pmml.api.enums.OP_TYPE)11 HashMap (java.util.HashMap)10 List (java.util.List)10