Search in sources :

Example 21 with OP_TYPE

use of org.kie.pmml.api.enums.OP_TYPE in project drools by kiegroup.

the class ModelUtilsTest method getOpTypeFromTargets.

@Test
public void getOpTypeFromTargets() {
    Optional<OP_TYPE> opType = ModelUtils.getOpTypeFromTargets(null, "vsd");
    assertNotNull(opType);
    assertFalse(opType.isPresent());
    final Targets targets = new Targets();
    opType = ModelUtils.getOpTypeFromTargets(targets, "vsd");
    assertNotNull(opType);
    assertFalse(opType.isPresent());
    IntStream.range(0, 3).forEach(i -> {
        final Target target = getRandomTarget();
        targets.addTargets(target);
    });
    targets.getTargets().forEach(target -> {
        Optional<OP_TYPE> retrieved = ModelUtils.getOpTypeFromTargets(targets, target.getField().getValue());
        assertNotNull(retrieved);
        assertTrue(retrieved.isPresent());
        OP_TYPE expected = OP_TYPE.byName(target.getOpType().value());
        assertEquals(expected, retrieved.get());
    });
}
Also used : Target(org.dmg.pmml.Target) PMMLModelTestUtils.getRandomTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTarget) PMMLModelTestUtils.getTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getTarget) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) Targets(org.dmg.pmml.Targets) Test(org.junit.Test)

Example 22 with OP_TYPE

use of org.kie.pmml.api.enums.OP_TYPE in project drools by kiegroup.

the class TargetFieldFactory method getTargetFieldVariableInitializer.

static ObjectCreationExpr getTargetFieldVariableInitializer(final TargetField targetField) {
    final MethodDeclaration methodDeclaration = TARGET_TEMPLATE.getMethodsByName(GETTARGET_FIELD).get(0).clone();
    final BlockStmt targetBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(targetBody, TARGET_FIELD).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TARGET_FIELD, targetBody)));
    variableDeclarator.setName(targetField.getName());
    final ObjectCreationExpr toReturn = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TARGET_FIELD, targetBody))).asObjectCreationExpr();
    final NodeList<Expression> arguments = new NodeList<>();
    if (targetField.getTargetValues() != null) {
        for (TargetValue targetValue : targetField.getTargetValues()) {
            arguments.add(getTargetValueVariableInitializer(targetValue));
        }
    }
    toReturn.getArgument(0).asMethodCallExpr().setArguments(arguments);
    OP_TYPE oPT = targetField.getOpType();
    Expression opType = oPT != null ? new NameExpr(oPT.getClass().getName() + "." + oPT.name()) : new NullLiteralExpr();
    toReturn.setArgument(1, opType);
    toReturn.setArgument(2, getExpressionForObject(targetField.getField()));
    CAST_INTEGER cstInt = targetField.getCastInteger();
    Expression castInteger = cstInt != null ? new NameExpr(cstInt.getClass().getName() + "." + cstInt.name()) : new NullLiteralExpr();
    toReturn.setArgument(3, castInteger);
    toReturn.setArgument(4, getExpressionForObject(targetField.getMin()));
    toReturn.setArgument(5, getExpressionForObject(targetField.getMax()));
    toReturn.setArgument(6, getExpressionForObject(targetField.getRescaleConstant()));
    toReturn.setArgument(7, getExpressionForObject(targetField.getRescaleFactor()));
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) TargetValue(org.kie.pmml.api.models.TargetValue) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NodeList(com.github.javaparser.ast.NodeList) NameExpr(com.github.javaparser.ast.expr.NameExpr) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) CAST_INTEGER(org.kie.pmml.api.enums.CAST_INTEGER)

Example 23 with OP_TYPE

use of org.kie.pmml.api.enums.OP_TYPE in project drools by kiegroup.

the class ModelUtilsTest method getTargetFieldsWithTargetFieldsWithTargetsWithOptType.

@Test
public void getTargetFieldsWithTargetFieldsWithTargetsWithOptType() {
    final Model model = new RegressionModel();
    final DataDictionary dataDictionary = new DataDictionary();
    final MiningSchema miningSchema = new MiningSchema();
    final Targets targets = new Targets();
    IntStream.range(0, 3).forEach(i -> {
        final String fieldName = "fieldName-" + i;
        final DataField dataField = getDataField(fieldName, OpType.CATEGORICAL, DataType.STRING);
        dataDictionary.addDataFields(dataField);
        final MiningField miningField = getMiningField(fieldName, MiningField.UsageType.PREDICTED);
        miningField.setOpType(OpType.CONTINUOUS);
        miningSchema.addMiningFields(miningField);
        final Target targetField = getTarget(fieldName, OpType.CATEGORICAL);
        targets.addTargets(targetField);
    });
    model.setMiningSchema(miningSchema);
    model.setTargets(targets);
    List<KiePMMLNameOpType> retrieved = ModelUtils.getTargetFields(getFieldsFromDataDictionary(dataDictionary), model);
    assertNotNull(retrieved);
    assertEquals(miningSchema.getMiningFields().size(), retrieved.size());
    retrieved.forEach(kiePMMLNameOpType -> {
        Optional<Target> optionalTarget = targets.getTargets().stream().filter(fld -> kiePMMLNameOpType.getName().equals(fld.getField().getValue())).findFirst();
        assertTrue(optionalTarget.isPresent());
        Target target = optionalTarget.get();
        OP_TYPE expected = OP_TYPE.byName(target.getOpType().value());
        assertEquals(expected, kiePMMLNameOpType.getOpType());
    });
}
Also used : RESULT_FEATURE(org.kie.pmml.api.enums.RESULT_FEATURE) ModelUtils.getPrefixedName(org.kie.pmml.compiler.api.utils.ModelUtils.getPrefixedName) Arrays(java.util.Arrays) Date(java.util.Date) Model(org.dmg.pmml.Model) MiningSchema(org.dmg.pmml.MiningSchema) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) Row(org.dmg.pmml.Row) FieldName(org.dmg.pmml.FieldName) FIELD_USAGE_TYPE(org.kie.pmml.api.enums.FIELD_USAGE_TYPE) OpType(org.dmg.pmml.OpType) Map(java.util.Map) InputCell(org.jpmml.model.inlinetable.InputCell) PMMLModelTestUtils.getDataTypes(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataTypes) PMMLModelTestUtils.getMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getMiningField) RegressionModel(org.dmg.pmml.regression.RegressionModel) Targets(org.dmg.pmml.Targets) DataType(org.dmg.pmml.DataType) PMMLModelTestUtils.getRandomRow(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomRow) Collectors(java.util.stream.Collectors) PMMLModelTestUtils.getParameterFields(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getParameterFields) DataField(org.dmg.pmml.DataField) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) PMMLModelTestUtils.getArray(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getArray) PMMLModelTestUtils.getRandomDataType(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataType) ParameterField(org.dmg.pmml.ParameterField) IntStream(java.util.stream.IntStream) OutputField(org.dmg.pmml.OutputField) PMMLModelTestUtils.getDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataField) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Field(org.dmg.pmml.Field) DerivedField(org.dmg.pmml.DerivedField) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) KiePMMLNameOpType(org.kie.pmml.commons.model.tuples.KiePMMLNameOpType) OutputCell(org.jpmml.model.inlinetable.OutputCell) MiningField(org.dmg.pmml.MiningField) Iterator(java.util.Iterator) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) DataDictionary(org.dmg.pmml.DataDictionary) Test(org.junit.Test) PMMLModelTestUtils.getRandomDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField) CommonTestingUtils.getFieldsFromDataDictionary(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary) Target(org.dmg.pmml.Target) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) Array(org.dmg.pmml.Array) PMMLModelTestUtils.getRandomOutputField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomOutputField) PMMLModelTestUtils.getRandomTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTarget) Assert.assertNull(org.junit.Assert.assertNull) PMMLModelTestUtils.getRandomMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField) PMMLModelTestUtils.getTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getTarget) PMMLModelTestUtils.getRandomRowWithCells(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomRowWithCells) Assert.assertEquals(org.junit.Assert.assertEquals) 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) Targets(org.dmg.pmml.Targets) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) DataDictionary(org.dmg.pmml.DataDictionary) CommonTestingUtils.getFieldsFromDataDictionary(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary) RegressionModel(org.dmg.pmml.regression.RegressionModel) KiePMMLNameOpType(org.kie.pmml.commons.model.tuples.KiePMMLNameOpType) Target(org.dmg.pmml.Target) PMMLModelTestUtils.getRandomTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTarget) PMMLModelTestUtils.getTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getTarget) 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) Test(org.junit.Test)

Example 24 with OP_TYPE

use of org.kie.pmml.api.enums.OP_TYPE in project drools by kiegroup.

the class ModelUtilsTest method getOpTypeByTargets.

@Test
public void getOpTypeByTargets() {
    final Model model = new RegressionModel();
    final DataDictionary dataDictionary = new DataDictionary();
    final MiningSchema miningSchema = new MiningSchema();
    final Targets targets = new Targets();
    IntStream.range(0, 3).forEach(i -> {
        final DataField dataField = getRandomDataField();
        dataDictionary.addDataFields(dataField);
        final MiningField miningField = getRandomMiningField();
        miningField.setName(dataField.getName());
        miningSchema.addMiningFields(miningField);
        final Target targetField = getRandomTarget();
        targetField.setField(dataField.getName());
        targets.addTargets(targetField);
    });
    model.setMiningSchema(miningSchema);
    model.setTargets(targets);
    getFieldsFromDataDictionary(dataDictionary);
    targets.getTargets().forEach(target -> {
        OP_TYPE retrieved = ModelUtils.getOpType(getFieldsFromDataDictionary(dataDictionary), model, target.getField().getValue());
        assertNotNull(retrieved);
        OP_TYPE expected = OP_TYPE.byName(target.getOpType().value());
        assertEquals(expected, retrieved);
    });
}
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) Target(org.dmg.pmml.Target) PMMLModelTestUtils.getRandomTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTarget) PMMLModelTestUtils.getTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getTarget) 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) Targets(org.dmg.pmml.Targets) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) 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 25 with OP_TYPE

use of org.kie.pmml.api.enums.OP_TYPE in project drools by kiegroup.

the class KiePMMLDerivedFieldInstanceFactory method getKiePMMLDerivedField.

static KiePMMLDerivedField getKiePMMLDerivedField(final DerivedField derivedField, final List<Field<?>> fields) {
    DataType dataType = derivedField.getDataType() != null ? derivedField.getDataType() : getDataType(fields, derivedField.getName().getValue());
    OP_TYPE opType = derivedField.getOpType() != null ? OP_TYPE.byName(derivedField.getOpType().value()) : null;
    return KiePMMLDerivedField.builder(derivedField.getName().getValue(), getKiePMMLExtensions(derivedField.getExtensions()), DATA_TYPE.byName(dataType.value()), opType, getKiePMMLExpression(derivedField.getExpression())).withDisplayName(derivedField.getDisplayName()).build();
}
Also used : DataType(org.dmg.pmml.DataType) ModelUtils.getDataType(org.kie.pmml.compiler.api.utils.ModelUtils.getDataType) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE)

Aggregations

OP_TYPE (org.kie.pmml.api.enums.OP_TYPE)31 DATA_TYPE (org.kie.pmml.api.enums.DATA_TYPE)14 DataField (org.dmg.pmml.DataField)13 Test (org.junit.Test)13 MiningField (org.dmg.pmml.MiningField)12 DataDictionary (org.dmg.pmml.DataDictionary)8 MiningSchema (org.dmg.pmml.MiningSchema)8 OutputField (org.dmg.pmml.OutputField)8 CommonTestingUtils.getFieldsFromDataDictionary (org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary)8 PMMLModelTestUtils.getDataField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataField)8 PMMLModelTestUtils.getMiningField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getMiningField)8 PMMLModelTestUtils.getRandomDataField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField)8 PMMLModelTestUtils.getRandomMiningField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField)8 Model (org.dmg.pmml.Model)7 Target (org.dmg.pmml.Target)7 RegressionModel (org.dmg.pmml.regression.RegressionModel)7 FIELD_USAGE_TYPE (org.kie.pmml.api.enums.FIELD_USAGE_TYPE)7 DataType (org.dmg.pmml.DataType)6 DerivedField (org.dmg.pmml.DerivedField)6 ParameterField (org.dmg.pmml.ParameterField)6